dart-resolve-package-conflicts
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseManaging Dart Dependencies
Dart依赖管理
Contents
目录
Core Concepts
核心概念
Dart enforces a strict single-version rule for dependencies: a project and all its transitive dependencies must resolve to a single, shared version of any given package. This prevents runtime type mismatches but introduces the risk of "version lock."
To mitigate version lock, Dart relies on version constraints rather than pinned versions in the . The file maintains the exact resolved versions for reproducible builds.
pubspec.yamlpubspec.lockUnderstand the output columns of :
dart pub outdated- Current: The version currently recorded in .
pubspec.lock - Upgradable: The latest version allowed by the constraints in .
pubspec.yamlresolves to this.dart pub upgrade - Resolvable: The absolute latest version that can be resolved when factoring in all other dependencies in the project.
- Latest: The latest published version of the package (excluding prereleases).
Dart对依赖项实施严格的单版本规则:项目及其所有传递依赖项必须解析为任何给定包的单一共享版本。这可以防止运行时类型不匹配,但会带来“版本锁定”的风险。
为缓解版本锁定问题,Dart在中依赖版本约束而非固定版本。文件维护精确的已解析版本,以实现可复现的构建。
pubspec.yamlpubspec.lock理解的输出列:
dart pub outdated- **Current:**当前记录在中的版本。
pubspec.lock - Upgradable:中的约束允许的最新版本。
pubspec.yaml会解析到该版本。dart pub upgrade - **Resolvable:**考虑项目中所有其他依赖项时,可解析的绝对最新版本。
- **Latest:**包的最新发布版本(不包括预发布版本)。
Version Constraints
版本约束
- Use Caret Syntax: Always use caret syntax (e.g., ) for dependencies in
^1.2.3. This allowspubspec.yamlto select newer, non-breaking versions (up to, but not including, the next major version) during resolution.pub - Tighten Dev Dependencies: Set the lower bound of to the exact version currently used. This reduces resolution complexity and prevents older, incompatible dev tools from being selected.
dev_dependencies - Enforce Lockfiles in CI: Use in CI/CD pipelines to ensure the exact versions tested locally are used in production.
dart pub get --enforce-lockfile
- **使用脱字符语法:**在中始终为依赖项使用脱字符语法(例如:
pubspec.yaml)。这允许^1.2.3在解析过程中选择更新的、非破坏性的版本(直至但不包含下一个主版本)。pub - **收紧开发依赖项:**将的下限设置为当前使用的确切版本。这降低了解析复杂度,避免选择旧的、不兼容的开发工具。
dev_dependencies - **在CI中强制执行锁定文件:**在CI/CD流水线中使用,确保生产环境使用与本地测试完全相同的版本。
dart pub get --enforce-lockfile
Workflow: Auditing Dependencies
工作流:审计依赖项
Run this workflow periodically to identify stale packages that may impact stability or performance.
Task Progress:
- Run .
dart pub outdated - Review the Upgradable column to identify packages that can be updated without modifying .
pubspec.yaml - Review the Resolvable column to identify packages that require constraint modifications in to update.
pubspec.yaml - Identify any packages marked as retracted or discontinued.
定期运行此工作流,识别可能影响稳定性或性能的过时包。
任务进度:
- 运行。
dart pub outdated - 查看Upgradable列,识别无需修改即可更新的包。
pubspec.yaml - 查看Resolvable列,识别需要修改中的约束才能更新的包。
pubspec.yaml - 识别任何标记为撤回或已停止维护的包。
Workflow: Upgrading Dependencies
工作流:升级依赖项
Use conditional logic based on the audit results to upgrade dependencies.
Task Progress:
- If updating to "Upgradable" versions:
- Run .
dart pub upgrade - Run to automatically update the lower bounds in
dart pub upgrade --tightento match the newly resolved versions.pubspec.yaml
- Run
- If updating to "Resolvable" versions (Major updates):
- Manually edit to bump the version constraint to match the "Resolvable" column (e.g., change
pubspec.yamlto^0.11.0).^0.12.1 - Run to resolve the new constraints and update
dart pub upgrade.pubspec.lock
- Manually edit
- Feedback Loop:
- Run -> review errors -> fix breaking API changes.
dart analyze - Run -> review failures -> fix regressions.
dart test
- Run
根据审计结果使用条件逻辑升级依赖项。
任务进度:
- 如果更新到“Upgradable”版本:
- 运行。
dart pub upgrade - 运行,自动将
dart pub upgrade --tighten中的下限更新为匹配新解析的版本。pubspec.yaml
- 运行
- 如果更新到“Resolvable”版本(主版本更新):
- 手动编辑,将版本约束调整为匹配“Resolvable”列的版本(例如:将
pubspec.yaml改为^0.11.0)。^0.12.1 - 运行,解析新约束并更新
dart pub upgrade。pubspec.lock
- 手动编辑
- 反馈循环:
- 运行-> 查看错误 -> 修复破坏性API变更。
dart analyze - 运行-> 查看失败用例 -> 修复回归问题。
dart test
- 运行
Workflow: Resolving Version Conflicts
工作流:解决版本冲突
When cannot find a set of concrete versions that satisfy all constraints, or when dealing with a retracted package version, manipulate the lockfile surgically.
pubNEVER delete the entire file and run . This causes uncontrolled upgrades across the entire dependency graph.
pubspec.lockdart pub getTask Progress:
- Open .
pubspec.lock - Locate the specific YAML block for the conflicting or retracted package.
- Delete ONLY that package's entry from the lockfile.
- Run to fetch the newest compatible, non-retracted version for that specific package.
dart pub get - Feedback Loop:
- Run -> verify the dependency graph resolves correctly.
dart pub deps - If resolution fails, identify the transitive dependency causing the lock, update its constraint in , and retry.
pubspec.yaml
- Run
当无法找到满足所有约束的具体版本集,或处理已撤回的包版本时,需精准操作锁定文件。
pub切勿删除整个文件并运行。这会导致整个依赖图不受控制地升级。
pubspec.lockdart pub get任务进度:
- 打开。
pubspec.lock - 定位冲突或已撤回包的特定YAML块。
- 仅删除该包的条目。
- 运行,获取该特定包的最新兼容且未撤回的版本。
dart pub get - 反馈循环:
- 运行-> 验证依赖图是否正确解析。
dart pub deps - 如果解析失败,识别导致锁定的传递依赖项,更新其在中的约束,并重试。
pubspec.yaml
- 运行
Examples
示例
Tightening Constraints
收紧约束
When shows a package is resolvable to a higher minor/patch version, use the flag to update the automatically.
dart pub outdated--tightenpubspec.yamlInput ():
pubspec.yamlyaml
dependencies:
http: ^0.13.0Command:
bash
dart pub upgrade --tighten httpOutput ():
pubspec.yamlyaml
dependencies:
http: ^0.13.5当显示某个包可解析到更高的次版本/补丁版本时,使用标志自动更新。
dart pub outdated--tightenpubspec.yaml输入():
pubspec.yamlyaml
dependencies:
http: ^0.13.0命令:
bash
dart pub upgrade --tighten http输出():
pubspec.yamlyaml
dependencies:
http: ^0.13.5Surgical Lockfile Removal
精准删除锁定文件条目
If is retracted or locked in a conflict, remove only its block from .
package_apubspec.lockBefore ():
pubspec.lockyaml
packages:
package_a:
dependency: "direct main"
description:
name: package_a
url: "https://pub.dev"
source: hosted
version: "1.0.0" # Retracted version
package_b:
dependency: "direct main"
# ...Action: Delete the block entirely. Leave untouched. Run .
package_apackage_bdart pub get如果已撤回或陷入冲突,仅从中删除其块。
package_apubspec.lock删除前():
pubspec.lockyaml
packages:
package_a:
dependency: "direct main"
description:
name: package_a
url: "https://pub.dev"
source: hosted
version: "1.0.0" # 已撤回版本
package_b:
dependency: "direct main"
# ...操作: 完全删除块,保留不变。运行。
package_apackage_bdart pub get