Skip to content

fix(pubspec): pin hosted dependency versions from pubspec.lock to speed up pub get - #118

Closed
qinshah wants to merge 1 commit into
dev4harmony:ohosfrom
qinshah:fix/pubspec-pin-versions
Closed

fix(pubspec): pin hosted dependency versions from pubspec.lock to speed up pub get#118
qinshah wants to merge 1 commit into
dev4harmony:ohosfrom
qinshah:fix/pubspec-pin-versions

Conversation

@qinshah

@qinshah qinshah commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

固定 pubspec.yaml 中 hosted 依赖的版本,加速 pub get

变更内容

通过 scripts/pin_pubspec.py 脚本,从当前 pubspec.lock 反向改写 pubspec.yaml

  • dependencies / dependency_overrides / dev_dependencies 中所有 ^x.y.z 形式的版本约束,替换为 lock 里解析出的精确版本
  • any 约束(vector_mathfixnumjson_annotationcollection 等)替换为 lock 里的精确版本
  • 注释掉的依赖、sdk: flutterflutter_launcher_icons / flutter_native_splash / flutter: 等 section 不动

Git 依赖不动

ref: <branch/tag> 保持原样不改写为 commit SHA

原因:git 包之间会通过 ref 名字互相引用,例如 audio_service(git 依赖)的 pubspec 里指定了 audio_session 的 ref 是 br_v0.2.2_ohos。如果把 audio_session 的 ref 改成 commit SHA,pub 解析时两者约束不匹配,version solving 直接失败。所以 git 包的 ref 不能动。

验证结果

qinshah/PiliPlusfix/pubspec-pin-versions 分支上手动触发了 pr_check workflow,flutter pub getdart analyze 全部通过:

完整的 Actions 运行历史可以到 https://github.com/qinshah/PiliPlus/actions 查看。

附带脚本

scripts/pin_pubspec.py 是用于生成此次改动的脚本,下次 lock 更新后可以重新跑一遍来重新 pin 版本,保持依赖版本与 lock 一致。

Speeds up `flutter pub get` by reducing version resolution work for
hosted packages: all `^x.y.z` and `any` constraints in dependencies /
dependency_overrides / dev_dependencies are replaced with the exact
version resolved in pubspec.lock.

Git dependencies are intentionally LEFT UNTOUCHED. Their `ref:` is a
branch/tag name and other git packages may reference them by that
name (e.g. audio_service depends on audio_session at
br_v0.2.2_ohos). Rewriting ref to a commit SHA breaks pub's version
solving.

Commented-out entries and non-deps sections (flutter_launcher_icons,
flutter_native_splash, flutter:) are left untouched.

Also adds scripts/pin_pubspec.py, the script used to regenerate this
file from pubspec.lock (reusable next time lock is updated).
@qinshah qinshah closed this Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant