Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions docs/plans/DIAGNOSTICS-STATIC-ANALYZERS-PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
153 changes: 127 additions & 26 deletions src/sharplsp/tests/e2e_modules/configuration_live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
["<OutputType>Library</OutputType>", ""],
"namespace Lib;\n\npublic sealed class Used { }\n\npublic sealed class Orphan { }\n",
);
let _app = write_project(root, "App", "cs", ["<OutputType>Exe</OutputType>", r#"<ItemGroup><ProjectReference Include="../Lib/Lib.csproj" /></ItemGroup>"#],
"internal static class Program\n{\n private static void Main() => _ = new Lib.Used();\n}\n",
);
std::fs::write(
root.join("Mono.slnx"),
"<Solution>\n <Project Path=\"Lib/Lib.csproj\" />\n <Project Path=\"App/App.csproj\" />\n</Solution>\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",
["<OutputType>Library</OutputType>", ""],
"module Lib.Values\n\nlet used () = 1\n\nlet orphan () = 2\n",
);
let _app = write_project(
root,
"App",
"fs",
[
"<OutputType>Exe</OutputType>",
r#"<ItemGroup><ProjectReference Include="../Lib/Lib.fsproj" /></ItemGroup>"#,
],
"module App.Program\n\n[<EntryPoint>]\nlet main _ = Lib.Values.used ()\n",
);
std::fs::write(
root.join("Mono.slnx"),
"<Solution>\n <Project Path=\"Lib/Lib.fsproj\" />\n <Project Path=\"App/App.fsproj\" />\n</Solution>\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 `<name>/<name>.<ext>proj` with `properties` and `items`, and its one
/// source `<name>.<ext>` (`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!("<ItemGroup><Compile Include=\"{name}.fs\" /></ItemGroup>")
} else {
String::new()
};
std::fs::write(
project.join("Dead.csproj"),
r#"<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup><TargetFramework>net10.0</TargetFramework><OutputType>Library</OutputType></PropertyGroup>
</Project>"#,
project.join(format!("{name}.{ext}proj")),
format!(
"<Project Sdk=\"Microsoft.NET.Sdk\">\n <PropertyGroup><TargetFramework>net10.0</TargetFramework>{properties}</PropertyGroup>\n {compile}\n {items}\n</Project>"
),
)
.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",
["<OutputType>Library</OutputType>", ""],
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#"<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup>
<ItemGroup><Compile Include="Dead.fs" /></ItemGroup>
</Project>"#,
)
.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`.
Expand Down Expand Up @@ -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
Expand Down