Skip to content

fix(detection): resolve Windows custom npm prefix tools via PATH - #4267

Closed
Ttungx wants to merge 1 commit into
farion1231:mainfrom
Ttungx:fix/windows-npm-prefix-detection
Closed

Ttungx wants to merge 1 commit into
farion1231:mainfrom
Ttungx:fix/windows-npm-prefix-detection

Conversation

@Ttungx

@Ttungx Ttungx commented Jun 15, 2026 •

Copy link
Copy Markdown

Summary / 概述

修复 Windows 上「改了 npm prefix 后,npm i -g 安装的 CLI 工具(codex / opencode)在 设置 → 关于 显示为『未安装』,而默认 prefix 的 Claude Code 检测正常」的问题。

根因

get_single_tool_version_impl(src-tauri/src/commands/misc.rs)的 else 分支存在平台不对称:

  • 非 Windows:先用 try_get_version 通过登录 shell($SHELL -lic "tool --version")查询,会加载 .zshrc/.bashrc、获得完整用户 PATH;只有 NotFound 才 fallback 到 scan_cli_version。
  • Windows(修复前):直接跳过 PATH 解析,只走 scan_cli_version。而 scan_cli_version 用的是进程级 std::env::var_os("PATH")(GUI/Tauri 进程的 PATH)+ 文件存在性检查。当自定义 npm prefix 目录仅由 shell profile / 系统环境注入、未进入 GUI 进程 PATH 时,就会漏检。

已有的修复 PR #2286(pnpm 全局安装检测)、PR #2822(mise 路径)只对非 Windows 生效,Windows 这条路径从未被修过。

修复

新增 try_get_version_windows,与非 Windows 的 try_get_version 结构对称:

  1. 用 resolve_path_default(cmd /C where,shell 侧标准 PATH 解析)定位命令行默认命中的真实可执行文件;
  2. 只对该真身跑 --version(复用既有 run_windows_tool_version_command,仍不直接 cmd /C {tool},避免误触发 App Execution Alias / 协议处理器);
  3. where 找不到时返回 NotFound,由调用方 fallback 到 scan_cli_version 的常见目录扫描。

调用点(misc.rs Windows 分支)改为与非 Windows 一致的 match … NotFound => scan_cli_version, found => found 形态。复用既有且经测试的 resolve_path_default / run_windows_tool_version_command / extract_version,未引入重复实现。

Related Issue / 关联 Issue

Fixes #4265

Screenshots / 截图

N/A —— 纯后端检测逻辑变更,无 UI 文案改动。

How verified / 验证方式

  • cargo fmt --check ✅ 干净
  • cargo clippy ✅ 无新增告警(仅有与本次改动无关的既有告警)
  • cargo test commands::misc::tests ✅ 无新增失败:53 通过;6 个失败全在 anchored_upgrade_windows::*(升级命令生成,非检测路径),在干净的 main 上同样复现,属既有问题、与本次改动无关。

手动验证场景(对应 issue):

  1. 默认 npm prefix(%APPDATA%\npm)—— 所有工具检测正常(回归,走 scan_cli_version fallback)。
  2. 自定义 npm prefix + npm i -g @openai/codex / opencode-ai —— 现在可检测到(新增 where 路径)。
  3. where 找不到工具 —— 仍 fallback 到 scan_cli_version(兜底保留)。

Checklist / 检查清单

  • pnpm typecheck passes / 通过 TypeScript 类型检查(无前端改动)
  • pnpm format:check passes / 通过代码格式检查(无前端改动)
  • cargo clippy passes (if Rust code changed) / 通过 Clippy 检查(修改了 Rust 代码)
  • Updated i18n files if user-facing text changed / 如修改了用户可见文本,已更新国际化文件(无用户可见文本改动,N/A)

@farion1231

Copy link
Copy Markdown
Owner

@codex review

@Ttungx

Ttungx commented Jun 15, 2026

Copy link
Copy Markdown
Author

撤回此 PR。经在复现环境中排查,issue #4265 的真实根因与 PR 描述的「Windows PATH 检测缺陷」不符——在我的环境下 npm-global 自始至终在注册表 PATH 中,后端 scan_cli_version 能正常检测到工具。实际漏检发生在「点击更新之后」,疑为更新窗口期二进制短暂不可用 + 前端未自动重新探测所致,与本 PR 的后端探测改动无关。抱歉带来困扰,issue 根因我另行更正。

@Ttungx Ttungx closed this Jun 15, 2026
@Ttungx
Ttungx deleted the fix/windows-npm-prefix-detection branch June 15, 2026 13:34

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1be028337b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

/// 漏掉。`where` 走的是 shell 侧的标准 PATH 解析,能覆盖自定义 npm prefix 等场景。
#[cfg(target_os = "windows")]
fn try_get_version_windows(tool: &str) -> ShellProbe {
let tool_path = match resolve_path_default(tool) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a refreshed PATH source before relying on where

In the Windows custom-prefix case described by this change, this still launches cmd /C where with the Tauri process environment. Microsoft documents where as searching the current directory and PATH (https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/where), and the fallback scanner also extends from the same std::env::var_os("PATH"); when the npm prefix only exists in the user's shell/profile or in a newer environment block that the GUI process has not inherited, where returns nothing and the fallback repeats the same stale PATH scan. That leaves codex/opencode reported as not installed for the scenario this patch is intended to fix, so the Windows probe needs to refresh/augment PATH from a source other than the current process before resolving the tool.

Useful? React with 👍 / 👎.

.map(|value| value.to_string_lossy().into_owned())
.unwrap_or_default();

match run_windows_tool_version_command(&tool_path, &new_path) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Filter where results before executing them

When where returns a match from WindowsApps or the current directory before the real npm shim, this branch executes that path immediately. The existing scanner intentionally filters PATH entries through should_skip_cli_path_env_dir, but this new path bypasses that guard; if an App Execution Alias/protocol stub exits non-zero, try_get_version_windows returns FoundButFailed and the caller will not fall back to scan_cli_version, so a valid later installation is hidden behind the alias. Reuse the same skip logic or iterate to the next where result before running --version.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: 自定义 npm prefix 后 codex/opencode 检测不到(PR #2286 未覆盖 Windows)

2 participants