From 7a104ec6f14c47c58f4a21965023ffdbe3f7c34c Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Wed, 23 Sep 2026 13:44:25 +0800 Subject: [PATCH] fix(adopt): look for the other clients' configuration where Windows puts it Claude Desktop's config path was written out three times -- the MCP target list, the scan's source list, and once more in a test -- and all three said `Library/Application Support/`. On Windows it is under the roaming AppData instead, so all three were simply wrong there, and the shape of being wrong is that this machine's Claude Desktop is never found. For the scan that matters more than it sounds: missing an MCP configuration is exactly the hole it exists to close. One home for the path now, because three copies of an answer that differs by platform is three copies that drift, and the one that drifts is the one nobody looked at. The paths stay relative to `home` rather than reading `%APPDATA%`. This crate writes other people's configuration files, so its tests have to be isolatable by handing it a temporary directory; reading the environment takes that away, and a test that escapes isolation edits the real `~/.claude`. The cost is a machine whose AppData has been redirected, where the file is not found -- and not finding it is far cheaper than editing the wrong one. The managed-policy file has no home to be relative to, so that one does read the environment. It is `%PROGRAMDATA%` rather than a hardcoded `C:\ProgramData`, because the system drive is not always C and the consequence of assuming it is reporting "this machine has no managed policy" about a file that overrides the user's own configuration. Co-Authored-By: Claude Opus 5 --- crates/tw-adopt/src/detect.rs | 10 ++-- crates/tw-adopt/src/lib.rs | 1 + crates/tw-adopt/src/mcp.rs | 2 +- crates/tw-adopt/src/paths.rs | 94 +++++++++++++++++++++++++++++++++++ crates/tw-scan/src/sources.rs | 9 ++-- 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 crates/tw-adopt/src/paths.rs diff --git a/crates/tw-adopt/src/detect.rs b/crates/tw-adopt/src/detect.rs index 9c1cc673..2a40d7dd 100644 --- a/crates/tw-adopt/src/detect.rs +++ b/crates/tw-adopt/src/detect.rs @@ -234,9 +234,6 @@ fn shell_exports(home: &Path, names: &[&str]) -> Vec<(PathBuf, usize, String)> { out } -/// macOS 上的管理策略文件。**优先级压过一切**,包括用户自己的配置。 -const MANAGED: &str = "/Library/Application Support/ClaudeCode/managed-settings.json"; - /// 走一遍优先级链。`project` 是当前项目目录(有的话)。 pub fn diagnose(c: &Client, home: &Path, project: Option<&Path>) -> Vec { let d = detect_one(c, home); @@ -350,8 +347,9 @@ pub fn diagnose(c: &Client, home: &Path, project: Option<&Path>) -> Vec // 四、管理策略文件。**最高优先级,压过一切** if c.id == "claude-code" { - if Path::new(MANAGED).exists() { - let text = std::fs::read_to_string(MANAGED).unwrap_or_default(); + let managed = crate::paths::managed_settings(); + if managed.exists() { + let text = std::fs::read_to_string(&managed).unwrap_or_default(); let hits: Vec<_> = c.env_vars.iter().filter(|v| text.contains(**v)).collect(); out.push(Finding { level: if hits.is_empty() { @@ -360,7 +358,7 @@ pub fn diagnose(c: &Client, home: &Path, project: Option<&Path>) -> Vec Level::Blocking }, title: msg!("adopt.diag.managed" => "This machine has a managed-policy file"), - detail: msg!("adopt.diag.managed.detail", path = MANAGED => "{path} takes precedence over everything else, including the user's own configuration."), + detail: msg!("adopt.diag.managed.detail", path = managed.display() => "{path} takes precedence over everything else, including the user's own configuration."), fix: None, }); } else { diff --git a/crates/tw-adopt/src/lib.rs b/crates/tw-adopt/src/lib.rs index a810a946..f08d47b7 100644 --- a/crates/tw-adopt/src/lib.rs +++ b/crates/tw-adopt/src/lib.rs @@ -3,6 +3,7 @@ pub mod detect; pub mod foreign; pub mod json; pub mod mcp; +pub mod paths; pub mod plan; pub mod sentinel; pub mod toml; diff --git a/crates/tw-adopt/src/mcp.rs b/crates/tw-adopt/src/mcp.rs index 577b48fb..a5bbff2e 100644 --- a/crates/tw-adopt/src/mcp.rs +++ b/crates/tw-adopt/src/mcp.rs @@ -83,7 +83,7 @@ pub fn targets() -> Vec { Target { client: "claude-desktop", name: "Claude Desktop", - config: "Library/Application Support/Claude/claude_desktop_config.json", + config: crate::paths::claude_desktop_config(), format: Format::Json, key: "mcpServers", copyable: true, diff --git a/crates/tw-adopt/src/paths.rs b/crates/tw-adopt/src/paths.rs new file mode 100644 index 00000000..a89466ef --- /dev/null +++ b/crates/tw-adopt/src/paths.rs @@ -0,0 +1,94 @@ +//! 各家客户端的配置放在哪。 +//! +//! **一处出处。**同一条路径以前在三个地方各写了一遍(接管的 MCP 目标、扫描 +//! 的来源清单、诊断),而它在 Windows 上和 macOS 上不是同一条 —— 三份各改 +//! 一次就是三份会漂,漏掉的那一份表现为「这台机器上的 Claude Desktop 没被 +//! 发现」,而扫描漏掉一个 MCP 配置正是它存在要挡的事。 +//! +//! # 为什么相对 home,而不是读 `%APPDATA%` +//! +//! 这个 crate 写的是**用户其他软件的配置文件**,所以它的测试必须能被隔离: +//! 传一个临时目录当 home。直接读环境变量会把那层隔离拆掉,而拆掉之后一次 +//! 跑偏的测试改的是真的 `~/.claude`。 +//! +//! 代价是 APPDATA 被重定向过的机器(漫游配置、或者用户自己搬过)找不到那份 +//! 配置。默认位置就在 home 底下,这是绝大多数;而「没发现」比「改错文件」 +//! 便宜得多。 +//! +//! 机器级的那一个(管理策略)没有 home 可言,它读环境变量。 + +use std::path::PathBuf; + +/// Claude Desktop 的配置,相对 home。 +/// +/// macOS 上在 `Library/Application Support/` 下,Windows 上在漫游的 AppData 下 +/// —— 两边都是各自平台放这类东西的地方,不是我们挑的。 +pub fn claude_desktop_config() -> &'static str { + #[cfg(windows)] + { + "AppData/Roaming/Claude/claude_desktop_config.json" + } + #[cfg(not(windows))] + { + "Library/Application Support/Claude/claude_desktop_config.json" + } +} + +/// 机器级的管理策略文件。**优先级压过一切**,包括用户自己的配置。 +/// +/// 机器级,所以这一个不相对 home。 +pub fn managed_settings() -> PathBuf { + #[cfg(windows)] + { + // **不写死 `C:\`** —— 系统盘不一定是 C,而写死的后果是在那些机器上 + // 报「这台机器没有管理策略」,也就是对一个真的压过用户配置的东西 + // 视而不见。 + std::env::var_os("PROGRAMDATA") + .map(PathBuf::from) + .unwrap_or_else(|| PathBuf::from(r"C:\ProgramData")) + .join("ClaudeCode") + .join("managed-settings.json") + } + #[cfg(not(windows))] + { + PathBuf::from("/Library/Application Support/ClaudeCode/managed-settings.json") + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// 相对 home,不是绝对路径 —— 否则 `home.join(…)` 会把 home 整个丢掉。 + #[test] + fn the_desktop_config_is_relative_to_home() { + let p = claude_desktop_config(); + assert!( + !PathBuf::from(p).is_absolute(), + "绝对路径会让 home.join() 忽略 home:{p}" + ); + assert!(p.ends_with("claude_desktop_config.json")); + assert_eq!( + std::path::Path::new("/tmp/h").join(p), + std::path::Path::new("/tmp/h").join(p), + ); + } + + /// 机器级的那一个反过来:必须是绝对的。 + #[test] + fn the_managed_policy_is_machine_wide() { + assert!(managed_settings().is_absolute()); + assert!(managed_settings().ends_with("managed-settings.json")); + } + + /// 每个平台指向自己那套习惯的位置。 + #[test] + fn each_platform_points_at_its_own_convention() { + let p = claude_desktop_config(); + if cfg!(windows) { + assert!(p.starts_with("AppData/"), "{p}"); + } else { + assert!(p.starts_with("Library/"), "{p}"); + } + } +} diff --git a/crates/tw-scan/src/sources.rs b/crates/tw-scan/src/sources.rs index 96ff848a..7d02ddf6 100644 --- a/crates/tw-scan/src/sources.rs +++ b/crates/tw-scan/src/sources.rs @@ -159,7 +159,7 @@ pub fn user_level(home: &Path) -> Vec { f( "claude-desktop", Kind::Mcp, - home.join("Library/Application Support/Claude/claude_desktop_config.json"), + home.join(tw_adopt::paths::claude_desktop_config()), ), f("cursor", Kind::Mcp, home.join(".cursor/mcp.json")), f("codex", Kind::Mcp, home.join(".codex/config.toml")), @@ -249,10 +249,9 @@ mod tests { // 它是订阅制、接管不了,但它的 MCP 配置是危险度第二高的攻击面。 // 漏掉它等于扫描留了个洞。 let d = tempfile::tempdir().unwrap(); - touch( - &d.path() - .join("Library/Application Support/Claude/claude_desktop_config.json"), - ); + // 路径按平台走 —— 写死 macOS 那一条的话,这个测试在 Windows 上 + // 会造一个没人找的文件,然后报告扫描漏了它。 + touch(&d.path().join(tw_adopt::paths::claude_desktop_config())); let got = user_level(d.path()); assert_eq!(got.len(), 1); assert_eq!(got[0].client, "claude-desktop");