build:single-exe 在Windows下失败:EPERM mkdir权限错误
问题描述
在 Windows 系统下执行 bun build:single-exe 时,脚本 download:tunwg.ts 失败并抛出 EPERM: operation not permitted, mkdir 'C:' 错误。
根本原因
在 Windows 系统下,new URL(import.meta.url).pathname 会返回以 / 开头的路径(例如 /C:/Users/...),导致路径解析错误,最终 mkdirSync 尝试创建 C: 根目录触发 EPERM 权限错误。
修复方案
使用 Node.js 内置的 fileURLToPath 正确解析 import.meta.url 的路径(自动适配 Windows 路径格式)。
相关文件
hub/scripts/download-tunwg.ts:39
我查了当前代码,build:single-exe 这条链路在仓库当前状态下依次执行:下载 tunwg、构建 web、生成嵌入式 web 资源清单、再由 CLI 脚本调用 bun build --compile 生成单文件可执行程序。你贴出来的日志里 [embedded-assets] Wrote ... 已经成功出现,所以至少可以确认 embedded assets manifest 这一步已经完成,后面才进入 bun build --compile。
我在当前仓库里没有验证到你正文里给出的那个根因。生成 manifest 和构建 exe 的两个脚本都已经使用 fileURLToPath(import.meta.url) 处理脚本路径;当前代码里唯一仍然使用 new URL(import.meta.url).pathname 的是 download-tunwg 脚本,但你提供的日志只显示这个步骤完成了 Skipping ...,没有显示它在这里抛错。基于现有日志和当前代码,我还不能确认这次 EPERM mkdir 的确切根因。
Relevant code:
package.json:10-build:single-exe的执行顺序hub/scripts/generate-embedded-web-assets.ts:57- 生成 embedded assets 时使用fileURLToPath(import.meta.url)解析脚本目录cli/scripts/build-executable.ts:215- 这里调用bun build --compilecli/scripts/build-executable.ts:260- 构建脚本同样使用fileURLToPath(import.meta.url)解析脚本目录hub/scripts/download-tunwg.ts:37- 当前仓库里仍使用new URL(import.meta.url).pathname
Need more info:
- 你使用的是哪个版本/commit?如果是 npm 包,请附上
hapi --version - 请贴出
[build:exe] ...之后的完整原始 stderr / stack,而不是整理后的“根本原因” - 这是在仓库源码目录里执行
bun run build:single-exe,还是在发布包里执行? - 单独运行
cd cli && bun run scripts/build-executable.ts --with-web-assets --target bun-windows-x64时,是否仍然报同一个EPERM mkdir?
HAPI Bot
如下
C:\Users\norm\1\program\hapi [main]> bun build:single-exe
$ bun run download:tunwg && bun run build:web && (cd hub && bun run generate:embedded-web-assets) && (cd cli && bun run build:exe:allinone)
$ bun run hub/scripts/download-tunwg.ts
Downloading tunwg binaries...
Downloading https://github.com/tiann/tunwg/releases/latest/download/tunwg...
Error: 25 | if (!response.ok) {
26 | throw new Error(`Failed to download ${url}: ${response.status} ${response.statusText}`);
27 | }
28 |
29 | const buffer = await response.arrayBuffer();
30 | mkdirSync(dirname(destPath), { recursive: true });
^
EPERM: operation not permitted, mkdir 'C:'
path: "C:",
syscall: "mkdir",
errno: -1,
code: "EPERM"
at downloadFile (C:\Users\norm\1\program\hapi\hub\scripts\download-tunwg.ts:30:5)
at async main (C:\Users\norm\1\program\hapi\hub\scripts\download-tunwg.ts:52:15)
error: script "download:tunwg" exited with code 1
error: script "build:single-exe" exited with code 1