Skip to main content
本文档介绍了在 Windows 上的构建流程。如果您遇到问题,请加入我们的 Discord #contributing 频道 寻求帮助。 强烈建议使用 PowerShell 7(pwsh.exe,而非默认的 powershell.exe

前置条件

启用脚本执行

默认情况下,运行未验证脚本会被阻止。
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

系统依赖

Bun 版本需为 v1.1 或更高。我们使用 Bun 来运行它自身的代码生成器。
irm bun.sh/install.ps1 | iex
安装带有“使用 C++ 的桌面开发”工作负载的 Visual Studio。安装时请确保同时安装 Git(若系统尚未安装 Git for Windows)。 Visual Studio 可以通过向导图形界面安装,或用 WinGet 命令安装:
winget install "Visual Studio Community 2022" --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.Component.Git " -s msstore
安装 Visual Studio 后,需要安装以下工具:
  • LLVM 19.1.7
  • Go
  • Rust
  • NASM
  • Perl
  • Ruby
  • Node.js
构建过程中会自动下载、安装和更新 Zig 编译器。
Scoop 是一个便捷的工具,用来安装上述剩余工具。
Scoop
irm https://get.scoop.sh | iex
scoop install nodejs-lts go rust nasm ruby perl ccache
# scoop 在同时安装 llvm 及其他工具时可能有问题
scoop install [email protected]
请勿使用 WinGet 或其他包管理器安装这些工具,因为您可能会安装 Strawberry Perl,而不是更轻量的 Perl 版本。Strawberry Perl 会安装许多其他工具到 $Env:PATH,这些工具会与 MSVC 冲突并导致构建失败。
如果您计划本地构建 WebKit(可选),请安装以下包:
Scoop
scoop install make cygwin python
从此开始,建议您使用以 .\scripts\vs-shell.ps1 脚本启动的 PowerShell 终端。该脚本位于 Bun 仓库中,可通过执行该脚本加载:
.\scripts\vs-shell.ps1
您可以通过检测一个 MSVC 专用命令行工具(比如 mt.exe)来验证是否正确加载:
Get-Command mt
不建议将 ninjacmake 安装到全局路径中,因为您可能会在未加载 .\scripts\vs-shell.ps1 的情况下构建 Bun,导致构建失败。

构建

bun run build

# 初次运行完成后,可以用下面命令构建
ninja -Cbuild/debug
如果成功,您会在 build/debug 文件夹中看到 bun-debug.exe
.\build\debug\bun-debug.exe --revision
建议将此路径加入 $Env:PATH。最简便的办法是打开开始菜单,输入“Path”,然后进入环境变量菜单,将 C:\.....\bun\build\debug 添加到用户环境变量 PATH。之后重启编辑器(如果依旧未更新,则注销后重新登录)。

额外路径

  • WebKit 解压路径为 build/debug/cache/webkit/
  • Zig 解压路径为 build/debug/cache/zig/bin/zig.exe

测试

您可以使用 bun test <路径> 或封装脚本 bun node:test <路径> 运行测试套件。bun node:test 命令会在独立的 bun.exe 实例中运行每个测试文件,防止测试运行器崩溃影响整个测试套件。
# 安装依赖
bun i --cwd packages\bun-internal-test

# 运行完整测试套件并带有报告器
# package.json 中的 “test” 脚本默认使用 "build/debug/bun-debug.exe"
bun run test

# 运行单个测试文件:
bun-debug test node\fs
bun-debug test "C:\bun\test\js\bun\resolve\import-meta.test.js"

故障排查

.rc 文件构建失败

llvm-rc.exe 表现异常,请勿使用。请使用 rc.exe,确保您在 Visual Studio 的开发者终端中,并用 rc /? 检查是否为 “Microsoft Resource Compiler”。

输出写入失败 ‘bun-debug.exe’: 权限被拒绝

如果 bun-debug.exe 已打开,您无法覆盖它。可能是您有正在运行的实例,或者在 VSCode 调试器中还未关闭。