Bun

建置 Windows

本文描述 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

Visual Studio 以及「使用 C++ 的桌面開發」工作負載。安裝時,請務必同時安裝 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 18.1.8
  • Go
  • Rust
  • NASM
  • Perl
  • Ruby
  • Node.js
  • Ccache

注意 – Zig 編譯器會由建置流程自動下載、安裝和更新。

Scoop 可用於輕鬆安裝這些剩餘工具。

Scoop
Scoop
irm https://get.scoop.sh | iex
scoop install nodejs-lts go rust nasm ruby perl ccache
# scoop seems to be buggy if you install llvm and the rest at the same time
scoop install llvm@18.1.8

請勿使用 WinGet/其他套件管理器來安裝這些工具,因為您可能會安裝 Strawberry Perl 而不是更精簡的 Perl 安裝。Strawberry Perl 包含許多其他實用程式,這些程式會安裝到 $Env:PATH 中,這會與 MSVC 衝突並導致建置失敗。

如果您打算在本機建置 WebKit (選用),您應該安裝這些套件

Scoop
scoop install make cygwin python

從現在開始,預期您使用 PowerShell 終端機並載入 .\scripts\vs-shell.ps1 腳本。此腳本在 Bun 儲存庫中提供,可以透過執行來載入

.\scripts\vs-shell.ps1

為了驗證,您可以檢查僅限 MSVC 的命令列,例如 mt.exe

Get-Command mt

不建議將 ninja / cmake 安裝到您的全域路徑中,因為您可能會遇到在未載入 .\scripts\vs-shell.ps1 腳本的情況下嘗試建置 bun 的情況。

建置

bun run build

# after the initial `bun run build` you can use the following to 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 <path> 或使用包裝腳本 packages\bun-internal-test 來執行測試套件。內部測試套件是一個包裝 CLI,用於在 bun.exe 的個別實例中執行每個測試檔案,以防止測試執行器中的崩潰停止整個套件。

# Setup
bun i --cwd packages\bun-internal-test

# Run the entire test suite with reporter
# the package.json script "test" uses "build/debug/bun-debug.exe" by default
bun run test

# Run an individual test file:
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 已經開啟,您無法覆寫它。您可能已經有一個執行中的實例,也許在 VS Code 除錯器中?