rust-npm-publish
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust npm Publishing
Rust 二进制文件发布至 npm
Distribute Rust binaries to npm using the platform package pattern.
optionalDependencies使用平台包模式将Rust二进制文件分发至npm。
optionalDependenciesWhen to Use This Skill
适用场景
Activate when any of the following are true:
- Publishing Rust binaries to npm or debugging publish pipeline failures
- Setting up ,
publish.config.ts, or platform package manifestsbin.js - Managing version sync across workspace packages and
Cargo.toml - Working with protocol or prepare/restore scripts
workspace:* - Scripts matching ,
*publish*,*platform*, or*version*exist*sync*
当满足以下任一条件时启用:
- 将Rust二进制文件发布到npm,或排查发布流水线失败问题
- 配置、
publish.config.ts或平台包清单bin.js - 管理工作区包与之间的版本同步
Cargo.toml - 使用协议或prepare/restore脚本
workspace:* - 存在匹配、
*publish*、*platform*或*version*的脚本*sync*
Decision Tree
决策树
What does the user need?
Setting up publishing for the first time?
→ The Pattern section + Configuration + Templates
Running the publish pipeline?
→ Publish Pipeline section
→ Full details: references/publish-pipeline.md
Adding a new platform?
→ Adding a New Platform section
→ Platform reference: references/platform-matrix.md
Version sync / bump?
→ Versioning section
→ Full strategy: references/version-strategy.md
→ workspace:* details: references/workspace-protocol.md
Debugging a failure?
→ references/troubleshooting.md用户需求是什么?
首次搭建发布流程?
→ 模式章节 + 配置 + 模板
执行发布流水线?
→ 发布流水线章节
→ 详细内容:references/publish-pipeline.md
新增平台支持?
→ 新增平台章节
→ 平台参考:references/platform-matrix.md
版本同步/更新?
→ 版本管理章节
→ 完整策略:references/version-strategy.md
→ workspace:*详情:references/workspace-protocol.md
排查失败问题?
→ references/troubleshooting.mdThe Pattern
模式说明
@scope/my-tool ← main package (thin JS wrapper + bin.js)
├── optionalDependencies:
│ ├── @scope/my-tool-darwin-arm64 ← macOS ARM (M-series)
│ ├── @scope/my-tool-darwin-x64 ← macOS Intel
│ ├── @scope/my-tool-linux-x64 ← Linux x86_64
│ └── @scope/my-tool-windows-x64 ← Windows x86_64Each platform package contains only the pre-compiled binary for that target.
npm installs only the one matching the user's OS/CPU at install time.
Same approach used by SWC, Turbopack, esbuild, and similar tools.
@scope/my-tool ← 主包(轻量JS封装 + bin.js)
├── optionalDependencies:
│ ├── @scope/my-tool-darwin-arm64 ← macOS ARM(M系列)
│ ├── @scope/my-tool-darwin-x64 ← macOS Intel
│ ├── @scope/my-tool-linux-x64 ← Linux x86_64
│ └── @scope/my-tool-windows-x64 ← Windows x86_64每个平台包仅包含对应目标平台的预编译二进制文件。npm会在安装时仅安装匹配用户操作系统/CPU的包。
该方法被SWC、Turbopack、esbuild等工具采用。
Configuration
配置
Each repo provides a (see examples/):
publish.config.tstypescript
export default {
scope: '@myorg',
binaries: [{ name: 'my-cli', scope: 'cli', cargoPackage: 'my-cli-rs' }],
platforms: ['darwin-x64', 'darwin-arm64', 'linux-x64', 'windows-x64'],
mainPackages: [{ path: 'packages/cli', name: 'my-cli' }],
cargoWorkspace: 'Cargo.toml',
repositoryUrl: 'https://github.com/myorg/my-project',
};每个仓库需提供(参见examples/):
publish.config.tstypescript
export default {
scope: '@myorg',
binaries: [{ name: 'my-cli', scope: 'cli', cargoPackage: 'my-cli-rs' }],
platforms: ['darwin-x64', 'darwin-arm64', 'linux-x64', 'windows-x64'],
mainPackages: [{ path: 'packages/cli', name: 'my-cli' }],
cargoWorkspace: 'Cargo.toml',
repositoryUrl: 'https://github.com/myorg/my-project',
};Publish Pipeline
发布流水线
sync-versions → generate-manifests → add-platform-deps → copy-binaries
→ validate-binaries → prepare-publish → validate-workspace → publish-platforms
→ wait-propagation → publish-main → restore-packagesCritical ordering: Platform packages MUST be published and propagated before
main packages, because main packages reference them as .
optionalDependenciesSee references/publish-pipeline.md for step-by-step details.
sync-versions → generate-manifests → add-platform-deps → copy-binaries
→ validate-binaries → prepare-publish → validate-workspace → publish-platforms
→ wait-propagation → publish-main → restore-packages关键顺序:平台包必须在主包之前发布并完成同步,因为主包将它们作为引用。
optionalDependencies详细步骤参见references/publish-pipeline.md。
Main Package Wrapper (bin.js
)
bin.js主包封装(bin.js)
The main npm package is a thin JS wrapper — resolves the platform binary and spawns it.
See templates/wrapper/ for the template.
bin.jsKey details:
- returns
process.platform(notwin32) — mapwindows→win32-x64@scope/cli-windows-x64 - Use to find platform package, then read
require.resolve('pkg/package.json')for binary namemain - Use and forward exit codes from the Rust process
execFileSync - Main package has NO /
osfields — it installs everywhere. Only platform packages use those.cpu
主npm包是一个轻量JS封装——负责解析平台二进制文件并启动它。模板参见templates/wrapper/。
bin.js核心细节:
- 返回
process.platform(而非win32)——需将windows映射为win32-x64@scope/cli-windows-x64 - 使用查找平台包,然后读取
require.resolve('pkg/package.json')字段获取二进制文件名main - 使用并转发Rust进程的退出码
execFileSync - 主包不包含/
os字段——它可在所有环境安装。仅平台包使用这些字段。cpu
Platform Package Manifests
平台包清单
Each platform package needs / fields and a for chmod:
oscpupostinstall.jsjson
{
"name": "@scope/cli-darwin-arm64",
"os": ["darwin"],
"cpu": ["arm64"],
"main": "my-cli"
}每个平台包需要/字段以及用于修改权限的:
oscpupostinstall.jsjson
{
"name": "@scope/cli-darwin-arm64",
"os": ["darwin"],
"cpu": ["arm64"],
"main": "my-cli"
}Platform Matrix
平台矩阵
| Platform | Rust Target | | | Binary Extension |
|---|---|---|---|---|
| | | | (none) |
| | | | (none) |
| | | | (none) |
| | | | |
See references/platform-matrix.md for adding new platforms.
| 平台 | Rust 目标 | | | 二进制扩展名 |
|---|---|---|---|---|
| | | | 无 |
| | | | 无 |
| | | | 无 |
| | | | |
新增平台参见references/platform-matrix.md。
Adding a New Platform
新增平台支持
- Add to array in
platformspublish.config.ts - Add Rust target:
rustup target add <target-triple> - Add to CI matrix in publish workflow
- Regenerate manifests:
pnpm tsx scripts/generate-platform-manifests.ts
- 在的
publish.config.ts数组中添加平台platforms - 添加Rust目标:
rustup target add <target-triple> - 在发布工作流的CI矩阵中添加平台
- 重新生成清单:
pnpm tsx scripts/generate-platform-manifests.ts
Versioning
版本管理
Root is the single source of truth. Never manually edit version elsewhere.
package.jsonroot package.json (version: "0.2.15")
├── packages/cli/package.json → 0.2.15 (via sync-versions.ts)
├── Cargo.toml → 0.2.15
└── platform-packages/*/ → 0.2.15 (via generate-manifests)根目录是唯一版本数据源。切勿手动修改其他位置的版本号。
package.jsonroot package.json (version: "0.2.15")
├── packages/cli/package.json → 0.2.15 (通过sync-versions.ts同步)
├── Cargo.toml → 0.2.15
└── platform-packages/*/ → 0.2.15 (通过generate-manifests生成)Version Sync Flow
版本同步流程
bash
npm version patch # Bump root: 0.2.15 → 0.2.16
pnpm tsx scripts/sync-versions.ts # Propagate to all packages + Cargo.tomlbash
npm version patch # 更新根版本:0.2.15 → 0.2.16
pnpm tsx scripts/sync-versions.ts # 同步至所有包 + Cargo.tomlDev Builds
开发构建版本
CI computes: →
Published with . Install:
0.2.150.2.16-dev.{github_run_id}--tag devnpm install my-cli@devCargo.toml stays at base version () for dev builds — Cargo pre-release handling differs from npm.
0.2.15CI会自动生成: →
使用发布。安装命令:
0.2.150.2.16-dev.{github_run_id}--tag devnpm install my-cli@dev开发构建时,Cargo.toml保持基础版本()——Cargo的预发布版本处理逻辑与npm不同。
0.2.15Version Bump Guide
版本更新指南
| Change | Command | Example |
|---|---|---|
| Breaking API | | 1.0.0 → 2.0.0 |
| New feature | | 0.2.0 → 0.3.0 |
| Bug fix | | 0.2.15 → 0.2.16 |
| CI/testing | Automatic (CI) | 0.2.15 → 0.2.16-dev.123 |
| 变更类型 | 命令 | 示例 |
|---|---|---|
| 破坏性API变更 | | 1.0.0 → 2.0.0 |
| 新增功能 | | 0.2.0 → 0.3.0 |
| Bug修复 | | 0.2.15 → 0.2.16 |
| CI/测试版本 | 自动生成(CI) | 0.2.15 → 0.2.16-dev.123 |
workspace:* Protocol
workspace:*协议
pnpm uses for internal deps during development. Replace before publish, restore after:
workspace:*bash
pnpm tsx scripts/prepare-publish.ts # Replace workspace:* → real versions
pnpm tsx scripts/validate-no-workspace-protocol.ts # Safety gate
npm publish
pnpm tsx scripts/restore-packages.ts # Restore workspace:*See references/workspace-protocol.md for details.
See references/version-strategy.md for the full strategy.
pnpm在开发阶段使用作为内部依赖标识。发布前需替换为实际版本,发布后恢复:
workspace:*bash
pnpm tsx scripts/prepare-publish.ts # 将workspace:*替换为实际版本
pnpm tsx scripts/validate-no-workspace-protocol.ts # 安全检查
npm publish
pnpm tsx scripts/restore-packages.ts # 恢复workspace:*详情参见references/workspace-protocol.md。
完整策略参见references/version-strategy.md。
Troubleshooting Quick Reference
故障排查速查
| Problem | Likely Cause | Fix |
|---|---|---|
| Missing platform in bin.js | Add platform key mapping |
| Platform pkg not found on npm | Registry propagation delay | Wait; check publish logs |
| prepare-publish didn't run | Run prepare-publish.ts |
| Binary not executable | postinstall didn't run | |
| Version mismatch | Forgot to sync | Run sync-versions.ts |
See references/troubleshooting.md for detailed diagnostics.
| 问题 | 可能原因 | 修复方案 |
|---|---|---|
| bin.js中缺少平台映射 | 添加平台键映射 |
| 平台包在npm上找不到 | 注册表同步延迟 | 等待一段时间;检查发布日志 |
发布包中存在 | 未执行prepare-publish | 运行prepare-publish.ts |
| 二进制文件无执行权限 | postinstall未运行 | 执行 |
| 版本不匹配 | 忘记同步版本 | 运行sync-versions.ts |
详细诊断参见references/troubleshooting.md。
Templates
模板
| Directory | Contents |
|---|---|
| templates/scripts/ | All 11 publish pipeline scripts |
| templates/wrapper/ | |
| examples/ | Real |
| 目录 | 内容 |
|---|---|
| templates/scripts/ | 所有11个发布流水线脚本 |
| templates/wrapper/ | |
| examples/ | 来自实际项目的 |
Setup & Activation
安装与激活
bash
npx skills add -g onsager-ai/dev-skills --skill rust-npm-publish -a claude-code -yAuto-activates when: present, scripts matching or exist, user mentions "publish", "platform packages", or "version sync".
publish.config.ts*publish**platform*bash
npx skills add -g onsager-ai/dev-skills --skill rust-npm-publish -a claude-code -y自动激活条件:存在、匹配或的脚本,或用户提及"publish"、"platform packages"、"version sync"。
publish.config.ts*publish**platform*