Skip to content

Commit d2db919

Browse files
author
houkexin
committed
fix: 内置 DSH 启动前去除 Windows verbatim 路径前缀
Tauri 的 resource_dir() 在 Windows 上可能返回 \\\\?\\ 前缀的 verbatim 路径, Node.js 22.20+ 无法将其解析为主模块(nodejs/node#60435),会以 'EISDIR: illegal operation on a directory, lstat D:' 崩溃。 现在对交给内置 node 子进程的所有路径(node.exe、bin.js、DSH_HOME)统一 规范化,去掉 \\\\?\\ 前缀。
1 parent cca16cf commit d2db919

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

‎src-tauri/src/server.rs‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ use std::{
1515
use serde::Serialize;
1616
use tauri::{AppHandle, Manager};
1717

18+
/// 去掉 Windows verbatim 前缀(`\\?\` / `\\?\UNC\`),返回普通路径。
19+
///
20+
/// Tauri 的 `resource_dir()` 在 Windows 上可能返回 `\\?\D:\...` 形式的
21+
/// verbatim 路径;Node.js 22.20+(含捆绑的 v22.21.1)无法把这种路径解析为
22+
/// 主模块,会把 `\\?\D:\...` 弄成只剩盘符的 `D:`,并以
23+
/// `EISDIR: illegal operation on a directory, lstat 'D:'` 崩溃
24+
/// (见 nodejs/node#60435)。因此所有交给内置 Node 子进程的路径
25+
/// (node.exe、bin.js、DSH_HOME 等)都必须先规范化。
26+
#[cfg(windows)]
27+
fn strip_verbatim_prefix(p: &std::path::Path) -> PathBuf {
28+
use std::os::windows::ffi::{OsStrExt, OsStringExt};
29+
let wide: Vec<u16> = p.as_os_str().encode_wide().collect();
30+
const VERBATIM: [u16; 4] = [b'\\' as u16, b'\\' as u16, b'?' as u16, b'\\' as u16];
31+
if !wide.starts_with(&VERBATIM) {
32+
return p.to_path_buf();
33+
}
34+
let rest = &wide[4..];
35+
let mut out: Vec<u16> = Vec::with_capacity(rest.len());
36+
if rest.starts_with(&[b'U' as u16, b'N' as u16, b'C' as u16, b'\\' as u16]) {
37+
// \\?\UNC\server\share\... -> \\server\share\...
38+
out.extend_from_slice(&[b'\\' as u16, b'\\' as u16]);
39+
out.extend_from_slice(&rest[4..]);
40+
} else {
41+
// \\?\D:\... -> D:\...
42+
out.extend_from_slice(rest);
43+
}
44+
PathBuf::from(std::ffi::OsString::from_wide(&out))
45+
}
46+
47+
#[cfg(not(windows))]
48+
fn strip_verbatim_prefix(p: &std::path::Path) -> PathBuf {
49+
p.to_path_buf()
50+
}
51+
1852
/// 由 `tauri::State` 持有的内置服务器句柄
1953
pub struct ManagedServer {
2054
child: Option<Child>,
@@ -48,7 +82,8 @@ impl ManagedServer {
4882
}
4983

5084
pub(crate) fn data_dir(app: &AppHandle) -> Result<PathBuf, String> {
51-
let dir = app.path().app_data_dir().map_err(|e| e.to_string())?;
85+
let raw = app.path().app_data_dir().map_err(|e| e.to_string())?;
86+
let dir = strip_verbatim_prefix(&raw);
5287
std::fs::create_dir_all(&dir).map_err(|e| format!("无法创建数据目录: {e}"))?;
5388
Ok(dir)
5489
}
@@ -65,8 +100,9 @@ impl ManagedServer {
65100

66101
/// 内置资源路径:`<resource_dir>/node/node.exe` 与
67102
/// `<resource_dir>/dsh/node_modules/@deepseek-ai/dsh/lib/bin.js`
103+
/// (资源目录先去掉 Windows verbatim 前缀,否则 Node 无法解析脚本路径)
68104
fn bundled_paths(app: &AppHandle) -> Option<(PathBuf, PathBuf)> {
69-
let res = app.path().resource_dir().ok()?;
105+
let res = strip_verbatim_prefix(&app.path().resource_dir().ok()?);
70106
let node = res.join("node").join("node.exe");
71107
let bin = res
72108
.join("dsh")

0 commit comments

Comments
 (0)