Skip to content

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

fix(plugin): version-aware pnpm selection for preinstall, log actual specs (issue #34)

hairyf6d3f8b61566c
diff --git a/src-tauri/src/service/cli/shim.rs b/src-tauri/src/service/cli/shim.rs
index fce88787..dc9e9620 100644
--- a/src-tauri/src/service/cli/shim.rs
+++ b/src-tauri/src/service/cli/shim.rs
@@ -298,10 +298,15 @@ exec "$NODE" "$DSH_BIN" "$@"
// ---------------------------------------------------------------------------
// pnpm shim(额外带"用户 pnpm 优先"逻辑,见各函数注释)
+//
+// `DSH_PREFER_BUNDLED_PNPM=1`(应用内部插件安装注入,见 service/plugin/install.rs)
+// 时改为捆绑版优先:跳过用户 pnpm 直接运行捆绑 pnpm.cjs,仅当捆绑缺失时回退
+// 用户 pnpm。默认(未设置)行为不变:用户 pnpm 优先。
// ---------------------------------------------------------------------------
/// Windows `pnpm.cmd` 内容:优先转发用户自己安装的 pnpm(`where pnpm` 遍历、
/// 跳过本 shim 目录、只收 `.cmd/.exe/.bat`),否则用 node 运行捆绑 pnpm.cjs。
+/// `DSH_PREFER_BUNDLED_PNPM=1` 时捆绑版优先(见模块头注)。
///
/// 实现要点:
/// - 不用 `findstr` 匹配路径(`\` 会被当正则转义导致过滤失效);
@@ -317,6 +322,12 @@ setlocal
set "APP_DIR={app_dir}"
set "PNPM_BIN={pnpm_bin}"
+rem App-internal installs (DSH_PREFER_BUNDLED_PNPM=1) use the bundled pnpm,
+rem falling back to the user's only when the bundled one is missing.
+if "%DSH_PREFER_BUNDLED_PNPM%"=="1" (
+ if exist "%PNPM_BIN%" goto :after_user
+)
+
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.
set "SELF_PREFIX=%~dp0"
@@ -339,6 +350,8 @@ if defined USER_PNPM (
call "%USER_PNPM%" %*
exit /b %ERRORLEVEL%
)
+
+:after_user
{node_resolve}
:launch
if not exist "%PNPM_BIN%" goto :no_pnpm
@@ -361,6 +374,7 @@ exit /b 1
/// Windows `pnpm.ps1` 内容:优先转发用户 pnpm(`Get-Command pnpm -All`,
/// 排除本 shim 目录),否则用 node 运行捆绑 pnpm.cjs。
+/// `DSH_PREFER_BUNDLED_PNPM=1` 时捆绑版优先(见模块头注)。
pub fn build_pnpm_ps1_shim(app_dir: &Path) -> String {
let pnpm_bin = app_dir.join("dependencies/pnpm/bin/pnpm.cjs");
@@ -371,6 +385,15 @@ $ErrorActionPreference = "Stop"
$appDir = '{app_dir}'
$pnpmBin = '{pnpm_bin}'
+{node_resolve}
+
+# App-internal installs (DSH_PREFER_BUNDLED_PNPM=1) use the bundled pnpm,
+# falling back to the user's only when the bundled one is missing.
+if ($env:DSH_PREFER_BUNDLED_PNPM -eq '1' -and (Test-Path -LiteralPath $pnpmBin)) {{
+ & $node $pnpmBin @args
+ exit $LASTEXITCODE
+}}
+
# Prefer a user-installed pnpm (skip our own shim dir), fall back to bundled.
$selfDir = $PSScriptRoot.TrimEnd('\') + '\'
$userPnpm = Get-Command pnpm -All -ErrorAction SilentlyContinue |
@@ -380,7 +403,7 @@ if ($userPnpm) {{
& $userPnpm.Source @args
exit $LASTEXITCODE
}}
-{node_resolve}
+
if (-not (Test-Path -LiteralPath $pnpmBin)) {{
Write-Error '[pnpm] pnpm not found. Please run DeepSeek Harness Desktop to install it first.'
exit 1
@@ -395,7 +418,8 @@ exit $LASTEXITCODE
}
/// Unix `pnpm` shell 脚本内容(POSIX sh):按 PATH 顺序转发第一个非本目录
-/// 的用户 pnpm,否则用 node 运行捆绑 pnpm.cjs。
+/// 的用户 pnpm,否则用 node 运行捆绑 pnpm.cjs。`DSH_PREFER_BUNDLED_PNPM=1`
+/// 时捆绑版优先(见模块头注)。
#[cfg_attr(all(windows, not(test)), allow(dead_code))]
pub fn build_pnpm_sh_shim(app_dir: &Path) -> String {
let pnpm_bin = app_dir.join("dependencies/pnpm/bin/pnpm.cjs");
@@ -406,6 +430,13 @@ pub fn build_pnpm_sh_shim(app_dir: &Path) -> String {
# Do not edit: regenerated by the desktop app on install/startup.
APP_DIR='{app_dir}'
PNPM_BIN='{pnpm_bin}'
+{node_resolve}
+
+# App-internal installs (DSH_PREFER_BUNDLED_PNPM=1) use the bundled pnpm,
+# falling back to the user's only when the bundled one is missing.
+if [ "$DSH_PREFER_BUNDLED_PNPM" = "1" ] && [ -f "$PNPM_BIN" ]; then
+ exec "$NODE" "$PNPM_BIN" "$@"
+fi
# Prefer a user-installed pnpm (skip our own shim dir), fall back to bundled.
SELF_DIR=$(cd "$(dirname "$0")" && pwd)
@@ -419,7 +450,7 @@ for dir in $PATH; do
fi
done
unset IFS
-{node_resolve}
+
if [ ! -f "$PNPM_BIN" ]; then
echo "[pnpm] pnpm not found. Please run DeepSeek Harness Desktop to install it first." >&2
exit 1
@@ -554,6 +585,11 @@ mod tests {
assert!(content.contains(r#"call "%USER_PNPM%" %*"#));
assert!(content.contains(":use_bundled"));
assert!(content.contains("%APP_DIR%\\runtime\\node.exe"));
+ // 应用内部安装可经 DSH_PREFER_BUNDLED_PNPM=1 强制捆绑版(须在用户搜索前生效)
+ assert!(content.contains("DSH_PREFER_BUNDLED_PNPM"));
+ let env_at = content.find("DSH_PREFER_BUNDLED_PNPM").unwrap();
+ let user_at = content.find("where pnpm").unwrap();
+ assert!(env_at < user_at);
}
#[test]
@@ -564,6 +600,7 @@ mod tests {
assert!(content.contains("$userPnpm.Source"));
assert!(content.contains("@args"));
assert!(content.contains("Join-Path $appDir 'runtime\\node.exe'"));
+ assert!(content.contains("$env:DSH_PREFER_BUNDLED_PNPM"));
}
#[test]
@@ -574,6 +611,7 @@ mod tests {
assert!(content.contains("SELF_DIR"));
assert!(content.contains(r#"exec "$NODE" "$PNPM_BIN" "$@""#));
assert!(content.contains(r#"$APP_DIR/runtime/bin/node"#));
+ assert!(content.contains("DSH_PREFER_BUNDLED_PNPM"));
}
#[test]
diff --git a/src-tauri/src/service/plugin/install.rs b/src-tauri/src/service/plugin/install.rs
index b79cbd61..37c36aff 100644
--- a/src-tauri/src/service/plugin/install.rs
+++ b/src-tauri/src/service/plugin/install.rs
@@ -28,6 +28,14 @@ use super::process::{run_plugin_process, PreinstallLogPayload, PREINSTALL_LOG_EV
/// 传递构建包名),多个 git 插件 / 多个原生依赖各占一次,上限封顶防死循环。
const MAX_ALLOW_LIST_RETRIES: usize = 8;
+/// 可安全用于插件安装的用户 pnpm 最低主版本。
+///
+/// pnpm 10+ 才从 `pnpm-workspace.yaml` 读取 `autoInstallPeers`(9 及更早只读
+/// `.npmrc`),且 10+ 移除了 workspace-root 安装门槛(`ERR_PNPM_ADDING_TO_ROOT`
+/// 是 8/9 行为)。低于此版本时插件安装必须改用捆绑版 pnpm,否则会出现
+/// 自动合成 peer 后 `No matching version found for @deepseek-ai/...` 的假失败。
+const MIN_TRUSTED_PNPM_MAJOR: u32 = 10;
+
/// 校验并安装选中的预装插件:`dsh plugin --profile web add <ids...>`
pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), String> {
if ids.is_empty() {
@@ -65,8 +73,8 @@ pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), Strin
.get_webview_window("main")
.ok_or("WINDOW_NOT_FOUND: main window missing")?;
- // 按需补齐捆绑 pnpm
- ensure_pnpm(app_handle, &window).await?;
+ // 选定/补齐安装用的 pnpm:返回是否应强制使用捆绑版(版本感知,见 ensure_pnpm)
+ let prefer_bundled_pnpm = ensure_pnpm(app_handle, &window).await?;
// 安装前停止运行中的服务,避免资源冲突
if workflow::has_owned_process() {
@@ -88,6 +96,14 @@ pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), Strin
("DSH_TELEMETRY_DISABLED".to_string(), "1".to_string()),
("NO_COLOR".to_string(), "1".to_string()),
]);
+ // 用户 pnpm 过旧/不可探测时强制 pnpm shim 优先捆绑版,避免 8/9 的
+ // autoInstallPeers 语义与 workspace-root gate 破坏插件安装(见 ensure_pnpm)
+ if prefer_bundled_pnpm {
+ envs.insert(
+ "DSH_PREFER_BUNDLED_PNPM".to_string(),
+ "1".to_string(),
+ );
+ }
let mut paths = vec![bin_dir];
if let Some(node_dir) = node.parent() {
@@ -109,10 +125,11 @@ pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), Strin
OsString::from(PREINSTALL_PROFILE),
OsString::from("add"),
];
- args.extend(specs.into_iter().map(OsString::from));
+ args.extend(specs.iter().map(|s| OsString::from(s.as_str())));
let cwd = config::get_dsh_install_path(app_handle);
- log::info!("Running dsh plugin install for {ids:?}");
+ // 日志打印实际传给 dsh 的 spec(此前打印 id 会误导排查:安装用的是 spec)
+ log::info!("Running dsh plugin install for {specs:?}");
// `dsh plugin add` 在 profile 目录里驱动 pnpm。pnpm v11 会拦下 git 托管
// 插件的 prepare 构建与传递原生依赖(见模块头注),其允许键不可预知,因此
@@ -163,10 +180,33 @@ pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), Strin
Ok(())
}
-/// 确保捆绑 pnpm 已安装
-async fn ensure_pnpm(app_handle: &AppHandle, window: &WebviewWindow) -> Result<(), String> {
- if download::Pnpm.check_installed(app_handle) {
- return Ok(());
+/// 确保插件安装使用的 pnpm 可用,返回是否应强制使用捆绑版
+/// (true 时调用方注入 `DSH_PREFER_BUNDLED_PNPM=1`,pnpm shim 优先捆绑版)。
+///
+/// 版本感知策略,避免给已装正确 pnpm 的用户增加下载步骤:
+/// - 捆绑版已存在 → 直接用捆绑版(零额外下载,确定性最强);
+/// - 用户 pnpm 主版本 ≥ MIN_TRUSTED_PNPM_MAJOR → 复用用户 pnpm,零额外步骤;
+/// - 用户 pnpm 过旧(8/9:不读 pnpm-workspace.yaml 的 autoInstallPeers、有
+/// workspace-root gate;corepack shim 在 Node 24 上还会 ERR_INVALID_THIS 崩溃)
+/// 或版本不可探测 → 下载捆绑版并强制使用。
+async fn ensure_pnpm(app_handle: &AppHandle, window: &WebviewWindow) -> Result<bool, String> {
+ if config::get_pnpm_binary_path(app_handle).exists() {
+ return Ok(true);
+ }
+
+ match user_pnpm_major_version(app_handle) {
+ Some(major) if major >= MIN_TRUSTED_PNPM_MAJOR => {
+ log::info!("Reusing user-installed pnpm (major {major}) for plugin install");
+ return Ok(false);
+ }
+ Some(major) => {
+ log::warn!(
+ "User pnpm major {major} < {MIN_TRUSTED_PNPM_MAJOR} (missing autoInstallPeers/workspace-root semantics), downloading bundled pnpm"
+ );
+ }
+ None => {
+ log::warn!("User pnpm version not detectable (broken/blocked shim?), downloading bundled pnpm");
+ }
}
let _ = window.emit(
@@ -196,7 +236,22 @@ async fn ensure_pnpm(app_handle: &AppHandle, window: &WebviewWindow) -> Result<(
line: "[pnpm] bundled pnpm ready".to_string(),
},
);
- Ok(())
+ Ok(true)
+}
+
+/// 用户 pnpm 主版本号(解析 `pnpm --version` 首个点分字段);不存在或不可运行
+/// (corepack shim 在 Node 24 上 ERR_INVALID_THIS 崩溃等)返回 None。
+fn user_pnpm_major_version(app_handle: &AppHandle) -> Option<u32> {
+ let pnpm = cli::find_user_pnpm(app_handle)?;
+ let output = std::process::Command::new(&pnpm)
+ .arg("--version")
+ .output()
+ .ok()?;
+ if !output.status.success() {
+ return None;
+ }
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ stdout.split('.').next()?.trim().parse::<u32>().ok()
}
/// 从 pnpm 失败输出中解析需写入 `allowBuilds` 的键集合: