NuGet crawler test crawls the host's ~/.nuget/packages and fails on dev machines

未关闭 适合新手
#2,023 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
devtools, testing

调研方向

阅读 crates/socket-patch-core/src/crawlers/nuget_crawler.rs,重点查看测试 solution_roots_and_unrestored_projects_keep_the_walk 以及第 1450 行附近的 NUGET_PACKAGES 测试;检查 crate::test_env::EnvGuard,了解现有的环境隔离模式。运行 issue 中的 cargo test 命令,以复现问题并验证修复。完成标准是两个测试都使用受保护的环境设置并通过,且不读取主机的 NuGet 包文件夹。

由索引模型根据 Issue 内容生成。

描述

bug pm:nuget

LLM Description written by Claude Code:claude-opus-5-5

Summary

crawlers::nuget_crawler::tests::solution_roots_and_unrestored_projects_keep_the_walk (crates/socket-patch-core/src/crawlers/nuget_crawler.rs:1978) is not hermetic. It crawls the real global packages folder of the machine running it, so it fails on any developer machine whose ~/.nuget/packages holds a package. It passes in CI only because the runners' NuGet cache is empty.

Repro

On main (7bde3ba5), on a machine with any package in ~/.nuget/packages:

cargo test --locked -p socket-patch-core --lib solution_roots_and_unrestored_projects_keep_the_walk
panicked at crates/socket-patch-core/src/crawlers/nuget_crawler.rs:1994:9:
  left: ["pkg:nuget/[email protected]", "pkg:nuget/[email protected]", "pkg:nuget/[email protected]", "pkg:nuget/[email protected]"]
 right: ["pkg:nuget/[email protected]", "pkg:nuget/[email protected]", "pkg:nuget/[email protected]"]

The extra purl comes from the host's ~/.nuget/packages/socketsecurity.socketpatch/3.3.0. The test fails every time on such a machine, and passes with an isolated home:

HOME=$(mktemp -d) CARGO_HOME=~/.cargo RUSTUP_HOME=~/.rustup \
  cargo test --locked -p socket-patch-core --lib solution_roots_and_unrestored_projects_keep_the_walk
# test result: ok. 1 passed

Root cause

In project mode, crawl_roots always adds the global packages folder NuGet would restore into as search root 3 (nuget_crawler.rs:626-630, via global_packages_folder → nuget_home(), which falls back to $DOTNET_CLI_HOME or ~/.nuget/packages). A solution root, or a project that hasn't been restored, has no restore scope (roots.scope is set only for DotnetRoot::Project), so every package in that folder is crawled.

The test builds its packages in a temp shared-cache named by packageFolders, but leaves NUGET_PACKAGES and DOTNET_CLI_HOME unset. Its expected list (every) therefore assumes the host's default folder is empty or missing.

Suggested fix

Run the crawl under the existing crate::test_env::EnvGuard, with NUGET_PACKAGES pointed at an empty directory inside the test's tempdir (and DOTNET_CLI_HOME cleared). That makes root 3 hermetic without changing the crawler.

The NUGET_PACKAGES test around nuget_crawler.rs:1450 sets and removes the variable with bare std::env::set_var/remove_var, outside the guard. It should move onto EnvGuard too, so a parallel crawl test can't observe the variable while it's set.

Note

The product behavior the test pins, where a solution-root or unrestored-project crawl includes every package in the user's global folder, is the unscoped "today's walk" that #427 / #1183 deliberately kept for those roots. This issue is only about the test's isolation.

Found while validating #2015.

主要语言
Rust
星标
9
派生
2
平均合并
20 小时 9 分钟
30 天内合并 PR
629

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

SocketDev/socket-patch 的其他 Issue

查看 SocketDev/socket-patch 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。