Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(plugin): cmd shim 重入保护移到入口单点,探测 PATH 与安装同序

ruan.ljcdf6c9264f75
diff --git a/src-tauri/src/service/cli/shim/build.rs b/src-tauri/src/service/cli/shim/build.rs
index 2143b675..49ed4da4 100644
--- a/src-tauri/src/service/cli/shim/build.rs
+++ b/src-tauri/src/service/cli/shim/build.rs
@@ -182,6 +182,16 @@ if "%DSH_PREFER_BUNDLED_PNPM%"=="1" (
if exist "%PNPM_BIN%" goto :after_user
)
+rem Re-entry guard: DSH_PNPM, or the first pnpm found on PATH, may itself be a shim
+rem that resolves back through PATH into this file; forwarding twice would exec the
+rem two shims into each other forever and the installer child would never exit.
+rem Armed once here, before either forward, and checked on entry: a re-entered shim
+rem goes straight to the bundled pnpm. The forwarding blocks below therefore stay
+rem byte-identical to the issue #130 fix, whose exit-code propagation depends on
+rem `exit /b %ERRORLEVEL%` sitting directly after `call` outside any block.
+if "%DSH_PNPM_SHIM_GUARD%"=="1" goto :after_user
+set "DSH_PNPM_SHIM_GUARD=1"
+
rem Use the exact user pnpm discovered by the desktop app.
if defined DSH_PNPM (
if exist "%DSH_PNPM%" goto :use_selected
@@ -189,7 +199,6 @@ if defined DSH_PNPM (
rem Prefer a user-installed pnpm (skip our own shim dir), fall back to bundled.
rem Accept only executable extensions (.cmd/.exe/.bat), ignore extensionless shell scripts.
-:user_search
set "SELF_PREFIX=%~dp0"
set "SELF_PREFIX=%SELF_PREFIX:~0,-1%"
set "USER_PNPM="
@@ -211,18 +220,10 @@ if defined USER_PNPM goto :use_user
goto :after_user
:use_selected
-rem Re-entry guard: DSH_PNPM may itself be a shim that resolves back through PATH
-rem into this file; forwarding twice would exec the two shims into each other
-rem forever and the installer child would never exit. Once forwarded, fall back
-rem to the PATH search and then to the bundled pnpm.
-if "%DSH_PNPM_SHIM_GUARD%"=="1" goto :user_search
-set "DSH_PNPM_SHIM_GUARD=1"
call "%DSH_PNPM%" %*
exit /b %ERRORLEVEL%
:use_user
-if "%DSH_PNPM_SHIM_GUARD%"=="1" goto :after_user
-set "DSH_PNPM_SHIM_GUARD=1"
call "%USER_PNPM%" %*
exit /b %ERRORLEVEL%
@@ -778,10 +779,25 @@ mod tests {
"{name}: missing re-entry guard"
);
}
- // cmd 的转发目标改用标签回跳,两个 `:use_*` 都必须受保护(各含一次判断 + 一次置位)。
+ // cmd 在入口一次判断 + 一次置位;转发块保持 issue #130 修复时的原样。
let cmd = build_pnpm_cmd_shim(&app_dir);
- assert_eq!(cmd.matches("\n:user_search").count(), 1);
- assert_eq!(cmd.matches("DSH_PNPM_SHIM_GUARD").count(), 4);
+ assert_eq!(cmd.matches("DSH_PNPM_SHIM_GUARD").count(), 2);
+ assert_eq!(cmd.matches("set \"DSH_PNPM_SHIM_GUARD=1\"").count(), 1);
+ }
+
+ /// 回归 issue #130:用户 pnpm 的退出码必须原样透出。`exit /b %ERRORLEVEL%`
+ /// 一旦落进括号块,或与 `call` 之间插进别的语句,`%ERRORLEVEL%` 就会在块解析
+ /// 时提前展开,失败码被吞成 0/1。这里锁死「call 之后紧跟 exit /b %ERRORLEVEL%」。
+ #[test]
+ fn cmd_shim_exit_code_stays_adjacent_to_call() {
+ let content = build_pnpm_cmd_shim(&sample_app_dir());
+ for call in [r#"call "%DSH_PNPM%" %*"#, r#"call "%USER_PNPM%" %*"#] {
+ let expected = format!("{call}\r\nexit /b %ERRORLEVEL%\r\n");
+ assert!(
+ content.contains(&expected),
+ "`{call}` must be followed directly by `exit /b %ERRORLEVEL%`"
+ );
+ }
}
/// 回归:`DSH_PNPM` 指向的 shim 若按 PATH 解析回本 shim(mise shims 的真实
diff --git a/src-tauri/src/service/plugin/install/pnpm.rs b/src-tauri/src/service/plugin/install/pnpm.rs
index aef4e38b..d5cc9c19 100644
--- a/src-tauri/src/service/plugin/install/pnpm.rs
+++ b/src-tauri/src/service/plugin/install/pnpm.rs
@@ -623,26 +623,29 @@ fn parse_pnpm_major_output(pnpm: &Path, output: &std::process::Output) -> Option
stdout.split('.').next()?.trim().parse::<u32>().ok()
}
-/// 构建 pnpm 探测专用 PATH:选定 Node 目录、桌面端 shim 目录、用户 pnpm 所在
-/// 目录依次前置,其余环境保留。
+/// 构建 pnpm 探测专用 PATH:**与 `build_plugin_envs` 同序**——桌面端 shim 目录
+/// 首位,随后选定 Node 目录、用户 pnpm 所在目录,其余环境保留。
///
-/// `bin_dir` 必须参与:`build_plugin_envs` 把它放在子进程 PATH 首位,`dsh plugin`
-/// 的 `spawnSync("pnpm")` 命中的就是它。探测少了这一层,就会在一个「自身没有实现、
-/// 只按 PATH 转交」的用户 shim(如 mise shims)上得出与真实安装相反的结论。
+/// 顺序必须一致:安装子进程的 PATH 就是 `[bin_dir, node_dir, ...]`,`dsh plugin`
+/// 的 `spawnSync("pnpm")` 命中的是 `bin_dir/pnpm`,而该 shim 只会按 `DSH_PNPM`
+/// 或 PATH 继续转发。探测若把 `bin_dir` 排到用户 pnpm 之后,就可能解析出与真实
+/// 安装不同的 pnpm(Corepack / 转发型 shim 尤其明显),使选出的主版本与实际安装
+/// 的不一致;反过来,少了 `bin_dir` 这一层就会漏掉「按 PATH 解析回桌面 shim」的
+/// 间接层,得出与安装相反的结论。
fn pnpm_probe_path(
pnpm: &Path,
node: Option<&Path>,
bin_dir: Option<&Path>,
) -> Option<OsString> {
let mut paths = Vec::new();
- // 选定 Node 必须位于 pnpm shim 目录之前:corepack 目录可能残留另一份 node,
+ if let Some(bin_dir) = bin_dir {
+ paths.push(bin_dir.to_path_buf());
+ }
+ // 选定 Node 仍须位于 pnpm shim 目录之前:corepack 目录可能残留另一份 node,
// bare `node` 应与桌面端预检和后续插件命令使用同一运行时。
if let Some(parent) = node.and_then(Path::parent) {
paths.push(parent.to_path_buf());
}
- if let Some(bin_dir) = bin_dir {
- paths.push(bin_dir.to_path_buf());
- }
if let Some(parent) = pnpm.parent() {
paths.push(parent.to_path_buf());
}
@@ -1153,4 +1156,39 @@ virtualStoreDir: node_modules/.pnpm
assert_eq!(strip_store_version("v10"), "v10");
assert_eq!(strip_store_version(""), "");
}
+
+ /// 探测 PATH 必须与 `build_plugin_envs`(安装子进程)同序:桌面端 shim 目录在
+ /// 首位,然后才是选定 Node 与用户 pnpm 所在目录。顺序不一致时探测与安装可能
+ /// 解析到不同的 pnpm,`ensure_pnpm` 据探测结果选出的主版本与实际安装的不相符。
+ #[test]
+ fn probe_path_puts_desktop_shim_dir_first() {
+ let path = pnpm_probe_path(
+ Path::new("/user/bin/pnpm"),
+ Some(Path::new("/opt/node/bin/node")),
+ Some(Path::new("/desktop/bin")),
+ )
+ .unwrap();
+ let entries: Vec<String> = std::env::split_paths(&path)
+ .map(|entry| entry.to_string_lossy().into_owned())
+ .collect();
+ assert_eq!(entries[0], "/desktop/bin");
+ assert_eq!(entries[1], "/opt/node/bin");
+ assert_eq!(entries[2], "/user/bin");
+ }
+
+ /// 不注入桌面 shim 目录(按精确路径探测)时保持原有顺序,不凭空插入空目录。
+ #[test]
+ fn probe_path_without_bin_dir_keeps_node_first() {
+ let path = pnpm_probe_path(
+ Path::new("/user/bin/pnpm"),
+ Some(Path::new("/opt/node/bin/node")),
+ None,
+ )
+ .unwrap();
+ let entries: Vec<String> = std::env::split_paths(&path)
+ .map(|entry| entry.to_string_lossy().into_owned())
+ .collect();
+ assert_eq!(entries[0], "/opt/node/bin");
+ assert_eq!(entries[1], "/user/bin");
+ }
}