Skip to content

fix(wpm): key download cache by version and artifact hash (issue #1154) - #1157

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/wpm-cache-key-version
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/wpm-cache-key-version

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

wpm 的下载缓存键此前只含包名(<pkg>.<ext>,src/commands/wpm.cpp download_artifact)。索引 revision 升级包版本(新 URL + 新 sha256)后,wpm install --force 仍命中旧缓存条目并静默安装旧产物——正是 #1154 实测复现的静默错误。

修复:缓存文件名改为 <pkg>-<version>-<sha8>.<ext>,版本与短产物哈希直接取自 index.json(packages[].version / artifacts[].sha256),结构上杜绝跨版本复用:

  • 新增 artifact_cache_filename() + sanitize_cache_key_component()(版本/包名中的文件系统不安全字符替换为 _);
  • download_artifact() 增加 version 参数,install 与 wpm update(自更新)两个调用点传入;
  • 缓存命中路径维持既有校验:缓存文件必须散列到 index artifacts[].sha256(cached_artifact_is_valid),不符即删除缓存重新下载——同版本 --force 重装继续复用缓存,缓存损坏/污染自动重下;
  • 缓存键变化后旧格式文件成为孤儿,wpm clean cache 可一次性清除(行为已有,无需迁移)。

不新增任何用户可见文案(无需 i18n 同步)。

关联 issue

closes #1154

实测复现引用(2026-10-10,wpm 0.4.0,WinuxCmd 1.3.3 自带,#1154 原文):

  1. index.json 的 niugit 条目从 2.55.0.2 更新到 2.55.0.3(新 URL + 新 sha256);
  2. wpm install niugit --force —— 输出 extracting niugit.zip + installed niugit(报成功);
  3. 实际落盘 opt/niugit/git.exe 仍是旧版(sha256 前缀 E5BFBF9C,v2.55.0.2 产物);
  4. 根因:.wpm/cache/niugit.zip 哈希 FC293AD2... 为旧 zip,缓存键只含包名;
  5. wpm clean cache 后重装才装上 2.55.0.3(6FC85A25...)。期间 --force 五次重装全部报成功但文件未变。

Test plan

  • scripts/build-with-vs.ps1(VS18 / Ninja / Debug,-DBUILD_FFI=OFF)构建通过
  • winuxcmd-tests --gtest_filter=wpm.*:44/44 通过,含 3 个新增回归测试 + 1 个更新测试:
    • wpm_install_cache_key_distinguishes_versions:旧版本缓存条目(含旧格式 <pkg>.<ext>)无法满足新版本索引(该用例在修复前必失败——旧键 + sha 匹配即命中)
    • wpm_install_redownloads_when_cached_artifact_sha_mismatches:缓存键正确但内容损坏 → 重新下载并替换缓存
    • wpm_install_force_after_version_bump_installs_new_artifact:索引升版后 --force 重装落盘新产物,生成新缓存条目
    • wpm_install_uses_valid_cached_artifact_before_downloading:同版本+同 sha 命中新格式缓存,不下载
  • 全量单测 2778 个:2776 通过;2 个失败为 grep.grep_l_exit_zero_iff_any_line_selected 与 grep.grep_r_skips_cross_junction_trees_without_crashing(junction 相关环境性失败,与本 diff 无关——本 PR 仅改 src/commands/wpm.cpp 与 wpm 单测)
  • scripts/format.py(clang-format)已运行,仅保留本任务两个文件的改动

The artifact cache file name was '<pkg>.<ext>', keyed by package name
alone. After an index revision bumped a package version (new URL and
sha256), 'wpm install --force' kept hitting the stale cache entry and
silently installed the old artifact.

Cache entries are now '<pkg>-<version>-<sha8>.<ext>' (version and short
artifact sha256 come straight from the index), so entries can never be
reused across versions. The existing cache-hit path still requires the
cached file to hash to the index artifacts[].sha256 and falls back to a
fresh download otherwise, so same-version reinstalls keep reusing the
cache while corrupted or polluted entries are discarded.

Tests: cache key distinguishes versions (stale 1.0.0 entry cannot
satisfy a 2.0.0 index), cached artifact with mismatched sha256 is
re-downloaded, same version+sha still hits the cache, and a
--force install after an index version bump installs the new artifact.
@caomengxuan666
caomengxuan666 merged commit 6e04623 into main Oct 10, 2026
4 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/wpm-cache-key-version branch October 10, 2026 06:08
caomengxuan666 added a commit that referenced this pull request Oct 10, 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.

[bug] wpm 下载缓存键不含版本号:索引更新后 --force 重装仍使用旧缓存包(实测)

1 participant