# 依赖地狱

**依赖地狱**（Dependency hell）是软件开发中的一种俗称，指已安装的软件包对其他软件包的特定版本存在相互冲突、互不兼容的依赖，导致软件难以构建、安装或运行。<sup>[1](https://en.wiktionary.org/wiki/dependency_hell)</sup> 该词起源于共享库时代，在 Windows 和 Java 世界中分别又称 **DLL 地狱**（DLL hell）和**类路径地狱**（classpath hell）。<sup>[2](https://www.techtarget.com/it-infrastructure/definition/dependency-hell)</sup> 其常见后果包括软件行为异常、出现缺陷、运行或安装软件时报出错误信息，或者软件彻底失效。<sup>[2](https://www.techtarget.com/it-infrastructure/definition/dependency-hell)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 对其他软件包特定版本存在相互冲突、互不兼容的依赖<sup>[1](https://en.wiktionary.org/wiki/dependency_hell)</sup> |
| 其他名称 | DLL 地狱、类路径地狱、JAR 地狱<sup>[2](https://www.techtarget.com/it-infrastructure/definition/dependency-hell)</sup> |
| 典型成因 | 同一库的版本冲突、过深的传递依赖链、对实际使用情况可见性差、跨环境的版本不一致<sup>[3](https://www.sonatype.com/resources/articles/what-are-software-dependencies)</sup> |
| 版本锁定 | 若依赖声明过于严格，不发布所有依赖于该包的软件包的新版本就无法升级该包<sup>[4](https://www.rd-alliance.org/system/files/RDA%20Data%20Versioning%20WG%20Use%20Cases.pdf)</sup> |
| 算法代价 | 在 Cargo 式约束下，版本选择等价于求解布尔可满足性；Go 的最小版本选择则以线性时间运行<sup>[5](https://go.dev/blog/versioning-proposal)</sup> |
| 主要对策 | 带解析器的包管理器、锁文件、多版本并存、按应用隔离依赖<sup>[2](https://www.techtarget.com/it-infrastructure/definition/dependency-hell)</sup><sup> • </sup><sup>[6](https://learn.microsoft.com/windows/win32/sbscs/about-side-by-side-assemblies-)</sup> |

## 成因

当软件的依赖没有被良好地定义或管理，以致对某一依赖的改动会意外破坏其他依赖时，依赖地狱便出现了。据 Sonatype 称，依赖地狱通常有四个成因：应用的不同部分需要同一个库的互不兼容版本；依赖链过长或不透明，含有大量传递依赖；不清楚哪些依赖实际被使用；以及开发、测试与生产环境之间的依赖版本不一致。<sup>[3](https://www.sonatype.com/resources/articles/what-are-software-dependencies)</sup>

第二个成因维度是声明风格。若依赖声明过于严格，项目就会遭遇**版本锁定**，即不发布每个依赖于该包的软件包的新版本就无法升级该包；若声明过于宽松，构建又会变得不可复现。<sup>[4](https://www.rd-alliance.org/system/files/RDA%20Data%20Versioning%20WG%20Use%20Cases.pdf)</sup>

## 历史形态

**DLL 地狱。** 在 Windows 上，经典形态源自共享的动态链接库：安装一个应用可能替换掉另一个应用所依赖的 DLL，使其损坏。微软的补救措施是并行部署（自 [Windows 2000](https://www.edgepedia.org/zh/windows-2000) 起支持，做法是把 DLL 放在应用程序文件夹中），以及 Windows 文件保护，它防止系统 DLL 被未经授权的代理覆盖。<sup>[7](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)</sup> 从 [Windows XP](https://www.edgepedia.org/zh/windows-xp) 开始，并行程序集允许多个版本的 COM 或 Windows 程序集同时运行；清单文件声明应用依赖哪个版本，加载器据此绑定，从而降低 DLL 版本冲突的可能性。<sup>[6](https://learn.microsoft.com/windows/win32/sbscs/about-side-by-side-assemblies-)</sup>

**JAR 地狱与类路径地狱。** 在 Java 虚拟机上，当类路径上存在某个库的多个版本时，JVM 只加载其中一个版本，其余版本被遮蔽；研究者把这种遮蔽称为依赖冲突，它可以在不报任何错误的情况下改变程序的语义。<sup>[8](https://arxiv.org/abs/2006.07633)</sup>

**Linux 包管理。** 在 Debian 系系统中，当 dpkg 在升级过程中漏掉包之间的细微关系，或安装中途失败、某个包已解包而依赖它的包尚未安装时，就会出现损坏状态。`apt-get -f` 选项让 APT 自行推断解决方案并继续，但严重损坏的系统可能需要用 dpkg 配合强制选项手动修复。<sup>[9](https://www.debian.org/doc/manuals/apt-guide/ch4.en.html)</sup>

## 解析器如何应对

现代包管理器借助自动化的**依赖解析**（dependency resolution）来对抗依赖地狱，即选择满足所有已声明约束的版本的过程。<sup>[10](https://doc.rust-lang.org/cargo/reference/resolver.html)</sup> 各家策略不同，而这直接关系到冲突发生的频率。

**带回溯的约束求解。** Cargo 的解析器在约束与启发式规则下运作，寻找一个普遍适用的解析结果，若找不到满足约束的图则返回错误；其算法偏好包的最新可用版本，这可能与较旧的要求产生冲突，且在这种情况下它不会尝试对同一个 crate 的两份副本去重。<sup>[10](https://doc.rust-lang.org/cargo/reference/resolver.html)</sup> pip 的解析器自 20.3 版起支持回溯，它在解析过程中对包版本作出假设，当先前的假设被证明有误时，就丢弃已有工作并另选路径。<sup>[11](https://pip.pypa.io/en/stable/topics/dependency-resolution/)</sup> .NET 的 [NuGet](https://www.edgepedia.org/zh/nuget) 对旁系依赖采用最低适用版本规则，当精确版本固定与最低版本之类的要求无法同时满足时便报错。<sup>[12](https://learn.microsoft.com/en-us/nuget/concepts/dependency-resolution)</sup> 对 Perl 的 cpanm 与 CPAN.pm 的学术分析发现，它们临时的解析方式既不正确也不完备；而 Carton 在 cpanm 之上增加了锁文件支持，可在不改变底层算法的前提下实现可复现的安装。<sup>[13](https://github.com/ecosyste-ms/package-manager-resolvers)</sup>

**最小版本选择。** Go 走了另一条路。Go 团队成员、vgo 原型的创造者拉斯·考克斯（Russ Cox）指出，Cargo 与 Dep 的约束使版本选择等价于求解布尔可满足性，代价可能非常高昂；而改为依赖导入兼容性，则可以换来一个简单、线性时间的算法，它总能找到唯一的最佳配置。<sup>[5](https://go.dev/blog/versioning-proposal)</sup> Go 的最小版本选择（Minimal Version Selection）遍历模块图，追踪每个模块被要求的最高版本；由此得到的构建列表是满足所有要求的最小版本集合，它是确定性的，不会因新依赖版本的发布而改变，而且与大多数系统不同，它不会被保存到锁文件中。<sup>[14](https://go.dev/ref/mod)</sup>

**锁文件与可复现构建。** 锁文件记录依赖图中每个包（无论直接还是传递）的确切版本与密码学哈希，使构建成为一个函数：相同的输入在每台机器上都产生相同的输出。<sup>[15](https://software-engineering-guide.github.io/chapters/02-18-dependency-and-supply-chain-management/)</sup> Cargo 把解析结果存入 `Cargo.lock`，将依赖锁定到特定版本并长期保持不变。<sup>[10](https://doc.rust-lang.org/cargo/reference/resolver.html)</sup> 当出现菱形冲突时，推荐的做法是有意识地解决它：升级落后的那一方，固定一个覆盖版本，或者舍弃某个其要求无法被满足的依赖。<sup>[15](https://software-engineering-guide.github.io/chapters/02-18-dependency-and-supply-chain-management/)</sup>

**生态层面的修复。** npm 通过 `peerDependencies` 表达插件兼容性；在 npm 3 至 6 版中，这些依赖不会被自动安装，对无效的对等依赖版本只会发出警告；而自 npm v7 起，它们默认被安装，若依赖树无法解析，冲突的对等依赖要求会导致安装错误。<sup>[16](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)</sup> npm 的 `overrides` 字段改变既有依赖边的解析结果，`packageExtensions` 则在等待上游修复期间，为第三方包增补或修正依赖元数据。<sup>[17](https://docs.npmjs.com/cli/configuring-npm/package-json/)</sup>

## 规模与后果

问题随复用而放大。蒙斯大学研究软件依赖网络的研究者汤姆·门斯（Tom Mens）与亚历山大·德坎（Alexandre Decan）编纂的开源包注册表中依赖挑战目录，将依赖地狱描述为软件从业者经常面对的一种状况，它是开源复用的直接后果。<sup>[18](https://arxiv.org/abs/2409.18884)</sup> 一项针对 100 个使用 npm 的 [JavaScript](https://www.edgepedia.org/zh/javascript) 项目的实证研究发现，配置为运行时依赖的已安装依赖中有 59% 并未在生产中使用，而配置为开发依赖的依赖中有 28.2% 在生产中使用；作者以此作为证据，说明声明的依赖角色常常与实际使用不符。<sup>[19](https://arxiv.org/abs/2207.14711)</sup> 依赖决策还事关安全：一个薄弱的依赖选择可能影响构建完整性、暴露的攻击面，以及在层层嵌套的开源包与自动化构建流水线中补丁被采纳的速度。<sup>[20](https://nhimg.org/glossary/dependency-hell/)</sup>

对于“要求”本身，目前没有跨生态系统的标准。尽管语义化版本 2.0（Semantic Versioning 2.0）已成为版本声明的标准，但 npm、PyPI、RubyGems、Go 等每个生态都使用自己的要求语法与解析策略；一篇 2026 年的 ACM Queue 论文提出用通用的要求模型来统一它们，该模型抽离语法而保留语义。<sup>[21](https://research.google/pubs/modelling-version-requirements-in-open-source-packaging/)</sup>

## References

1. [dependency hell, Wiktionary](https://en.wiktionary.org/wiki/dependency_hell)
2. [What Is Dependency Hell? Definition from SearchITOperations, TechTarget](https://www.techtarget.com/it-infrastructure/definition/dependency-hell)
3. [What is a Software Dependency? Sonatype](https://www.sonatype.com/resources/articles/what-are-software-dependencies)
4. [RDA Data Versioning Working Group Use Cases](https://www.rd-alliance.org/system/files/RDA%20Data%20Versioning%20WG%20Use%20Cases.pdf)
5. [A Proposal for Package Versioning in Go, Russ Cox, The Go Blog](https://go.dev/blog/versioning-proposal)
6. [About Side-by-Side Assemblies, Microsoft Learn](https://learn.microsoft.com/windows/win32/sbscs/about-side-by-side-assemblies-)
7. [Dynamic link library (DLL) troubleshooting, Microsoft Learn](https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library)
8. [Will Dependency Conflicts Affect My Program's Semantics? arXiv](https://arxiv.org/abs/2006.07633)
9. [Chapter 4. The Interface, Debian APT User's Guide](https://www.debian.org/doc/manuals/apt-guide/ch4.en.html)
10. [Dependency Resolution, The Cargo Book](https://doc.rust-lang.org/cargo/reference/resolver.html)
11. [Dependency Resolution, pip documentation](https://pip.pypa.io/en/stable/topics/dependency-resolution/)
12. [NuGet Package Dependency Resolution, Microsoft Learn](https://learn.microsoft.com/en-us/nuget/concepts/dependency-resolution)
13. [package-manager-resolvers, ecosyste.ms](https://github.com/ecosyste-ms/package-manager-resolvers)
14. [Go Modules Reference, The Go Programming Language](https://go.dev/ref/mod)
15. [Dependency and supply-chain management, Software Engineering Guide](https://software-engineering-guide.github.io/chapters/02-18-dependency-and-supply-chain-management/)
16. [package.json, npm Docs (v11)](https://docs.npmjs.com/cli/v11/configuring-npm/package-json/)
17. [package.json, npm Docs (packageExtensions)](https://docs.npmjs.com/cli/configuring-npm/package-json/)
18. [An Overview and Catalogue of Dependency Challenges in Open Source Software Package Registries, Mens & Decan, arXiv](https://arxiv.org/abs/2409.18884)
19. [Not All Dependencies are Equal: An Empirical Study on Production Dependencies in NPM, arXiv](https://arxiv.org/abs/2207.14711)
20. [What Is Dependency Hell? Definition & Examples](https://nhimg.org/glossary/dependency-hell/)
21. [Modelling Version Requirements in Open Source Packaging, ACM Queue / Google Research](https://research.google/pubs/modelling-version-requirements-in-open-source-packaging/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: 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/dependency-hell

English original: https://www.edgepedia.org/dependency-hell
