第146章 在 Windows 上构建 Zed 编辑器指南
以下命令可在任意 Shell 中执行。
仓库
克隆 Zed 仓库。
依赖项
-
安装 rustup
-
安装带有可选组件
MSVC v*** - VS YYYY C++ x64/x86 build tools和MSVC v*** - VS YYYY C++ x64/x86 Spectre-mitigated libs (latest)的 Visual Studio(v***代表你的 VS 版本,YYYY代表发布年份,请根据需要调整架构)。 - 或者,如果你偏好更精简的安装,可以仅安装 Build Tools(外加上述库)以及 "Desktop development with C++" 工作负载。 这种配置不会被 rustup 自动识别。编译前,请通过启动开始菜单或 Windows Terminal 中安装的开发者 Shell(cmd/PowerShell)来初始化环境变量。
- 安装适用于你系统的 Windows 11 或 10 SDK,并确保至少安装了
Windows 10 SDK version 2104 (10.0.20348.0)。你可以从 Windows SDK Archive 下载。 - 安装 CMake(由 某个依赖项 所需)。或者,你可以通过 Visual Studio Installer 安装,然后手动将
bin目录添加到PATH中,例如:C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin。
从 Visual Studio 2026(或 MSVC 14.50)开始,你需要安装可选组件
MSVC Build Tools for x64/x86 (Latest)和C++ Spectre-mitigated libraries for x64/x86 (Latest MSVC),因为 Microsoft 已将 MSVC 版本与 Visual Studio 版本解耦。传统的命名格式MSVC v*** - VS YYYY C++ x64/x86 ...将不再适用于新的 MSVC 工具链。详见 此博客文章。
如果无法编译 Zed,请确保 Visual Studio 安装中至少包含以下组件:
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Component.CoreEditor",
"Microsoft.VisualStudio.Workload.CoreEditor",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"Microsoft.VisualStudio.ComponentGroup.WebToolsExtensions.CMake",
"Microsoft.VisualStudio.Component.VC.CMake.Project",
"Microsoft.VisualStudio.Component.Windows11SDK.26100",
"Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre"
],
"extensions": []
}
如果仅使用 Build Tools,请确保已安装这些组件:
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Component.Roslyn.Compiler",
"Microsoft.Component.MSBuild",
"Microsoft.VisualStudio.Component.CoreBuildTools",
"Microsoft.VisualStudio.Workload.MSBuildTools",
"Microsoft.VisualStudio.Component.Windows10SDK",
"Microsoft.VisualStudio.Component.VC.CoreBuildTools",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"Microsoft.VisualStudio.Component.VC.Redist.14.Latest",
"Microsoft.VisualStudio.Component.Windows11SDK.26100",
"Microsoft.VisualStudio.Component.VC.CMake.Project",
"Microsoft.VisualStudio.Component.TextTemplating",
"Microsoft.VisualStudio.Component.VC.CoreIde",
"Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core",
"Microsoft.VisualStudio.Workload.VCTools",
"Microsoft.VisualStudio.Component.VC.Runtimes.x86.x64.Spectre"
],
"extensions": []
}
可以通过以下步骤导出此组件列表:
- 打开 Visual Studio Installer
- 在
Installed选项卡中点击More - 点击
Export configuration
备注
将 data 目录下的 pg_hba.conf 中 host 方法的认证方式从 scram-sha-256 改为 trust,否则连接时会报 password authentication failed 错误。该文件通常位于 C:\Program Files\PostgreSQL\17\data\pg_hba.conf。修改后应如下所示:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
如果你的 Windows 使用非拉丁语系区域设置,需要将 data 目录下 postgresql.conf 中的 lc_messages 参数设为 English_United States.1252(或系统上其他兼容 UTF-8 的编码),否则数据库可能会崩溃。修改后应如下所示:
# lc_messages = 'Chinese (Simplified)_China.936' # locale for system error message strings
lc_messages = 'English_United States.1252'
完成后重启 postgresql 服务:按 Win+R 打开运行对话框,输入 services.msc 并点击确定。在服务管理器中找到 postgresql-x64-XX,右键选择重新启动。
从源码构建
依赖装好后,就可以用 Cargo 构建 Zed 了。
Debug 构建:
cargo run
Release 构建:
cargo run --release
运行测试:
cargo test --workspace
注意:视觉回归测试目前仅支持 macOS,且需要屏幕录制权限。详见 Building Zed for macOS。
通过 msys2 安装
Zed 不支持为 Mingw-w64 构建的非官方 MSYS2 Zed 软件包。如果您遇到与 mingw-w64-zed 相关的任何问题,请向 msys2/MINGW-packages/issues 报告。
请先参阅MSYS2 文档。
故障排查
设置 RUSTFLAGS 环境变量会导致构建失败
如果您设置了 RUSTFLAGS 环境变量,它将覆盖 .cargo/config.toml 中的 rustflags 配置,而后者对于正确构建 Zed 是必需的。
由于这些配置会随时间变化,由此产生的构建错误可能从链接器故障到其他难以诊断的错误不等。
如果您需要额外的 Rust 标志,请在 .cargo/config.toml 中使用以下任一方法:
在 build 部分添加您的标志
[build]
rustflags = ["-C", "symbol-mangling-version=v0", "--cfg", "tokio_unstable"]
在 windows target 部分添加您的标志
[target.'cfg(target_os = "windows")']
rustflags = [
"--cfg",
"windows_slim_errors",
"-C",
"target-feature=+crt-static",
]
或者,在 Zed 仓库的父目录中创建一个新的 .cargo/config.toml(如下图所示)。这在 CI 中非常有用,因为您无需编辑仓库原始的 .cargo/config.toml。
upper_dir
├── .cargo // <-- 创建此文件夹
│ └── config.toml // <-- 创建此文件
└── zed
├── .cargo
│ └── config.toml
└── crates
├── assistant
└── ...
在新的(上述).cargo/config.toml 中,如果我们要向 rustflags 添加 --cfg gles,它看起来会是这样
[target.'cfg(all())']
rustflags = ["--cfg", "gles"]
Cargo 报错称某个依赖项使用了不稳定特性
请尝试运行 cargo clean 和 cargo build。
STATUS_ACCESS_VIOLATION
如果您正在使用 "rust-lld.exe" 链接器,可能会出现此错误。请考虑尝试使用其他链接器。
如果你使用的是全局配置,建议将 Zed 仓库移至嵌套目录,并在父目录中添加一个包含自定义 linker 配置的 .cargo/config.toml 文件。
更多详细信息请参阅此 Issue #12041
选择了无效的 RC 路径
有时,根据笔记本电脑上应用的安全规则不同,在编译 Zed 时可能会遇到以下错误:
error: failed to run custom build command for `zed(C:\Users\USER\src\zed\crates\zed)`
Caused by:
process didn't exit successfully: `C:\Users\USER\src\zed\target\debug\build\zed-b24f1e9300107efc\build-script-build` (exit code: 1)
--- stdout
cargo:rerun-if-changed=../../.git/logs/HEAD
cargo:rustc-env=ZED_COMMIT_SHA=25e2e9c6727ba9b77415588cfa11fd969612adb7
cargo:rustc-link-arg=/stack:8388608
cargo:rerun-if-changed=resources/windows/app-icon.ico
package.metadata.winresource does not exist
Selected RC path: 'bin\x64\rc.exe'
--- stderr
The system cannot find the path specified. (os error 3)
warning: build failed, waiting for other jobs to finish...
要修复此问题,请手动将 ZED_RC_TOOLKIT_PATH 环境变量设置为 RC toolkit 的路径。通常路径为:
C:\Program Files (x86)\Windows Kits\10\bin\<SDK_version>\x64。
更多详细信息请参阅此 Issue。
构建失败:路径过长
构建过程中你可能会收到如下错误:
error: failed to get `pet` as a dependency of package `languages v0.1.0 (D:\a\zed-windows-builds\zed-windows-builds\crates\languages)`
Caused by:
failed to load source for dependency `pet`
Caused by:
Unable to update https://github.com/microsoft/python-environment-tools.git?rev=ffcbf3f28c46633abd5448a52b1f396c322e0d6c#ffcbf3f2
Caused by:
path too long: 'C:/Users/runneradmin/.cargo/git/checkouts/python-environment-tools-903993894b37a7d2/ffcbf3f/crates/pet-conda/tests/unix/conda_env_without_manager_but_found_in_history/some_other_location/conda_install/conda-meta/python-fastjsonschema-2.16.2-py310hca03da5_0.json'; class=Filesystem (30)
要修复此问题,请为 Git 和 Windows 两者启用长路径支持。
对于 Git: git config --system core.longpaths true
对于 Windows,请使用此 PS 命令:
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force
详情请参阅 win32 文档
(启用长路径支持后需要重启系统。)
图形问题
Zed 无法启动
Zed 在 Windows 上目前使用 Vulkan 作为图形 API。如果 Zed 无法启动,Vulkan 往往是常见原因。
你可以查看 Zed 的日志文件:
C:\Users\YOU\AppData\Local\Zed\logs\Zed.log
如果看到类似以下的报错:
Zed failed to open a window: NoSupportedDeviceFoundERROR_INITIALIZATION_FAILEDGPU CrashedERROR_SURFACE_LOST_KHR
说明系统上的 Vulkan 可能没有正常工作。更新 GPU 驱动通常可以解决这个问题。
如果日志里没有任何 Vulkan 相关的内容,而你恰好安装了 Bandicam,可以尝试卸载它——Zed 目前与 Bandicam 不兼容。