Skip to content

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

fix(cli): preserve user-installed dsh in shims

hairyf01efd4e376e8
diff --git a/src-tauri/src/service/cli/core.rs b/src-tauri/src/service/cli/core.rs
index acab5078..a14f6084 100644
--- a/src-tauri/src/service/cli/core.rs
+++ b/src-tauri/src/service/cli/core.rs
@@ -6,7 +6,7 @@ use std::fs;
use tauri::AppHandle;
use super::path::{get_bin_dir, get_shim_path, path_registered, register_path, unregister_path};
-use super::shim::write_shims;
+use super::shim::{user_dsh_preserved, write_shims};
/// 命令行集成状态(设置页展示)
#[derive(Debug, Clone, Serialize)]
@@ -17,6 +17,8 @@ pub struct CliLinkStatus {
pub shim_exists: bool,
/// bin 目录是否已在用户 PATH 中注册
pub path_registered: bool,
+ /// 检测到用户自行安装的同名 `dsh`(未被覆盖,已保留)
+ pub user_dsh_preserved: bool,
/// bin 目录绝对路径
pub bin_dir: String,
/// 主 shim 文件绝对路径
@@ -27,11 +29,14 @@ pub struct CliLinkStatus {
pub fn get_status(app_handle: &AppHandle) -> CliLinkStatus {
let setting = config::get_store_dat_setting(app_handle);
let shim_path = get_shim_path(app_handle);
+ let bin_dir = get_bin_dir(app_handle);
CliLinkStatus {
enabled: setting.cli_link_enabled,
shim_exists: shim_path.is_file(),
path_registered: path_registered(app_handle),
- bin_dir: get_bin_dir(app_handle).to_string_lossy().into_owned(),
+ // 安装集成时若在 shim 路径检测到用户自装的同名 dsh,则已被保留、未被覆盖
+ user_dsh_preserved: user_dsh_preserved(&bin_dir),
+ bin_dir: bin_dir.to_string_lossy().into_owned(),
shim_path: shim_path.to_string_lossy().into_owned(),
}
}
diff --git a/src-tauri/src/service/cli/shim.rs b/src-tauri/src/service/cli/shim.rs
index 1c482fc1..fce88787 100644
--- a/src-tauri/src/service/cli/shim.rs
+++ b/src-tauri/src/service/cli/shim.rs
@@ -106,6 +106,72 @@ if [ -z "$NODE" ]; then
fi
"#;
+// ---------------------------------------------------------------------------
+// dsh shim 共享片段:用户已安装的 dsh 优先(避免覆盖/遮蔽用户自己的 dsh 与
+// $DSH_HOME)。与 pnpm shim 的"用户优先"策略一致:先转发 PATH 中(排除本
+// shim 目录)的用户 dsh,转发时不注入本应用的 DSH_HOME,保留用户环境;
+// 仅找不到用户 dsh 时才回退到捆绑 dsh。
+// 变量约定:cmd 用 %SELF_PREFIX%/%USER_DSH%,ps1 用 $selfDir/$userDsh,
+// sh 用 $SELF_DIR。
+// ---------------------------------------------------------------------------
+
+const CMD_USER_DSH_PRECEDENCE: &str = r#"
+rem Prefer a user-installed dsh on PATH (skip our own shim dir), fall back to bundled.
+rem This preserves your own dsh binary and its $DSH_HOME config; nothing is overwritten.
+set "SELF_PREFIX=%~dp0"
+set "SELF_PREFIX=%SELF_PREFIX:~0,-1%"
+set "USER_DSH="
+for /f "delims=" %%d in ('where dsh 2^>nul') do (
+ if not defined USER_DSH (
+ if /i not "%%d"=="%SELF_PREFIX%\dsh.cmd" (
+ if /i not "%%d"=="%SELF_PREFIX%\dsh.ps1" (
+ if /i not "%%d"=="%SELF_PREFIX%\dsh.exe" (
+ if /i not "%%d"=="%SELF_PREFIX%\dsh.bat" (
+ if /i "%%~xd"==".cmd" set "USER_DSH=%%d"
+ if /i "%%~xd"==".exe" set "USER_DSH=%%d"
+ if /i "%%~xd"==".bat" set "USER_DSH=%%d"
+ )
+ )
+ )
+ )
+ )
+)
+if defined USER_DSH (
+ call "%USER_DSH%" %*
+ exit /b %ERRORLEVEL%
+)
+"#;
+
+const PS1_USER_DSH_PRECEDENCE: &str = r#"
+# Prefer a user-installed dsh on PATH (skip our own shim dir), fall back to bundled.
+# This preserves your own dsh binary and its $env:DSH_HOME config; nothing is overwritten.
+$selfDir = $PSScriptRoot.TrimEnd('\') + '\'
+$userDsh = Get-Command dsh -All -ErrorAction SilentlyContinue |
+ Where-Object { $_.Source -and -not $_.Source.StartsWith($selfDir, [System.StringComparison]::OrdinalIgnoreCase) } |
+ Select-Object -First 1
+if ($userDsh) {
+ & $userDsh.Source @args
+ exit $LASTEXITCODE
+}
+"#;
+
+#[cfg_attr(windows, allow(dead_code))] // 仅 Unix shim 使用
+const SH_USER_DSH_PRECEDENCE: &str = r#"
+# Prefer a user-installed dsh on PATH (skip our own shim dir), fall back to bundled.
+# This preserves your own dsh binary and its $DSH_HOME config; nothing is overwritten.
+SELF_DIR=$(cd "$(dirname "$0")" && pwd)
+IFS=:
+for dir in $PATH; do
+ if [ "$dir" = "$SELF_DIR" ]; then
+ continue
+ fi
+ if [ -x "$dir/dsh" ]; then
+ exec "$dir/dsh" "$@"
+ fi
+done
+unset IFS
+"#;
+
// ---------------------------------------------------------------------------
// 路径转义(按目标脚本语言的字符串规则)
// ---------------------------------------------------------------------------
@@ -143,6 +209,7 @@ rem DeepSeek Harness Desktop - dsh command shim (generated)
rem Do not edit: regenerated by the desktop app on install/startup.
setlocal
set "APP_DIR={app_dir}"
+{user_dsh}
set "DSH_BIN={dsh_bin}"
set "DSH_HOME={dsh_home}"
set "DSH_TELEMETRY_DISABLED=1"
@@ -163,6 +230,7 @@ exit /b 1
app_dir = escape_path_cmd(app_dir),
dsh_bin = escape_path_cmd(&dsh_bin),
dsh_home = escape_path_cmd(&dsh_home),
+ user_dsh = CMD_USER_DSH_PRECEDENCE,
node_resolve = CMD_NODE_RESOLVE,
)
}
@@ -178,20 +246,23 @@ pub fn build_ps1_shim(app_dir: &Path) -> String {
$ErrorActionPreference = "Stop"
$appDir = '{app_dir}'
$dshBin = '{dsh_bin}'
-$env:DSH_HOME = '{dsh_home}'
+{user_dsh}
+$dshHome = '{dsh_home}'
$env:DSH_TELEMETRY_DISABLED = '1'
{node_resolve}
if (-not (Test-Path -LiteralPath $dshBin)) {{
Write-Error '[dsh] Harness CLI not found. Please run DeepSeek Harness Desktop to install it first.'
exit 1
}}
+$env:DSH_HOME = $dshHome
& $node $dshBin @args
exit $LASTEXITCODE
"#,
app_dir = escape_path_ps1(app_dir),
dsh_bin = escape_path_ps1(&dsh_bin),
- dsh_home = escape_path_ps1(&dsh_home),
+ user_dsh = PS1_USER_DSH_PRECEDENCE,
node_resolve = PS1_NODE_RESOLVE,
+ dsh_home = escape_path_ps1(&dsh_home),
)
}
@@ -207,6 +278,7 @@ pub fn build_sh_shim(app_dir: &Path) -> String {
# Do not edit: regenerated by the desktop app on install/startup.
APP_DIR='{app_dir}'
DSH_BIN='{dsh_bin}'
+{user_dsh}
export DSH_HOME='{dsh_home}'
export DSH_TELEMETRY_DISABLED=1
{node_resolve}
@@ -218,6 +290,7 @@ exec "$NODE" "$DSH_BIN" "$@"
"#,
app_dir = escape_path_sh(app_dir),
dsh_bin = escape_path_sh(&dsh_bin),
+ user_dsh = SH_USER_DSH_PRECEDENCE,
dsh_home = escape_path_sh(&dsh_home),
node_resolve = SH_NODE_RESOLVE,
)
@@ -363,36 +436,77 @@ exec "$NODE" "$PNPM_BIN" "$@"
// 落盘
// ---------------------------------------------------------------------------
-/// 将 shim 文件写入 bin 目录(覆盖式,保证自愈后内容与当前安装一致)
+/// 生成的 shim 自带的可识别标记(首行注释)。用于区分"本应用生成的 shim"
+/// 与"用户自行放置的同名文件"。读文件只读该标记行,避免误删用户自有文件。
+const GENERATED_MARKER: &str = "DeepSeek Harness Desktop - ";
+
+/// 目标路径已存在且不是本应用生成的 shim(即用户手动放置的 `dsh`/`pnpm`)。
+///
+/// 此时绝不覆盖,保留用户文件,避免"安装后清空了之前手动安装的工具"。
+fn is_foreign_file(path: &Path) -> bool {
+ match std::fs::read_to_string(path) {
+ Ok(content) => !content.contains(GENERATED_MARKER),
+ Err(_) => true,
+ }
+}
+
+/// 主 `dsh` shim 路径下是否保留了用户自行安装的同名文件(用于状态展示)。
+pub fn user_dsh_preserved(bin_dir: &Path) -> bool {
+ let path = {
+ #[cfg(windows)]
+ {
+ bin_dir.join(SHIM_CMD_NAME)
+ }
+ #[cfg(not(windows))]
+ {
+ bin_dir.join(SHIM_SH_NAME)
+ }
+ };
+ path.is_file() && is_foreign_file(&path)
+}
+
+/// 将 shim 文件写入 bin 目录;目标已存在但非本应用生成的同名文件时跳过(保留)。
+///
+/// 覆盖式仅针对本应用生成的 shim(自愈时内容与当前安装一致);用户手动放置的
+/// 同名 `dsh`/`pnpm` 一律保留不动,避免覆盖用户自己的安装与配置。
pub fn write_shims(app_handle: &AppHandle, bin_dir: &Path) -> Result<(), String> {
let app_dir = config::get_base_dir(app_handle);
fs::create_dir_all(bin_dir).map_err(|e| format!("create bin dir failed: {e}"))?;
+ // 写入单个 shim:若目标已存在且非本应用生成,则跳过不覆盖(保留用户文件)。
+ macro_rules! write_if_ours {
+ ($path:expr, $content:expr) => {{
+ let target = bin_dir.join($path);
+ if target.exists() && is_foreign_file(&target) {
+ log::warn!(
+ "Skipping shim write to {:?}: an existing user file is preserved",
+ target
+ );
+ } else {
+ fs::write(&target, $content)
+ .map_err(|e| format!("write {} failed: {e}", target.display()))?;
+ }
+ target
+ }};
+ }
+
#[cfg(windows)]
{
- fs::write(bin_dir.join(SHIM_CMD_NAME), build_cmd_shim(&app_dir))
- .map_err(|e| format!("write {SHIM_CMD_NAME} failed: {e}"))?;
- fs::write(bin_dir.join(SHIM_PS1_NAME), build_ps1_shim(&app_dir))
- .map_err(|e| format!("write {SHIM_PS1_NAME} failed: {e}"))?;
- fs::write(bin_dir.join(PNPM_SHIM_CMD_NAME), build_pnpm_cmd_shim(&app_dir))
- .map_err(|e| format!("write {PNPM_SHIM_CMD_NAME} failed: {e}"))?;
- fs::write(bin_dir.join(PNPM_SHIM_PS1_NAME), build_pnpm_ps1_shim(&app_dir))
- .map_err(|e| format!("write {PNPM_SHIM_PS1_NAME} failed: {e}"))?;
+ write_if_ours!(SHIM_CMD_NAME, build_cmd_shim(&app_dir));
+ write_if_ours!(SHIM_PS1_NAME, build_ps1_shim(&app_dir));
+ write_if_ours!(PNPM_SHIM_CMD_NAME, build_pnpm_cmd_shim(&app_dir));
+ write_if_ours!(PNPM_SHIM_PS1_NAME, build_pnpm_ps1_shim(&app_dir));
}
#[cfg(not(windows))]
{
- // 类型由右侧推断,避免 Windows 构建引入未使用的 PathBuf 导入
- let shims = [
- (bin_dir.join(SHIM_SH_NAME), build_sh_shim(&app_dir)),
- (bin_dir.join(PNPM_SHIM_SH_NAME), build_pnpm_sh_shim(&app_dir)),
- ];
- for (shim, content) in shims {
- fs::write(&shim, content)
- .map_err(|e| format!("write {} failed: {e}", shim.display()))?;
- #[cfg(unix)]
- {
+ write_if_ours!(SHIM_SH_NAME, build_sh_shim(&app_dir));
+ write_if_ours!(PNPM_SHIM_SH_NAME, build_pnpm_sh_shim(&app_dir));
+ // 仅对本应用生成/覆盖过的 shim 设置可执行位;保留的用户文件不动
+ for name in [SHIM_SH_NAME, PNPM_SHIM_SH_NAME] {
+ let path = bin_dir.join(name);
+ if path.is_file() && !is_foreign_file(&path) {
use std::os::unix::fs::PermissionsExt;
- fs::set_permissions(&shim, fs::Permissions::from_mode(0o755))
+ fs::set_permissions(&path, fs::Permissions::from_mode(0o755))
.map_err(|e| format!("chmod shim failed: {e}"))?;
}
}
@@ -468,4 +582,67 @@ mod tests {
let content = build_ps1_shim(&dir);
assert!(content.contains(r"o''brien"));
}
+
+ #[test]
+ fn cmd_shim_prefers_user_dsh() {
+ let content = build_cmd_shim(&sample_app_dir());
+ assert!(content.contains("USER_DSH"));
+ assert!(content.contains(r#"call "%USER_DSH%" %*"#));
+ assert!(content.contains("SELF_PREFIX"));
+ // 用户 dsh 优先转发应出现在捆绑启动之前
+ let user_at = content.find("USER_DSH").unwrap();
+ let bundled_at = content.find(":use_bundled").unwrap();
+ assert!(user_at < bundled_at);
+ }
+
+ #[test]
+ fn ps1_shim_prefers_user_dsh() {
+ let content = build_ps1_shim(&sample_app_dir());
+ assert!(content.contains("Get-Command dsh -All"));
+ assert!(content.contains("$userDsh.Source"));
+ assert!(content.contains("$PSScriptRoot"));
+ // DSH_HOME 绑定只在捆绑启动分支注入(转发用户 dsh 时保留用户环境)
+ let user_at = content.find("$userDsh").unwrap();
+ let home_at = content.find("$env:DSH_HOME = $dshHome").unwrap();
+ assert!(user_at < home_at);
+ }
+
+ #[test]
+ fn sh_shim_prefers_user_dsh() {
+ #[cfg(not(windows))]
+ {
+ let content = build_sh_shim(&sample_app_dir());
+ assert!(content.contains(r#"exec "$dir/dsh" "$@""#));
+ assert!(content.contains("SELF_DIR"));
+ // 用户 dsh 优先 > 注入 DSH_HOME
+ let user_at = content.find(r#""$dir/dsh""#).unwrap();
+ let home_at = content.find("export DSH_HOME").unwrap();
+ assert!(user_at < home_at);
+ }
+ }
+
+ #[test]
+ fn foreign_file_detection() {
+ let dir = std::env::temp_dir().join(format!(
+ "dsh-shim-test-{}",
+ std::process::id()
+ ));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+
+ // 用户手动放置的 dsh 脚本 -> 视为 foreign,不应被覆盖
+ let user_dsh = dir.join(if cfg!(windows) { "dsh.cmd" } else { "dsh" });
+ std::fs::write(&user_dsh, "#!/bin/sh\necho my real dsh\n").unwrap();
+ assert!(is_foreign_file(&user_dsh), "user file must be treated as foreign");
+
+ // 本应用生成的 shim -> 不是 foreign,可覆盖
+ #[cfg(not(windows))]
+ let generated = build_sh_shim(&sample_app_dir());
+ #[cfg(windows)]
+ let generated = build_cmd_shim(&sample_app_dir());
+ std::fs::write(&user_dsh, generated).unwrap();
+ assert!(!is_foreign_file(&user_dsh), "generated shim must not be foreign");
+
+ let _ = std::fs::remove_dir_all(&dir);
+ }
}
diff --git a/src/components/debug-sidebar.tsx b/src/components/debug-sidebar.tsx
index f0f83820..0503bf83 100644
--- a/src/components/debug-sidebar.tsx
+++ b/src/components/debug-sidebar.tsx
@@ -41,6 +41,7 @@ export interface CliLinkStatus {
enabled: boolean
shim_exists: boolean
path_registered: boolean
+ user_dsh_preserved: boolean
bin_dir: string
shim_path: string
}
@@ -359,6 +360,9 @@ export default function DebugSidebar() {
<code className="mt-1 block truncate break-all text-[10px] text-muted">{cliStatus.bin_dir}</code>
)}
<p className="mt-1 text-[11px] text-muted">{t('ui.cli_link_hint')}</p>
+ {cliStatus?.user_dsh_preserved && (
+ <p className="mt-1 text-[11px] font-medium text-ink">{t('ui.cli_link_user_dsh_preserved')}</p>
+ )}
</div>
<button className={button({ size: 'sm', tone: 'primary', block: true })} onClick={saveConfig} disabled={saving}>
diff --git a/src/i18n/locales/en-US.json b/src/i18n/locales/en-US.json
index 8cf94838..66e3515f 100644
--- a/src/i18n/locales/en-US.json
+++ b/src/i18n/locales/en-US.json
@@ -48,6 +48,7 @@
"ui.cli_link_status_partial": "Enabled but PATH registration incomplete — try saving again",
"ui.cli_link_status_unknown": "Status unknown",
"ui.cli_link_hint": "Run dsh --help in a new terminal",
+ "ui.cli_link_user_dsh_preserved": "A dsh you installed yourself was detected and preserved — the dsh command still points to your own install and config",
"ui.app_info": "App Information",
"ui.current_version": "Current Version",
"ui.dsh_version": "Harness Version",
diff --git a/src/i18n/locales/zh-CN.json b/src/i18n/locales/zh-CN.json
index fe47cdf8..0947f573 100644
--- a/src/i18n/locales/zh-CN.json
+++ b/src/i18n/locales/zh-CN.json
@@ -48,6 +48,7 @@
"ui.cli_link_status_partial": "已开启,但 PATH 注册不完整,请重新保存",
"ui.cli_link_status_unknown": "状态未知",
"ui.cli_link_hint": "在新终端中运行 dsh --help 查看用法",
+ "ui.cli_link_user_dsh_preserved": "检测到你已自行安装 dsh,已保留未覆盖;终端中的 dsh 仍指向你自己的安装与配置",
"ui.app_info": "应用信息",
"ui.current_version": "当前版本",
"ui.dsh_version": "Harness 版本",