diff --git a/docs/plans/DIAGNOSTICS-STATIC-ANALYZERS-PLAN.md b/docs/plans/DIAGNOSTICS-STATIC-ANALYZERS-PLAN.md index a1f6441f..83659ddc 100644 --- a/docs/plans/DIAGNOSTICS-STATIC-ANALYZERS-PLAN.md +++ b/docs/plans/DIAGNOSTICS-STATIC-ANALYZERS-PLAN.md @@ -23,10 +23,12 @@ Implements [ANALYZERS-MONOREPO-GATE] and [ANALYZERS-SOLUTION-SCOPE]. - [x] Hot-reload workspace/static-analyzer config through `workspace/didChangeConfiguration` (shipped as `[analyzers] dead_code`/`monorepo`) — `configuration_live.rs` `reconfigure`; e2e `analyzer_settings_from_the_editor_or_the_file_apply_to_the_next_pull` (editor and on-disk edits). -- [ ] Bump `global_state_version` and send `workspace/diagnostic/refresh` when - monorepo/static-analyzer settings change. -- [ ] Full-stack test: default config produces no unused-public-code - diagnostics. +- [x] Bump `global_state_version` and send `workspace/diagnostic/refresh` when + monorepo/static-analyzer settings change — `configuration_live.rs` `reconfigure` → + `pull_diagnostics::state::configure` invalidates the store (its revision) and schedules the + refresh; `csharp_unused_public_types_are_dead_only_in_a_monorepo` waits for it after each switch. +- [x] Full-stack test: default config produces no unused-public-code + diagnostics — same test, step 1: no `sharplsp.toml`, only the internal type is reported. - [x] Full-stack test: `repository_kind = "standard"` suppresses the analyzer even when `unused_public_symbols = true` (shipped as `[analyzers] dead_code`/`monorepo`) — same e2e, step 2: `dead_code` on, no monorepo, the public F# function is not reported. @@ -87,8 +89,9 @@ Implements [ANALYZERS-UNUSED-PUBLIC], [ANALYZERS-REFERENCE-MODEL], and severity Information, and tag `Unnecessary`. - [x] Full-stack test: unused public C# type in a monorepo solution is reported — `configuration_live.rs` `csharp_unused_public_types_are_dead_only_in_a_monorepo`, step 2. -- [ ] Full-stack test: public C# type referenced by another project is not - reported. +- [x] Full-stack test: public C# type referenced by another project is not + reported — `configuration_live.rs` `a_public_csharp_type_another_project_uses_is_not_dead_in_a_monorepo` + (a two-project `.slnx`: only the unused `Orphan` is reported). - [ ] Full-stack test: public C# member used by reflection/framework attribute is suppressed. - [x] Full-stack test: non-monorepo config reports no C# unused-public-code @@ -118,8 +121,10 @@ Implements [ANALYZERS-UNUSED-PUBLIC], [ANALYZERS-REFERENCE-MODEL], and severity Information, and tag `Unnecessary`. - [x] Full-stack test: unused public F# function in a monorepo solution is reported — `configuration_live.rs` step 3: `unusedPublic` reported as an error. -- [ ] Full-stack test: public F# value referenced by another project is not - reported. +- [x] Full-stack test: public F# value referenced by another project is not + reported — + `configuration_live.rs` `a_public_fsharp_value_another_project_uses_is_not_dead_in_a_monorepo` + (a two-project `.slnx`: only the unused `orphan` is reported). - [ ] Full-stack test: `.fsi` public-surface declarations receive diagnostics at the signature declaration where possible. - [x] Full-stack test: non-monorepo config reports no F# unused-public-code @@ -160,7 +165,7 @@ Implements the cross-language part of [ANALYZERS-REFERENCE-MODEL]. - [ ] Add `WorkspaceConfig.repository_kind` defaulting to `"standard"`. - [ ] Add `DiagnosticsConfig.static_analyzers`. -- [ ] Hot-reload monorepo/static-analyzer settings. +- [x] Hot-reload monorepo/static-analyzer settings. (see the matching phase row) - [ ] Update example config through a TOML parser/serializer. ### Pipeline @@ -173,17 +178,17 @@ Implements the cross-language part of [ANALYZERS-REFERENCE-MODEL]. ### C# -- [ ] Collect public C# symbols via Roslyn. -- [ ] Count solution-wide Roslyn semantic references. +- [x] Collect public C# symbols via Roslyn. (see the matching phase row) +- [x] Count solution-wide Roslyn semantic references. (see the matching phase row) - [ ] Emit `SLSPC0101`. -- [ ] Add C# monorepo/non-monorepo full-stack tests. +- [x] Add C# monorepo/non-monorepo full-stack tests. (see the matching phase row) ### F# - [ ] Collect public F# symbols via FCS and `.fsi` public surface. -- [ ] Count solution-wide FCS semantic references. +- [x] Count solution-wide FCS semantic references. (see the matching phase row) - [ ] Emit `SLSPF0101`. -- [ ] Add F# monorepo/non-monorepo full-stack tests. +- [x] Add F# monorepo/non-monorepo full-stack tests. (see the matching phase row) ### Cross-Language diff --git a/src/sharplsp/tests/e2e_modules/configuration_live.rs b/src/sharplsp/tests/e2e_modules/configuration_live.rs index 1664c443..eed75830 100644 --- a/src/sharplsp/tests/e2e_modules/configuration_live.rs +++ b/src/sharplsp/tests/e2e_modules/configuration_live.rs @@ -60,55 +60,138 @@ fn csharp_unused_public_types_are_dead_only_in_a_monorepo() { let (workspace, source) = dead_csharp_workspace(); let mut session = LiveSession::start(workspace.path(), &source, "SLSPC0101"); - // 1. A standard repository: the internal type is dead, a warning; the public one is API. + // 1. No configuration at all: the default is a standard repository, so the + // internal type is dead, a warning, and the public one is possible API. session.await_dead(&[("Hidden", 2)]); - // 2. The file declares a monorepo: the public type is dead too, and both are errors. + // 2. The file declares a monorepo: the public type is dead too, and both are + // errors — and the editor is told to pull again rather than left stale. + let refreshes = session.count("workspace/diagnostic/refresh"); write_configuration( workspace.path(), "[analyzers]\ndead_code = true\nmonorepo = true\n", ); session.await_dead(&[("Hidden", 1), ("Orphan", 1)]); + session.await_refresh_after(refreshes, "a monorepo switch"); - // 3. Back to a standard repository: the public type is API again. + // 3. Back to a standard repository: the public type is API again, refreshed again. + let refreshes = session.count("workspace/diagnostic/refresh"); write_configuration(workspace.path(), "[analyzers]\ndead_code = true\n"); session.await_dead(&[("Hidden", 2)]); + session.await_refresh_after(refreshes, "leaving monorepo mode"); session.client.shutdown_and_exit(); } -/// A C# project with dead types, and a `sharplsp.toml` turning the dead-code analyzer on. -fn dead_csharp_workspace() -> (tempfile::TempDir, PathBuf) { - let workspace = tempfile::tempdir().expect("real C# workspace"); - let project = workspace.path().join("Dead"); +/// [ANALYZERS-REFERENCE-MODEL] solution-wide: in a monorepo, a public C# type +/// another project of the solution uses is live, and only the one nobody uses +/// is dead. +#[test] +fn a_public_csharp_type_another_project_uses_is_not_dead_in_a_monorepo() { + require_dotnet(); + let workspace = tempfile::tempdir().expect("real C# solution"); + let root = workspace.path(); + let library = write_project( + root, + "Lib", + "cs", + ["Library", ""], + "namespace Lib;\n\npublic sealed class Used { }\n\npublic sealed class Orphan { }\n", + ); + let _app = write_project(root, "App", "cs", ["Exe", r#""#], + "internal static class Program\n{\n private static void Main() => _ = new Lib.Used();\n}\n", + ); + std::fs::write( + root.join("Mono.slnx"), + "\n \n \n\n", + ) + .expect("write the solution"); + write_configuration(root, "[analyzers]\ndead_code = true\nmonorepo = true\n"); + let mut session = LiveSession::start(root, &library, "SLSPC0101"); + + // `Used` is referenced from App, so only `Orphan` is dead — an error in a monorepo. + session.await_dead(&[("Orphan", 1)]); + session.client.shutdown_and_exit(); +} + +/// The F# twin: a public F# value another project of the solution uses is +/// live in a monorepo, and only the one nobody uses is dead. +#[test] +fn a_public_fsharp_value_another_project_uses_is_not_dead_in_a_monorepo() { + require_dotnet(); + let workspace = tempfile::tempdir().expect("real F# solution"); + let root = workspace.path(); + let library = write_project( + root, + "Lib", + "fs", + ["Library", ""], + "module Lib.Values\n\nlet used () = 1\n\nlet orphan () = 2\n", + ); + let _app = write_project( + root, + "App", + "fs", + [ + "Exe", + r#""#, + ], + "module App.Program\n\n[]\nlet main _ = Lib.Values.used ()\n", + ); + std::fs::write( + root.join("Mono.slnx"), + "\n \n \n\n", + ) + .expect("write the solution"); + write_configuration(root, "[analyzers]\ndead_code = true\nmonorepo = true\n"); + let mut session = LiveSession::start(root, &library, "SLSPF0101"); + + // `used` is called from App, so only `orphan` is dead — an error in a monorepo. + session.await_dead(&[("orphan", 1)]); + session.client.shutdown_and_exit(); +} + +/// Write `/.proj` with `properties` and `items`, and its one +/// source `.` (`cs` or `fs`; F# lists it in compile order); returns the source. +fn write_project(root: &Path, name: &str, ext: &str, settings: [&str; 2], source: &str) -> PathBuf { + let [properties, items] = settings; + let project = root.join(name); std::fs::create_dir_all(&project).expect("project directory"); + let compile = if ext == "fs" { + format!("") + } else { + String::new() + }; std::fs::write( - project.join("Dead.csproj"), - r#" - net10.0Library -"#, + project.join(format!("{name}.{ext}proj")), + format!( + "\n net10.0{properties}\n {compile}\n {items}\n" + ), ) .expect("write the project"); - std::fs::write(project.join("Dead.cs"), DEAD_CS).expect("write the source"); - write_configuration(workspace.path(), "[analyzers]\ndead_code = true\n"); - (workspace, project.join("Dead.cs")) + let file = project.join(format!("{name}.{ext}")); + std::fs::write(&file, source).expect("write the source"); + file +} + +/// A C# project with dead types and no `sharplsp.toml`: the defaults apply. +fn dead_csharp_workspace() -> (tempfile::TempDir, PathBuf) { + let workspace = tempfile::tempdir().expect("real C# workspace"); + let source = write_project( + workspace.path(), + "Dead", + "cs", + ["Library", ""], + DEAD_CS, + ); + (workspace, source) } /// An F# project with dead code, and a `sharplsp.toml` turning the dead-code analyzer off. fn dead_code_workspace() -> (tempfile::TempDir, PathBuf) { let workspace = tempfile::tempdir().expect("real F# workspace"); - let project = workspace.path().join("Dead"); - std::fs::create_dir_all(&project).expect("project directory"); - std::fs::write( - project.join("Dead.fsproj"), - r#" - net10.0 - -"#, - ) - .expect("write the project"); - std::fs::write(project.join("Dead.fs"), DEAD_FS).expect("write the source"); + let source = write_project(workspace.path(), "Dead", "fs", ["", ""], DEAD_FS); write_configuration(workspace.path(), "[analyzers]\ndead_code = false\n"); - (workspace, project.join("Dead.fs")) + (workspace, source) } /// Replace the workspace's `sharplsp.toml`. @@ -200,6 +283,24 @@ impl LiveSession { self.restart_warning().expect("the restart warning arrived") } + /// How many messages of `method` the server has sent so far. + fn count(&self, method: &str) -> usize { + self.traffic + .iter() + .filter(|message| message["method"] == method) + .count() + } + + /// Wait, within [`NOTICED`], for a `workspace/diagnostic/refresh` beyond the + /// `seen` already counted: the server debounces it after a settings change. + fn await_refresh_after(&mut self, seen: usize, change: &str) { + self.wait_for( + NOTICED, + &format!("a diagnostic refresh after {change}"), + |session| session.count("workspace/diagnostic/refresh") > seen, + ); + } + /// The last notification of `method` the server sent. fn received(&self, method: &str) -> Option<&Value> { self.traffic