diff --git a/.gitignore b/.gitignore index 0a6775b2a..a9d52e3b3 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,8 @@ paper/ # macOS Finder metadata .DS_Store + +# MSBuild restore/build output: editors with C# tooling restore the fixture +# .csproj files on open +tests/fixtures/**/obj/ +tests/fixtures/**/bin/ diff --git a/graphify/detect.py b/graphify/detect.py index d984d088b..4ad8a0e71 100644 --- a/graphify/detect.py +++ b/graphify/detect.py @@ -1149,6 +1149,53 @@ def _has_venv_markers(d: "Path") -> bool: return False +# Files only MSBuild/NuGet write. ``obj/`` gets the restore outputs (every +# SDK-style restore writes project.assets.json) and, for legacy non-SDK +# projects, the per-configuration FileListAbsolute.txt. ``bin/`` gets the +# runtime manifests the SDK emits next to every executable, plus the PDBs. +# .dll/.exe are deliberately absent: a checked-in tools/bin/ of vendored +# binaries next to its wrapper scripts is source, not output. +_MSBUILD_OBJ_MARKERS = ( + "project.assets.json", ".nuget.dgspec.json", ".nuget.g.props", + ".filelistabsolute.txt", +) +_MSBUILD_BIN_MARKERS = (".deps.json", ".runtimeconfig.json", ".pdb") + + +def _has_msbuild_markers(d: "Path", markers: tuple, max_depth: int) -> bool: + """True when a file ending in one of *markers* sits within *max_depth* + directory levels below *d*. + + ``bin`` and ``obj`` are MSBuild's output and intermediate dirs, and an + unignored C# project carries both after its first build: the generated + GlobalUsings/AssemblyInfo sources land in the graph as file nodes with + import edges to every implicit namespace, and the build-copied + appsettings*.json are scanned twice. ``dotnet new`` writes no .gitignore, + so nothing else stops them. Neither name is safe to prune alone — ``bin`` + is source in Rust's src/bin/, Ruby binstubs and script dirs — so prune + only on MSBuild's own artifacts, the same keep-on-doubt gating ``out`` + gets (#3347). The walk is bounded by entries scanned, not just depth. + """ + budget = 4000 + pending = [(str(d), 0)] + while pending: + current, depth = pending.pop() + try: + with os.scandir(current) as it: + for entry in it: + budget -= 1 + if budget <= 0: + return False + if entry.is_file(): + if entry.name.lower().endswith(markers): + return True + elif depth < max_depth and entry.is_dir(follow_symlinks=False): + pending.append((entry.path, depth + 1)) + except OSError: + continue + return False + + def _is_noise_dir(part: str, parent: "Path | None" = None) -> bool: """Return True if this directory name looks like a venv, cache, or dep dir.""" if part in _SKIP_DIRS: @@ -1173,6 +1220,15 @@ def _is_noise_dir(part: str, parent: "Path | None" = None) -> bool: if parent is None: return False # cannot verify; keep a possibly-real code dir return _has_build_output_markers(parent / part) + if part in ("obj", "bin"): + # Ambiguous: MSBuild intermediate/output dirs OR real source (Rust's + # src/bin/, script dirs). Depths reach a legacy obj// and a + # RID-specific bin////. + if parent is None: + return False # cannot verify; keep a possibly-real code dir + if part == "obj": + return _has_msbuild_markers(parent / part, _MSBUILD_OBJ_MARKERS, 1) + return _has_msbuild_markers(parent / part, _MSBUILD_BIN_MARKERS, 3) if part == "snapshots": # Prune only when it looks like an actual JS/Vitest snapshot dir. if parent is None: diff --git a/tests/test_msbuild_dir_evidence.py b/tests/test_msbuild_dir_evidence.py new file mode 100644 index 000000000..d0360e03e --- /dev/null +++ b/tests/test_msbuild_dir_evidence.py @@ -0,0 +1,108 @@ +"""MSBuild's ``obj``/``bin`` dirs are pruned only on MSBuild evidence. + +Neither name was skipped, so a C# project scanned after its first build — and +``dotnet new`` writes no .gitignore to hide them — put the generated +GlobalUsings/AssemblyInfo sources into the graph as file nodes with import +edges to every implicit namespace, and scanned the build-copied +appsettings*.json a second time. ``bin`` is also real source in Rust, Ruby and +script trees, so both get the ``out`` treatment (#3347): keep on doubt, prune +on proof. +""" + +from graphify.detect import detect + + +def _scanned(td): + d = detect(td) + return sorted( + str(f).replace("\\", "/") for cat in d["files"].values() for f in cat + ) + + +def _write(path, text=""): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text, encoding="utf-8") + + +def _sdk_project(root): + _write(root / "App.csproj", '\n') + _write(root / "Program.cs", "public class Program { }\n") + _write(root / "appsettings.json", "{}\n") + + +def test_built_sdk_project_prunes_obj_and_bin(tmp_path, monkeypatch): + """The layout `dotnet build` leaves behind contributes nothing.""" + monkeypatch.chdir(tmp_path) + _sdk_project(tmp_path) + _write(tmp_path / "obj/project.assets.json", "{}\n") + _write(tmp_path / "obj/App.csproj.nuget.dgspec.json", "{}\n") + _write(tmp_path / "obj/Debug/net8.0/App.GlobalUsings.g.cs", + "global using global::System;\n") + _write(tmp_path / "obj/Debug/net8.0/App.AssemblyInfo.cs", + "using System.Reflection;\n") + _write(tmp_path / "bin/Debug/net8.0/App.deps.json", "{}\n") + _write(tmp_path / "bin/Debug/net8.0/App.runtimeconfig.json", "{}\n") + _write(tmp_path / "bin/Debug/net8.0/appsettings.json", "{}\n") + + files = _scanned(tmp_path) + assert any(f.endswith("/Program.cs") for f in files), files + assert any(f.endswith("/appsettings.json") for f in files), files + assert not any("/obj/" in f or "/bin/" in f for f in files), files + + +def test_rid_specific_bin_is_pruned(tmp_path, monkeypatch): + """`dotnet publish -r ` nests the manifests one level deeper.""" + monkeypatch.chdir(tmp_path) + _sdk_project(tmp_path) + rid = tmp_path / "bin/Release/net8.0/linux-x64" + _write(rid / "App.runtimeconfig.json", "{}\n") + _write(rid / "appsettings.json", "{}\n") + + files = _scanned(tmp_path) + assert any(f.endswith("/Program.cs") for f in files), files + assert not any("/bin/" in f for f in files), files + + +def test_legacy_framework_project_prunes_obj_and_bin(tmp_path, monkeypatch): + """A non-SDK project has no project.assets.json, only per-config files.""" + monkeypatch.chdir(tmp_path) + _write(tmp_path / "Legacy.csproj", "\n") + _write(tmp_path / "Program.cs", "class Program { }\n") + _write(tmp_path / "obj/Debug/Legacy.csproj.FileListAbsolute.txt", + "C:\\src\\Legacy\\bin\\Debug\\Legacy.exe\n") + _write(tmp_path / "obj/Debug/TemporaryGeneratedFile_036C0B5B.cs", "\n") + (tmp_path / "bin/Debug").mkdir(parents=True) + (tmp_path / "bin/Debug/Legacy.pdb").write_bytes(b"Microsoft C/C++ MSF 7.00\r\n") + _write(tmp_path / "bin/Debug/Legacy.exe.config", "\n") + + files = _scanned(tmp_path) + assert any(f.endswith("/Program.cs") for f in files), files + assert not any("/obj/" in f or "/bin/" in f for f in files), files + + +def test_source_dirs_named_bin_and_obj_are_scanned(tmp_path, monkeypatch): + """Rust's src/bin/, a scripts bin/ and a source obj/ have no MSBuild + artifacts and stay in the corpus.""" + monkeypatch.chdir(tmp_path) + _write(tmp_path / "src/bin/cli.rs", "fn main() {}\n") + _write(tmp_path / "bin/deploy.sh", "#!/bin/sh\necho deploy\n") + _write(tmp_path / "engine/obj/mesh.cpp", "struct Mesh {};\n") + + files = _scanned(tmp_path) + assert any(f.endswith("src/bin/cli.rs") for f in files), files + assert any(f.endswith("bin/deploy.sh") for f in files), files + assert any(f.endswith("engine/obj/mesh.cpp") for f in files), files + + +def test_vendored_binaries_do_not_prune_bin(tmp_path, monkeypatch): + """A checked-in tools/bin/ of .exe/.dll next to its wrapper scripts is + source; compiled binaries alone are not MSBuild evidence.""" + monkeypatch.chdir(tmp_path) + tools = tmp_path / "tools/bin" + tools.mkdir(parents=True) + (tools / "nuget.exe").write_bytes(b"MZ\x90\x00") + (tools / "Newtonsoft.Json.dll").write_bytes(b"MZ\x90\x00") + _write(tools / "restore.ps1", "& $PSScriptRoot/nuget.exe restore\n") + + files = _scanned(tmp_path) + assert any(f.endswith("tools/bin/restore.ps1") for f in files), files