Skip to content

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

fix(preinstall): auto-allow pnpm build scripts from error output and retry (issues #39, #41)

hairyf3ec5faa994db
diff --git a/src-tauri/src/service/plugin/install.rs b/src-tauri/src/service/plugin/install.rs
index 390ef487..b79cbd61 100644
--- a/src-tauri/src/service/plugin/install.rs
+++ b/src-tauri/src/service/plugin/install.rs
@@ -1,6 +1,14 @@
//! 预装插件安装:校验选中项、准备环境(pnpm/dsh shim、按需补齐捆绑 pnpm、
//! 停止运行中的服务),随后调用 `dsh plugin --profile web add <specs...>`,
//! 成功后执行 Windows 极简模式专项修复。
+//!
+//! pnpm v11 对两类构建脚本默认不放行、缺白名单时报硬错误:
+//! 1. git 托管插件的 `prepare` 构建(`ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`)——
+//! 其允许键(depPath = `name@<pkgResolutionId>`)随 pnpm 的克隆方式变化
+//! (git+ssh#sha / codeload tar.gz),无法预先确定;
+//! 2. 传递依赖的原生构建(如 `node-pty`,`ERR_PNPM_IGNORED_BUILDS`)。
+//! 因此在安装失败时从 pnpm 错误输出解析它建议的 `allowBuilds` 键,写入 profile
+//! 的 `pnpm-workspace.yaml` 后重试,直至成功或无可解析项。
use crate::config;
use crate::service::cli;
@@ -9,12 +17,17 @@ use crate::service::download::Installable;
use crate::service::workflow;
use std::collections::HashMap;
use std::ffi::OsString;
+use std::path::PathBuf;
use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
-use super::installed::PREINSTALL_PROFILE;
+use super::installed::{PREINSTALL_PROFILE, profile_dir};
use super::preset::load_presets;
use super::process::{run_plugin_process, PreinstallLogPayload, PREINSTALL_LOG_EVENT};
+/// 允许构建重试的上限。每次重试解决 pnpm 报出的一个允许键(git depPath 或
+/// 传递构建包名),多个 git 插件 / 多个原生依赖各占一次,上限封顶防死循环。
+const MAX_ALLOW_LIST_RETRIES: usize = 8;
+
/// 校验并安装选中的预装插件:`dsh plugin --profile web add <ids...>`
pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), String> {
if ids.is_empty() {
@@ -101,7 +114,37 @@ pub async fn install(app_handle: &AppHandle, ids: &[String]) -> Result<(), Strin
let cwd = config::get_dsh_install_path(app_handle);
log::info!("Running dsh plugin install for {ids:?}");
- let exit_code = run_plugin_process(&node, &args, &cwd, &envs, &window).await?;
+ // `dsh plugin add` 在 profile 目录里驱动 pnpm。pnpm v11 会拦下 git 托管
+ // 插件的 prepare 构建与传递原生依赖(见模块头注),其允许键不可预知,因此
+ // 失败时解析输出里印出的 `allowBuilds` 键写回 profile 的 pnpm-workspace.yaml
+ // 后重试,直至成功或再无键可加。
+ let mut retries = 0usize;
+ let exit_code = loop {
+ let (code, captured) =
+ run_plugin_process(&node, &args, &cwd, &envs, &window).await?;
+ if code == 0 {
+ break 0;
+ }
+
+ let new_keys = parse_allowlist_keys(&captured);
+ if new_keys.is_empty() || retries >= MAX_ALLOW_LIST_RETRIES {
+ log::error!(
+ "dsh plugin install failed with exit code {code}; no more allowBuilds entries to add"
+ );
+ break code;
+ }
+
+ retries += 1;
+ add_allow_build_keys(app_handle, &new_keys)?;
+ log::info!("pnpm allowBuilds updated with {new_keys:?}, retrying ({retries})");
+ let _ = window.emit(
+ PREINSTALL_LOG_EVENT,
+ PreinstallLogPayload {
+ line: "[pnpm] 已放行插件构建(allowBuilds),重试安装…".to_string(),
+ },
+ );
+ };
+
if exit_code != 0 {
log::error!("dsh plugin install failed with exit code {exit_code}");
return Err(format!(
@@ -155,3 +198,171 @@ async fn ensure_pnpm(app_handle: &AppHandle, window: &WebviewWindow) -> Result<(
);
Ok(())
}
+
+/// 从 pnpm 失败输出中解析需写入 `allowBuilds` 的键集合:
+/// - git 托管插件 prepare 被拦时,pnpm 会提示 `allowBuilds:\n <depPath>: true`,
+/// 原样采纳 depPath(形式随克隆方式变化,只能是运行期报出的值);
+/// - 传递原生依赖被忽略构建(`Ignored build scripts:`)时,取其 `name@version` 的包名。
+fn parse_allowlist_keys(output: &str) -> Vec<String> {
+ let mut keys: Vec<String> = Vec::new();
+ let lines: Vec<&str> = output.lines().collect();
+
+ // 1) git depPath 允许键:跟随 `allowBuilds:` 示例行后的缩进 `<key>: true`。
+ for (idx, line) in lines.iter().enumerate() {
+ if line.trim() == "allowBuilds:" {
+ if let Some(next) = lines.get(idx + 1) {
+ if let Some(key) = extract_allow_line_key(next) {
+ if !keys.iter().any(|k| k == &key) {
+ keys.push(key);
+ }
+ }
+ }
+ }
+ }
+
+ // 2) 传递原生构建包名:`Ignored build scripts: <name>@<ver>, ...`。
+ for line in &lines {
+ if let Some(sub) = line.split("Ignored build scripts:").nth(1) {
+ for token in sub.split([',', ' ']) {
+ let token = token.trim();
+ if token.is_empty() {
+ continue;
+ }
+ let name = token.split('@').next().unwrap_or(token).trim();
+ if !name.is_empty() && !keys.iter().any(|k| k == name) {
+ keys.push(name.to_string());
+ }
+ }
+ }
+ }
+
+ keys
+}
+
+/// 若 `line` 形如 ` <key>: true`(有缩进),返回 `<key>`(去缩进与后缀)。
+/// pnpm 报出的 depPath 键本身不带引号,这里只做剥离该行格式。
+fn extract_allow_line_key(line: &str) -> Option<String> {
+ let trimmed = line.trim_start();
+ if trimmed.len() == line.len() {
+ return None; // 无缩进,不是白名单条目
+ }
+ let suffix = trimmed.strip_suffix(": true")?;
+ let key = suffix.trim_end();
+ if key.is_empty() {
+ return None;
+ }
+ Some(key.to_string())
+}
+
+/// profile 下的 `pnpm-workspace.yaml` 路径($DSH_HOME/profiles/web)
+fn profile_workspace_path(app_handle: &AppHandle) -> PathBuf {
+ profile_dir(app_handle).join("pnpm-workspace.yaml")
+}
+
+/// 把新的 `allowBuilds` 键合并写回 profile 的 `pnpm-workspace.yaml`。
+///
+/// dsh 的 `initProfile` 仅在文件缺失时创建(其模板无 `allowBuilds`),因此桌面端
+/// 自行维护该块:缺失时按 dsh 模板补建基础设置并追加 `allowBuilds`;已有时按键
+/// 去重合并。git depPath 键含 `@`/`/`/`:`/`#`,按 YAML 单引号标量写入避免误解析;
+/// `false` 不应出现于此(我们只放行)。重复写入同一键是无害的(幂等)。
+fn add_allow_build_keys(app_handle: &AppHandle, keys: &[String]) -> Result<(), String> {
+ let path = profile_workspace_path(app_handle);
+ let dir = path
+ .parent()
+ .ok_or("PREINSTALL_BAD_PROFILE_DIR: no profile dir")?;
+ std::fs::create_dir_all(dir).map_err(|e| format!("PREINSTALL_MKDIR: {e}"))?;
+
+ let mut content = if path.exists() {
+ std::fs::read_to_string(&path)
+ .map_err(|e| format!("PREINSTALL_READ_WORKSPACE: {e}"))?
+ } else {
+ // 与 dsh `initProfile` 生成的基础模板保持一致(尚无 allowBuilds)
+ "packages:\n - .\n\nnodeLinker: hoisted\nautoInstallPeers: false\n".to_string()
+ };
+
+ let has_allow_builds = content
+ .lines()
+ .any(|l| l.trim_start().starts_with("allowBuilds:"));
+ if !has_allow_builds {
+ if !content.ends_with('\n') {
+ content.push('\n');
+ }
+ content.push_str("allowBuilds:\n");
+ }
+
+ // 收集已有 ` <key>: true` 条目(含单引号形式),避免重复。基础模板里
+ // 的 `packages`/`nodeLinker`/`autoInstallPeers` 等行不会以 `: true` 结尾,
+ // 天然被排除。
+ let existing: Vec<String> = content
+ .lines()
+ .filter_map(|l| {
+ let trimmed = l.trim_start();
+ if trimmed.len() == l.len() {
+ return None; // 非缩进行(顶层键)不参与
+ }
+ let suffix = trimmed.strip_suffix(": true")?;
+ let key = suffix.trim().trim_matches(['\'', '"']);
+ if key.is_empty() || key.contains(':') {
+ return None;
+ }
+ Some(key.to_string())
+ })
+ .collect();
+
+ let mut dirty = false;
+ for key in keys {
+ if existing.iter().any(|k| k == key) {
+ continue;
+ }
+ // 单引号包裹键:git depPath 含 `:`/`#`/`@`,裸写会让 YAML 误解析
+ content.push_str(&format!(" '{}': true\n", key.replace('\'', "''")));
+ dirty = true;
+ }
+ if !dirty {
+ return Ok(());
+ }
+
+ std::fs::write(&path, content).map_err(|e| format!("PREINSTALL_WRITE_WORKSPACE: {e}"))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{extract_allow_line_key, parse_allowlist_keys};
+
+ #[test]
+ fn parse_git_dep_path_key() {
+ let out = "\
+[ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED] Failed to prepare git-hosted package fetched from \"...\"
+The git-hosted package \"[email protected]\" needs to execute build scripts but is not in the \"allowBuilds\" allowlist.
+...
+allowBuilds:
+ dsh-better-sidebar@git+ssh://[email protected]/omdsh-dev/DSH-better-sidebar.git#6c89: true
+";
+ let keys = parse_allowlist_keys(out);
+ assert!(keys.contains(&"dsh-better-sidebar@git+ssh://[email protected]/omdsh-dev/DSH-better-sidebar.git#6c89".to_string()));
+ assert!(!keys.contains(&"dsh-better-sidebar".to_string()));
+ }
+
+ #[test]
+ fn parse_ignored_builds_name() {
+ let out = "[ERR_PNPM_IGNORED_BUILDS] Ignored build scripts: [email protected]\n";
+ let keys = parse_allowlist_keys(out);
+ assert_eq!(keys, vec!["node-pty".to_string()]);
+ }
+
+ #[test]
+ fn parse_empty_when_irrelevant() {
+ let out = "everything looks fine output\nno allowlist here\n";
+ assert!(parse_allowlist_keys(out).is_empty());
+ }
+
+ #[test]
+ fn allow_line_key_requires_indent() {
+ let key = extract_allow_line_key(" node-pty: true");
+ assert_eq!(key.as_deref(), Some("node-pty"));
+
+ // 无缩进(顶层键)不应被当作白名单条目
+ assert_eq!(extract_allow_line_key("packages:"), None);
+ assert_eq!(extract_allow_line_key("allowBuilds:"), None);
+ }
+}
diff --git a/src-tauri/src/service/plugin/process.rs b/src-tauri/src/service/plugin/process.rs
index 4540ff43..b6ac0ee9 100644
--- a/src-tauri/src/service/plugin/process.rs
+++ b/src-tauri/src/service/plugin/process.rs
@@ -10,6 +10,7 @@ use std::collections::HashMap;
use std::ffi::OsString;
use std::io::{BufRead, BufReader, Read};
use std::path::Path;
+use std::sync::{Arc, Mutex};
use tauri::{Emitter, WebviewWindow};
#[cfg(windows)]
@@ -36,22 +37,29 @@ struct WaitableHandle(windows_sys::Win32::Foundation::HANDLE);
#[cfg(windows)]
unsafe impl Send for WaitableHandle {}
-/// 启动 `dsh plugin` 进程并等待结束,返回退出码;输出实时转发事件。
+/// 启动 `dsh plugin` 进程并等待结束,返回 `(退出码, 捕获的完整输出)`。
+///
+/// 输出仍然逐行实时转发为 `preinstall-log` 事件(供前端进度反馈),同时把
+/// 全部行追加进共享缓冲区并返回——安装失败时 pnpm 会在错误里印出
+/// `allowBuilds:` 允许键(git depPath / 被忽略的构建包名),调用方需要这段
+/// 文本去解析并重试。
pub(crate) async fn run_plugin_process(
node: &Path,
args: &[OsString],
cwd: &Path,
envs: &HashMap<String, String>,
window: &WebviewWindow,
-) -> Result<i32, String> {
+) -> Result<(i32, String), String> {
+ let captured = Arc::new(Mutex::new(String::new()));
+
#[cfg(windows)]
{
let (stdout, stderr, handle) =
workflow::win_spawn::spawn_with_hidden_console_tracked(node, args, Some(cwd), envs)
.map_err(|e| format!("PREINSTALL_SPAWN: {e}"))?;
- spawn_line_emitter(stdout, window.clone());
- spawn_line_emitter(stderr, window.clone());
+ spawn_line_emitter(stdout, window.clone(), captured.clone());
+ spawn_line_emitter(stderr, window.clone(), captured.clone());
let handle = WaitableHandle(handle);
let exit_code = tauri::async_runtime::spawn_blocking(move || {
@@ -74,7 +82,7 @@ pub(crate) async fn run_plugin_process(
.map_err(|e| format!("PREINSTALL_WAIT: {e}"))?;
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
- Ok(exit_code)
+ Ok((exit_code, drain_captured(captured)))
}
#[cfg(not(windows))]
@@ -90,10 +98,10 @@ pub(crate) async fn run_plugin_process(
.map_err(|e| format!("PREINSTALL_SPAWN: {e}"))?;
if let Some(stdout) = child.stdout.take() {
- spawn_line_emitter(stdout, window.clone());
+ spawn_line_emitter(stdout, window.clone(), captured.clone());
}
if let Some(stderr) = child.stderr.take() {
- spawn_line_emitter(stderr, window.clone());
+ spawn_line_emitter(stderr, window.clone(), captured.clone());
}
let exit_code = tauri::async_runtime::spawn_blocking(move || {
@@ -103,22 +111,40 @@ pub(crate) async fn run_plugin_process(
.map_err(|e| format!("PREINSTALL_WAIT: {e}"))?;
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
- Ok(exit_code)
+ Ok((exit_code, drain_captured(captured)))
}
}
-/// 在独立线程中逐行读取进程输出并通过 `preinstall-log` 事件转发。
+/// 取出(并清空)共享缓冲区中的全部捕获输出。
+fn drain_captured(captured: Arc<Mutex<String>>) -> String {
+ captured
+ .lock()
+ .map(|mut buf| std::mem::take(&mut *buf))
+ .unwrap_or_default()
+}
+
+/// 在独立线程中逐行读取进程输出:实时通过 `preinstall-log` 事件转发,
+/// 同时追加进共享缓冲区。
/// 使用静态泛型约束 `R: Read + Send + 'static` 避免动态派发(Box<dyn Read>)堆分配。
-fn spawn_line_emitter<R: Read + Send + 'static>(reader: R, window: WebviewWindow) {
+fn spawn_line_emitter<R: Read + Send + 'static>(
+ reader: R,
+ window: WebviewWindow,
+ captured: Arc<Mutex<String>>,
+) {
std::thread::spawn(move || {
let buf = BufReader::new(reader);
for line in buf.lines().map_while(Result::ok) {
+ let trimmed = line.trim_end().to_string();
let _ = window.emit(
PREINSTALL_LOG_EVENT,
PreinstallLogPayload {
- line: line.trim_end().to_string(),
+ line: trimmed.clone(),
},
);
+ if let Ok(mut acc) = captured.lock() {
+ acc.push_str(&trimmed);
+ acc.push('\n');
+ }
}
});
}
\ No newline at end of file