Skip to content

fix(cdb): preserve tests/ entries across mcpp build (v0.0.63)#166

Merged
Sunrisepeak merged 1 commit into
mainfrom
feat/cdb-test-coverage
Jun 24, 2026
Merged

fix(cdb): preserve tests/ entries across mcpp build (v0.0.63)#166
Sunrisepeak merged 1 commit into
mainfrom
feat/cdb-test-coverage

Conversation

@Sunrisepeak

Copy link
Copy Markdown
Member

问题

clangd 在 tests/ 目录下对 gtest::InitGoogleTest()import stdimport mcpplibs.* 全无代码提示

根因:compile_commands.json当次构建 plan 的镜像mcpp build 的 plan 不含 tests/**/*.cpp 与 dev-deps,而它与 mcpp test同一个 cdb——后写覆盖前写。日常「编辑→mcpp build」循环里,测试条目几乎总被擦掉(P0 fast-path 全缓存时不重写,故偶尔残留;一旦真实重建即回退)。

完整分析见 .agents/docs/2026-06-25-cdb-test-coverage-design.md

方案:offline-first 的 cdb「合并保留」

mcpp test 本就能写出完整正确的 cdb(测试条目 flag 有效、其 BMI 因 build/test 共用输出目录而真实存在)。bug 本质是 mcpp build 摧毁它。修复 = 让 build 停止摧毁:

write_compile_commands 由「全量覆盖」改为「合并」:

  • 当前 plan 覆盖的文件 → 用本次条目(权威);
  • 当前 plan 未覆盖、但文件仍存在的旧条目(上次 mcpp test 写的测试条目)→ 保留;
  • 文件已删 → 剪除;坏 JSON → 回退纯 fresh。

mcpp build 自身零改动:不解析、不下载任何 dev-deps,build-only 用户与构建图均不受影响(严守 offline-first)。跑一次 mcpp test 后,测试补全在后续所有 mcpp build 中持久生效。

为何不让 mcpp build 主动生成测试 cdb:会触发 dev-deps 下载、违背 offline-first、坑「只想 build」的用户;且测试补全本就必须先装 gtest+构建 BMI(只能由 mcpp test/install 完成),纯 B 收益为零。决策理由见设计文档 §7/§8。

改动

  • src/build/compile_commands.cppm:新增纯函数 merge_compile_commands(fresh, existing, fileExists) + write_compile_commands 走合并。其余构建相关文件(plan/prepare/ninja_backend/execute)均无改动
  • 单测 tests/unit/test_compile_commands.cpp:合并保留 / 剪除已删 / fresh 胜出去重 / 坏 JSON 回退(TDD,先 RED 后 GREEN)。
  • e2e tests/e2e/77_cdb_preserves_test_entries.sh:mcpp test真实重建 mcpp build 仍保留测试条目 + 删测试文件后剪除(三平台)。
  • bump 0.0.62 → 0.0.63

验证(本机)

  • 全量单测 24/24 通过(含新 test_compile_commands 4/4)。
  • e2e 47 / 76 / 77 通过。
  • e2e 59 本机 llvm 环境性失败(基线 0.0.56 同样失败,与本改动无关)。

🤖 Generated with Claude Code

…rage)

`compile_commands.json` mirrors the current build plan. `mcpp build`'s plan
excludes tests/**/*.cpp and dev-deps, yet it writes the SAME cdb as `mcpp test`
— so in the edit→build loop the test entries get overwritten and clangd loses
all completion in tests/ (gtest, import std, import mcpplibs.*).

Fix: `write_compile_commands` now MERGES instead of overwriting — it preserves
still-valid prior entries the current plan doesn't cover (the tests/ entries a
previous `mcpp test` wrote) and prunes entries whose file no longer exists.

`mcpp build` itself is unchanged: it resolves/downloads NO dev-deps, leaving
build-only users and the build graph untouched (offline-first). Run `mcpp test`
once and test completion persists across every subsequent `mcpp build`.

- new pure helper merge_compile_commands(fresh, existing, fileExists)
- unit: tests/unit/test_compile_commands.cpp (merge/prune/dedup/bad-json)
- e2e:  tests/e2e/77_cdb_preserves_test_entries.sh (build keeps test entries)
- design: .agents/docs/2026-06-25-cdb-test-coverage-design.md
- bump 0.0.62 -> 0.0.63
@Sunrisepeak Sunrisepeak merged commit 95885dd into main Jun 24, 2026
5 checks passed
@Sunrisepeak Sunrisepeak deleted the feat/cdb-test-coverage branch June 24, 2026 20:13
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