# 锁文件

锁文件是由包管理器生成的文件，记录软件项目每个依赖项解析后的确切版本，包括传递依赖，以便后续安装时无论上游注册表如何变化或有新版本发布，都能复现同样的依赖集合。<sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup> 典型例子包括 npm 的 `package-lock.json`、Yarn 的 `yarn.lock`、pnpm 的 `pnpm-lock.yaml`、[Rust](https://www.edgepedia.org/zh/rust-programming-language) 的 Cargo 的 `Cargo.lock`、Ruby 的 Bundler 的 `Gemfile.lock`，以及 Python 的 Poetry 的 `poetry.lock`。<sup>[2](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup><sup> • </sup><sup>[3](https://classic.yarnpkg.com/en/docs/yarn-lock)</sup><sup> • </sup><sup>[4](https://fosdem.org/2026/events/attachments/E7BQKK-git_blame_for_your_dependencies/slides/267601/git-pkgs_h2tyqfk.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 内容 | 所有直接依赖和传递依赖的确切锁定版本、包产物的完整性哈希，以及每个包解析来源的注册表 URL<sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup><sup> • </sup><sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup> |
| 用途 | 确定性安装、对下载包进行完整性验证、跳过重复解析以加快安装速度，以及可审查的依赖变更差异<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup><sup> • </sup><sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup> |
| 冻结安装 | `npm ci` 严格依据锁文件安装，若 `package.json` 与锁文件不一致则安装失败；Yarn Classic 使用 `--frozen-lockfile`，Yarn Berry 使用 `--immutable`，pnpm 使用 `--frozen-lockfile`<sup>[7](https://dependencywatch.io/guides/lockfile-security-best-practices)</sup> |
| 提交实践 | 应用程序的锁文件提交到版本控制；库的锁文件会被依赖它的应用程序忽略，因此 Bundler 建议应用程序提交锁文件，但不要在发布的 gem 中包含它<sup>[3](https://classic.yarnpkg.com/en/docs/yarn-lock)</sup><sup> • </sup><sup>[8](https://bundler.io/guides/rationale.html)</sup> |
| 采用情况 | 在一项针对七个包管理器的研究中，截至 2025 年 3 月 28 日，81.4% 的抽样项目提交了锁文件<sup>[9](https://buildstackreview.com/posts/lockfile-strategies-for-reproducible-builds-across-go-and-rust-projects)</sup> |
| 局限 | 锁文件哈希覆盖的是下载的 tarball，而非安装后的文件；锁文件本身无法检测在解析阶段引入的恶意包<sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup><sup> • </sup><sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup> |

## 锁文件包含什么

锁文件是完整解析依赖图的确定性快照。对于每个包，它记录了解析时选定的确切版本、包管理器获取该包的注册表或源 URL，以及下载产物的密码学完整性哈希。<sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup> 每个管理器有自己的哈希风格：npm v3 在 `integrity` 字段中保存 SHA-512 摘要，按 W3C 子资源完整性方案编码（`sha512-<base64>`），Yarn Berry 也是如此；Yarn Classic 则以十六进制形式保存 SHA-1 摘要，而 pnpm 在记录 SHA-512 的同时还记录完整的 tarball URL。<sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup>

完整性校验的工作方式与 HTML `<script>` 和 `<link>` 元素上的 `integrity` 属性相同：包管理器计算所接收字节的摘要，并与记录值比对。无论不匹配是源于被入侵的注册表、中间人网络还是被篡改的本地缓存，都必须中止安装。<sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup>

## 包管理器如何使用锁文件

**npm。** 任何修改 `node_modules` 树或 `package.json` 的操作都会自动生成 `package-lock.json`，它描述确切的树结构，使后续安装生成完全相同的树。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup> npm 文档列出了四个目的：依赖树的单一表示，使队友、部署环境和持续集成安装相同的依赖；一种“时间旅行”到 `node_modules` 先前状态的方式，无需提交该目录；可读的版本控制差异，展示树的变化；以及通过跳过重复的元数据解析来加快安装。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup> 自 npm v7 起，锁文件携带足够的信息，无需读取 `package.json` 文件即可描绘整个包树，npm 还在 `node_modules/.package-lock.json` 维护一个“隐藏”锁文件，以避免重复处理树。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup> `lockfileVersion` 字段标记格式代际：1 对应 npm v5 和 v6，2 对应 npm v7 并带向后兼容，3 则不带这些兼容功能。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup> 一个相关文件 `npm-shrinkwrap.json` 格式相同但可以发布，因此它为已发布包的消费者锁定树结构；若两者同时存在，shrinkwrap 文件优先。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup>

**Yarn。** Yarn 将解析后的版本存储在项目根目录的 `yarn.lock` 中。安装时它只使用顶层锁文件，忽略依赖内部的任何 `yarn.lock` 文件，该文件自动生成，应由 Yarn 处理而非手动编辑。<sup>[3](https://classic.yarnpkg.com/en/docs/yarn-lock)</sup>

**Bundler。** `bundle install` 将所有 gem 的完整名称和版本（包括 gem 的依赖）持久化到 `Gemfile.lock` 中，之后每次安装都复用这些版本而不再重新解析。<sup>[10](https://guides.rubygems.org/gemfile-lock/)</sup> Bundler 的指导是：应用程序应提交锁文件；对于库，锁文件不属于包的一部分，当 gem 作为依赖被安装时会被忽略，因为锁定库的开发环境会缩窄其测试实际覆盖的依赖版本范围。<sup>[8](https://bundler.io/guides/rationale.html)</sup>

**Cargo。** Rust 依赖解析的结果存储在 `Cargo.lock` 中，它使依赖随时间保持固定，并在不同时间和不同系统上提供确定性构建；`cargo new` 默认将该文件纳入版本控制。<sup>[11](https://doc.rust-lang.org/cargo/reference/resolver.html)</sup><sup> • </sup><sup>[12](https://doc.rust-lang.org/cargo/faq.html?highlight=why+do+binaries+have+cargo.lock+but+not)</sup>

**Python 和 Java。** 当 `poetry.lock` 文件存在时，Poetry 安装其中列出的确切版本，确保参与项目的每个人都使用一致的版本。<sup>[13](https://python-poetry.org/docs/basic-usage/)</sup> Maven 历史上缺少锁文件，因此 Maven-Lockfile 项目添加了一个锁文件，捕获所有直接和传递依赖及其校验和，以最少的配置实现 Java 项目的高完整性和可复现构建。<sup>[14](https://arxiv.org/abs/2510.00730)</sup>

## 锁文件与供应链安全

锁文件是可复现构建和软件供应链完整性的基础性控制手段。从锁文件安装而非每次构建都重新解析，减少了不确定性，也更容易判断某个依赖变更是有意引入的还是上游注册表更新带来的副作用。<sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup> 它们还服务于事件响应：如果团队无法证明安装了哪些依赖版本和包内容，就无法在恶意更新或漏洞披露后可靠地评估暴露面。<sup>[15](https://nhimg.org/articles/lockfiles-are-the-control-point-for-supply-chain-incident-response/)</sup> 由于锁文件同时捕获传递依赖和直接依赖，审计人员可以利用已提交的锁文件重建某个发布版本的确切依赖集合，用于漏洞分析、许可证审查和软件物料清单生成。<sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup>

**CI 中的强制执行。** 已提交的锁文件将依赖变更转化为有意的、可审查的事件：只有当有人更新锁文件且该差异经过审查时，树才会变化。<sup>[7](https://dependencywatch.io/guides/lockfile-security-best-practices)</sup> 冻结安装模式强制执行这一点：`npm ci` 在清单与锁文件不一致时失败，而 `npm install` 会重写锁文件；Yarn 和 pnpm 提供等效的标志。<sup>[7](https://dependencywatch.io/guides/lockfile-security-best-practices)</sup> 欧盟网络安全局（ENISA）关于包管理器的技术通告建议使用 `package-lock.json` 或 `yarn.lock` 等锁文件来维持确切的依赖版本，同时警告如果不定期审查和应用更新，固定版本可能延长对已知漏洞的暴露时间。<sup>[16](https://www.enisa.europa.eu/sites/default/files/2025-12/ENISA%20Technical%20Advisory%20-%20Package_Managers_v_0.8_draft.pdf)</sup> 包管理器也已加强哈希强制执行：pnpm 11.4 默认将 tarball 完整性不匹配视为硬性安装失败，并提供范围狭窄的 `--update-checksums` 选项作为例外。<sup>[17](https://pnpm.io/blog/releases/11.4?trk=article-ssr-frontend-pulse_x-social-details_comments-action_comment-text)</sup> Bundler 2.6 于 2024 年 12 月发布，增加了记录在 `Gemfile.lock` 本身内部的可选校验和验证，填补了锁文件锁定版本但不防止该版本来源被篡改的空白。<sup>[18](https://bundler.io/blog/2024/12/19/bundler-v2-6.html)</sup>

## 固定版本与锁定

锁文件不同于在清单中固定版本。固定意味着在 `package.json` 或等效清单中写入确切的版本约束；锁文件则冻结整个解析后的图，包括没有单个项目直接控制的传递依赖。<sup>[19](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)</sup> 卡内基梅隆大学的何浩（Hao He）、博格丹·瓦西莱斯库（Bogdan Vasilescu）和克里斯蒂安·凯斯特纳（Christian Kästner）在 2025 年的一项研究中模拟了 npm 在历史时间点上的依赖解析，发现固定直接依赖会增加过时和易受攻击依赖的维护成本，并且反直觉地，在较大的依赖图中增加对恶意包更新的暴露，因为固定直接依赖可能让图中更多的部分处于浮动状态。在直接和传递依赖达到 498 个或以上的图中，这一效应逆转了预期的安全收益，该条件影响其数据集中 26% 的 GitHub 仓库。<sup>[19](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)</sup> 作者的建议是，应用程序开发者应使用共享锁文件冻结整个依赖图，而非固定单个依赖。<sup>[19](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)</sup>

## 局限

锁文件有其完整性保证无法覆盖的结构性局限。它仅限于应用程序，即依赖图的根：作为依赖发布的包无法将其锁定决策传递给下游，这使得锁文件对库的用途有限。<sup>[19](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)</sup> 在 npm 中，这由发布规则强制执行，因为 `package-lock.json` 不能发布。<sup>[6](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)</sup> 锁文件也不是为手动维护而设计的，因此清单中的浮动约束在锁文件重新生成时仍会导致自动更新。<sup>[19](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)</sup>

记录的哈希有其自身范围。锁文件哈希覆盖的是下载的 tarball，而非安装后的文件，因此交付给浏览器的资源需要对最终打包输出单独进行哈希处理。<sup>[5](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)</sup> 而且锁文件无法独立检测在解析阶段引入的恶意包；其完整性保证的强度仅取决于所用包管理器的哈希验证行为。<sup>[1](https://appsecuritystandards.org/glossary/lockfiles)</sup> 锁文件也不描述整个开发环境：运行时和包管理器版本、系统包以及本地服务可能不在其中，因此仅凭锁定的依赖图不能保证逐比特可复现的构建。<sup>[20](https://scalableweb.dev/appendices/reproducible-environments/)</sup>

## 各生态系统的设计差异

约格亚·加马格（Yogya Gamage）、迪皮卡·蒂瓦里（Deepika Tiwari）、马丁·蒙佩鲁斯（Martin Monperrus）和伯努瓦·博德里（Benoit Baudry）在 2025 年的一项研究中首次对七个包管理器的锁文件进行了全面比较：npm、pnpm、Cargo、Poetry、Pipenv、Gradle 和 Go，结合了源码和文档分析与对 15 名开发者的访谈。<sup>[21](https://arxiv.org/abs/2505.04834)</sup> 研究发现设计差异很大。Cargo 和 Go 的锁文件被判定为功能完整，npm 锁文件因元数据过多而趋于冗长，Gradle 锁文件则遗漏了校验和等强制性细节。各管理器在解析时对锁定版本的处理也不同：Cargo 和 Pipenv 在解析时忽略它们，npm 和 pnpm 在它们与规格文件不冲突时使用它们，而 Poetry 严格执行锁文件，检测到冲突即停止构建。<sup>[22](https://www.researchgate.net/publication/391575097_The_Design_Space_of_Lockfiles_Across_Package_Managers)</sup> 研究中受访的开发者报告了可复现性和审查方面的好处，但也指出了维护负担、间接依赖锁定的局限以及依赖更新延迟等挑战。<sup>[21](https://arxiv.org/abs/2505.04834)</sup>

## References

1. [Lockfiles, Application Security Standards glossary](https://appsecuritystandards.org/glossary/lockfiles)
2. [Lockfile Mapping & Analysis, SRI Guide](https://subresource-integrity.com/supply-chain-auditing-dependency-verification/lockfile-mapping-analysis/)
3. [yarn.lock, Yarn Classic documentation](https://classic.yarnpkg.com/en/docs/yarn-lock)
4. [git-pkgs: manifest and lockfile filenames per ecosystem, FOSDEM 2026 slides](https://fosdem.org/2026/events/attachments/E7BQKK-git_blame_for_your_dependencies/slides/267601/git-pkgs_h2tyqfk.pdf)
5. [package-lock.json, npm Docs (v7)](https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json/)
6. [Lockfile security best practices for npm, Yarn, pnpm and pip, DependencyWatch.io](https://dependencywatch.io/guides/lockfile-security-best-practices)
7. [How to manage dependencies with Bundler, RubyGems Guides](https://bundler.io/guides/rationale.html)
8. [How Gemfile.lock works, RubyGems Guides](https://guides.rubygems.org/gemfile-lock/)
9. [Lockfile Strategies for Reproducible Builds Across Go and Rust Projects, Build Stack Review](https://buildstackreview.com/posts/lockfile-strategies-for-reproducible-builds-across-go-and-rust-projects)
10. [Dependency Resolution, The Cargo Book](https://doc.rust-lang.org/cargo/reference/resolver.html)
11. [FAQ, The Cargo Book](https://doc.rust-lang.org/cargo/faq.html?highlight=why+do+binaries+have+cargo.lock+but+not)
12. [Basic usage, Poetry documentation](https://python-poetry.org/docs/basic-usage/)
13. [Maven-Lockfile: High Integrity Rebuild of Past Java Releases, arXiv:2510.00730](https://arxiv.org/abs/2510.00730)
14. [Lockfiles are the control point for supply chain incident response, NHIMG](https://nhimg.org/articles/lockfiles-are-the-control-point-for-supply-chain-incident-response/)
15. [ENISA Technical Advisory for Secure Use of Package Managers (draft)](https://www.enisa.europa.eu/sites/default/files/2025-12/ENISA%20Technical%20Advisory%20-%20Package_Managers_v_0.8_draft.pdf)
16. [pnpm 11.4 release notes, pnpm blog](https://pnpm.io/blog/releases/11.4?trk=article-ssr-frontend-pulse_x-social-details_comments-action_comment-text)
17. [Bundler v2.6: lockfile checksums are finally there, RubyGems Blog](https://bundler.io/blog/2024/12/19/bundler-v2-6.html)
18. [Pinning Is Futile: You Need More Than Local Dependency Versioning to Defend against Supply Chain Attacks, He, Vasilescu, and Kästner, FSE 2025](https://www.cs.cmu.edu/~ckaestne/pdf/fse25.pdf)
19. [Reproducible development environments, Elements of Scalable Web Development](https://scalableweb.dev/appendices/reproducible-environments/)
20. [The Design Space of Lockfiles Across Package Managers, Gamage, Tiwari, Monperrus, and Baudry, arXiv:2505.04834](https://arxiv.org/abs/2505.04834)
21. [The Design Space of Lockfiles Across Package Managers (full text), ResearchGate](https://www.researchgate.net/publication/391575097_The_Design_Space_of_Lockfiles_Across_Package_Managers)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/lockfile

English original: https://www.edgepedia.org/lockfile
