Skip to content

Commit 735c8c6

Browse files
committed
test(e2e): repros for object-path #240 (same-named main) + #239 (out-of-root gen source)
Two follow-up gaps in #233's object-path disambiguation, each reproduced as a failing e2e test (red on 0.0.97) plus a decomposed design doc: - 123 (#240): a dependency and the consumer ship a same-named source (the near-universal both-have-src/main.cpp). #233 renames the scanned consumer main to obj/<pkg>/src/main.o, but the link still references the stale flat obj/main.o -> 'obj/main.o missing and no known rule'. Blocks mcpplibs #79. - 124 (#239): a dependency build.mcpp emits a source into OUT_DIR (outside the package root); its relPath carries '..', so #233 pastes obj/<pkg>/../.. and the object escapes obj/ (and the '@' in <name>@<ver> gets ninja-quoted, breaking the #235 "$out.d" depfile redirect). Design: .agents/docs/2026-07-19-object-path-disambiguation-followups-239-240-design.md
1 parent 05155ff commit 735c8c6

3 files changed

Lines changed: 382 additions & 0 deletions

File tree

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# #233 对象路径消歧的两个后续缺口(#240 / #239)——根因分析与统一修复方案
2+
3+
> 日期:2026-07-19
4+
> 基线:mcpp **0.0.97**(HEAD `05155ff`,`MCPP_VERSION = "0.0.97"` @ `src/toolchain/fingerprint.cppm`)
5+
> 范围:GitHub issue **#240**(阻塞 mcpplibs #79 opencv 收录)+ **#239**(同批同区域回归)
6+
> 全部锚点均在 0.0.97 源码 `src/build/plan.cppm` / `src/modgraph/scanner.cppm` 上核实,并以自托管构建产物做了**两份真实最小复现**。
7+
8+
---
9+
10+
## 0. 结论先行
11+
12+
0.0.97 的 #233 修复(commit `3b4f18e`,"mirror source relative path in object paths")把对象输出路径从
13+
`obj/<pkg>_<父目录名>/<file>.o` 改成 `obj/<sanitize(pkg)>/<relPath 的目录部分>/<file>.o`,解决了"同父目录名不同深度"的
14+
折叠碰撞。但这次改动**只覆盖了 scanner 产出的编译单元**,遗漏了两条路径,构成两个 OPEN bug:
15+
16+
| # | 症状 | 触发面 | 根因子系统 |
17+
|---|------|--------|-----------|
18+
| **#240** | `ninja: error: 'obj/main.o' … missing and no known rule` | 依赖包与消费者存在**同名源**(双方都有 `main.cpp`) | 链接输入端的 entry-main 对象路径**独立重算**,没跟随消歧 |
19+
| **#239** | `obj/<pkg>/../…/gen.o`(逃逸出 `obj/`,甚至镜像整棵绝对路径树) | 依赖 `build.mcpp` 把生成源写进 **OUT_DIR**(绝对路径,在包根之外) | 消歧前缀直接取 `relPath.parent_path()`,含 `..`/绝对根时 `obj/<prefix>` 向上逃逸 |
20+
21+
两者**同源**:`#233` 把"对象路径 = 前缀 + 文件名"这件事拆散在两处计算(scanner 单元一处、link 端 entry-main 一处),
22+
且前缀构造对"源在包根之外"的相对路径不做净化。修复思路统一为一句话:
23+
24+
> **对象路径的分配收敛成一个函数,entry-main 走同一个函数;前缀构造对任意 relPath 保证"永远向下、绝不逃逸 `obj/`"。**
25+
26+
修复面小、局部、对常见工程**零字节差异**(非碰撞、relPath 干净时输出与今天完全一致),可一次 PR / 一个版本(**0.0.98**)、多 commit 落地。
27+
28+
---
29+
30+
## 1. 复现(两份,均已在本机自托管产物上跑通)
31+
32+
### 1.1 #240 —— 同名 `main.cpp`
33+
34+
```
35+
mydep/ (kind=lib) src/main.cpp -> int dep_helper()
36+
consumer/(kind=bin) src/main.cpp -> int main(){…} dep = { path=../mydep }
37+
[modules] sources = ["src/**/*.cpp"] # main.cpp 被 glob 进来 → 被 scan
38+
```
39+
40+
`mcpp build` →
41+
```
42+
ninja: error: 'obj/main.o', needed by 'bin/consumer', missing and no known rule to make it
43+
```
44+
45+
build.ninja 实况:
46+
```
47+
build obj/mydep/src/main.o : cxx_object …/mydep/src/main.cpp # dep 的 main 消歧了
48+
build obj/consumer/src/main.o : cxx_object …/consumer/src/main.cpp # 消费者的 main 也消歧了
49+
build bin/consumer : link … obj/main.o obj/mydep/src/main.o # 但链接引用了消歧前的 obj/main.o
50+
```
51+
52+
### 1.2 #239 —— 依赖 `build.mcpp` 生成的 OUT_DIR 绝对路径源
53+
54+
```
55+
gdep/ build.mcpp 把 gen.cpp 写进 out_dir()(= <consumer>/target/.build-mcpp/deps/gdep@0.1.0/out)
56+
并 mcpp::generated("gen.cpp")
57+
consumer/ src/gen.cpp(与 gdep 生成的 gen.cpp 同 basename → 触发消歧分支)
58+
```
59+
60+
`mcpp build` →
61+
```
62+
cc1plus: fatal error: opening output file
63+
obj/gdep/../consumer/target/.build-mcpp/deps/gdep@0.1.0/out/gen.o: No such file or directory
64+
```
65+
66+
对象路径里出现 `..`(relPath = `relative(绝对生成源, dep 包根)`),`obj/gdep/../…` 已经逃出 `obj/`;
67+
若 `..` 更多(生成源与包根分处更远),归一化后会爬到根再下钻,等价于在 CWD 下**镜像整棵绝对路径树**。
68+
69+
---
70+
71+
## 2. 根因(file:line,0.0.97)
72+
73+
### 2.1 #240 —— entry-main 对象路径独立重算,link 用了陈旧值
74+
75+
`src/build/plan.cppm`:
76+
77+
- **消歧只发生在 scanner 单元循环**(L448–L484):`basenameCount` 仅统计 `topoOrder`;碰撞时
78+
`cu.object = obj / <sanitize(pkg)>/<relDir> / fname`,否则 `obj/fname`。
79+
- **entry-main 是在 link 组装时另造的**(L733–L784):
80+
```cpp
81+
main_cu.object = std::filesystem::path("obj") / object_filename_for(*lu.entryMain, objExt); // L737 恒为 obj/<name>.o
82+
…
83+
bool already = …; // main.cpp 若被 glob 进来则已在 compileUnits 里
84+
if (!already) plan.compileUnits.push_back(main_cu);
85+
lu.objects.push_back(main_cu.object); // L784 —— 无论 already 与否,都 push 了 L737 的扁平路径
86+
```
87+
当消费者 `main.cpp` 被 glob 进 sources(几乎所有工程 `sources=["src/**/*.cpp"]`)且与依赖的 `main.cpp`
88+
同名时:scanner 已把它编到 `obj/<pkg>/src/main.o`,但 L784 仍把 `obj/main.o` 塞进链接输入 → 没有规则产出 → 报错。
89+
entry-main 也从未进入 `basenameCount` 普查,是消歧体系的一个盲区。
90+
91+
### 2.2 #239 —— 消歧前缀对包根外的 relPath 不做净化
92+
93+
`src/build/plan.cppm` L469–L474:
94+
```cpp
95+
if (basenameCount[fname] > 1) {
96+
auto relDir = u.relPath.parent_path(); // 可能含 `..` 或为绝对
97+
auto prefix = u.packageName.empty() ? relDir
98+
: std::filesystem::path(sanitize(u.packageName)) / relDir;
99+
cu.object = std::filesystem::path("obj") / prefix / fname; // `obj/<..>/…` 逃逸
100+
}
101+
```
102+
`relPath` 由 `scanner.cppm` L1005 `std::filesystem::relative(f, p.root)` 得到。依赖 `build.mcpp` 的生成源经
103+
`build_program.cppm` L563–L567 被改写成 **OUT_DIR 下的绝对路径**再进 `modules.sources`;它在包根之外,
104+
`relative()` 于是产出 `../../…` 前缀。碰撞分支把它原样拼进 `obj/`,路径逃逸。
105+
106+
---
107+
108+
## 3. 修复方案(统一,局部)
109+
110+
全部改动集中在 `src/build/plan.cppm` 的 `make_plan`,新增两个纯函数式的小工具,无跨文件/无 schema 变更。
111+
112+
### 3.1 前缀净化:`safe_object_prefix`(修 #239,并顺带护住 entry-main)
113+
114+
把"包名 + relPath 目录部分"折叠成一个**永远向下**的子目录,对每个路径分量做单射映射:
115+
- 根名 / 根目录(`/`、盘符)分量:丢弃(去绝对根);
116+
- `.`:丢弃;
117+
- `..`:映射为 `__up`(保留单射 → 不破坏 #233 的唯一性,除非真有目录名叫 `__up`,概率极低且有 §3.4 断言兜底);
118+
- 其余分量:原样保留。
119+
120+
```cpp
121+
// relPath 可能是绝对的或含 `..`(源在其包根之外 —— 如依赖 build.mcpp 的 OUT_DIR 生成源,#239)。
122+
// 直接拼进 obj/ 会让对象路径爬出构建树。把每个分量折叠成"安全且向下"的 token:
123+
// 去根、丢 `.`、`..`→`__up`、其余保留。逐分量单射,保住 #233 的唯一性前提。
124+
std::filesystem::path safe_object_prefix(const std::string& pkg,
125+
const std::filesystem::path& relDir) {
126+
std::filesystem::path safe;
127+
for (auto const& comp : relDir) {
128+
if (comp.has_root_name() || comp.has_root_directory()) continue; // 去绝对根
129+
auto s = comp.string();
130+
if (s.empty() || s == ".") continue;
131+
safe /= (s == ".." ? "__up" : s);
132+
}
133+
return pkg.empty() ? safe : std::filesystem::path(sanitize(pkg)) / safe;
134+
}
135+
```
136+
非碰撞路径不走此函数(仍是扁平 `obj/fname`);碰撞且 relPath 干净时,输出与今天**逐字节一致**(`__up` 只在有 `..` 时出现)。
137+
138+
### 3.2 对象路径分配收敛成一个 lambda:`object_for`(修 #240 的一半 + 复用)
139+
140+
```cpp
141+
auto object_for = [&](const std::filesystem::path& src,
142+
const std::string& pkg,
143+
const std::filesystem::path& relPath) -> std::filesystem::path {
144+
const auto fname = object_filename_for(src, objExt);
145+
if (basenameCount[fname] > 1)
146+
return std::filesystem::path("obj") / safe_object_prefix(pkg, relPath.parent_path()) / fname;
147+
return std::filesystem::path("obj") / fname;
148+
};
149+
```
150+
scanner 单元循环(L459–L483)改用 `cu.object = object_for(u.path, u.packageName, u.relPath);`。
151+
152+
### 3.3 entry-main 进入普查、并复用已扫描单元的对象路径(修 #240 的另一半)
153+
154+
1. **普查纳入 entry-main**:在 `basenameCount` 计算处(L448),把 root manifest 里 **binary/test 且带 `main`**、
155+
且**尚未**被 scan(不在 `scannedSources`)的 entry 源也 `++` 计入。这样"消费者 main 未被 glob、但依赖有同名 main"
156+
的场景也能正确消歧。
157+
2. **link 端复用**(L777–L784)改为:
158+
```cpp
159+
std::filesystem::path entryObject;
160+
bool already = false;
161+
for (auto& cu : plan.compileUnits)
162+
if (cu.source == main_cu.source) { already = true; entryObject = cu.object; break; }
163+
if (!already) {
164+
main_cu.object = object_for(main_cu.source, main_cu.packageName,
165+
std::filesystem::relative(main_cu.source, projectRoot));
166+
plan.compileUnits.push_back(main_cu);
167+
entryObject = main_cu.object;
168+
}
169+
lu.objects.push_back(entryObject); // 永远用编译单元真实产出的对象
170+
```
171+
- `already`(main 被 glob):直接复用 scanner 已消歧的 `cu.object` —— **这正是 #240 的直接修复**。
172+
- `!already`(main 只在 `main=` 里、未被 glob):走同一 `object_for`,与 dep 的同名 main 一致消歧。
173+
- L737 那句独立重算(`object_filename_for`)删除,不再是路径事实来源。
174+
175+
### 3.4 保留 #233 的唯一性断言(L486–L512)
176+
177+
`safe_object_prefix` 逐分量单射 + entry-main 纳入同一分配器后,理论上不会再碰撞;L486 的"碰撞即报错"防御断言**保留原样**,
178+
作为任何未预料输入的可诊断兜底(把 ninja 的 "multiple rules generate X" 变成点名两个源文件的 mcpp 错误)。
179+
180+
---
181+
182+
## 4. 影响面与兼容性
183+
184+
- **常见工程零差异**:单 binary、`main.cpp` 唯一 → 仍是扁平 `obj/main.o`,link 复用同一路径。e2e 117 现状(`main=src/main.cpp` 且唯一)不变。
185+
- **仅在发生 basename 碰撞时**行为改变,且改后才是正确的(今天是构建直接失败)。
186+
- 无 manifest schema、无 CLI、无跨平台字节差异(`safe_object_prefix` 用 `path` 迭代器,Windows 盘符走 `has_root_name` 分支)。
187+
188+
## 5. 提交拆分(单 PR / 0.0.98,多 commit)
189+
190+
1. `test(e2e): add failing repros for #240 (same-named main) and #239 (OUT_DIR abs gen source)` —— 先落两个红测(TDD)。
191+
2. `fix(build): route entry-main object path through disambiguation; reuse scanned unit's object (#240)`。
192+
3. `fix(build): sanitize collision prefix so out-of-root/abs relPaths stay under obj/ (#239)`。
193+
4. `chore(release): bump mcpp 0.0.97 -> 0.0.98` + CHANGELOG。
194+
195+
> 发版后按既定链路:release 四平台 → 镜像 xlings-res(gh+gtc)→ xim-pkgindex 索引 → bump bootstrap pin;随后 mcpplibs #79 CI pin 升到 0.0.98 即可解阻塞。#242(消费端 default-features opt-out)、#243(依赖 feature 转发)为独立非阻塞项,不在本 PR。
196+
197+
## 6. 验证清单
198+
199+
- `mcpp test`(单测,含 ninja backend)全绿。
200+
- e2e:新增两测 + `tests/e2e/run_all.sh` host-aware 全过(至少 117/118 + 新两测)。
201+
- 手工:§1 两份复现均 `mcpp build && mcpp run` 成功,ninja 中 entry-main link 输入 = 其编译边真实对象;`obj/` 下无 `..`/绝对逃逸。
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
# requires: elf gcc
3+
# mcpp#240 (follow-up to #233): when a dependency package and the consumer
4+
# ship a SAME-NAMED source (the near-universal case: both have src/main.cpp —
5+
# e.g. OpenCV's sample main.cpp's vs the consumer's own), #233's object-path
6+
# disambiguation renames the scanned consumer main to obj/<pkg>/src/main.o,
7+
# but the LINK step kept referencing the pre-disambiguation flat obj/main.o:
8+
# ninja: error: 'obj/main.o', needed by 'bin/<x>', missing and no known rule
9+
# The entry-main link input must follow the same disambiguation as its
10+
# compile edge. Member-scoped tests dodge this (distinct filenames), so only
11+
# a real dep+consumer collision exercises it.
12+
set -e
13+
14+
TMP=$(mktemp -d)
15+
trap "rm -rf $TMP" EXIT
16+
cd "$TMP"
17+
18+
# Dependency (kind=lib) that ships its OWN src/main.cpp (an impl file here,
19+
# same basename as the consumer's entry).
20+
mkdir -p mydep/src
21+
cat > mydep/src/main.cpp <<'EOF'
22+
int dep_helper() { return 41; }
23+
EOF
24+
cat > mydep/mcpp.toml <<'EOF'
25+
[package]
26+
name = "mydep"
27+
version = "0.1.0"
28+
[modules]
29+
sources = ["src/**/*.cpp"]
30+
[targets.mydep]
31+
kind = "lib"
32+
EOF
33+
34+
# Consumer with its OWN src/main.cpp (globbed into sources, so it is scanned
35+
# and subject to disambiguation).
36+
mkdir -p consumer/src
37+
cat > consumer/src/main.cpp <<'EOF'
38+
import std;
39+
extern int dep_helper();
40+
int main() {
41+
std::println("val={}", dep_helper());
42+
return dep_helper() == 41 ? 0 : 1;
43+
}
44+
EOF
45+
cat > consumer/mcpp.toml <<'EOF'
46+
[package]
47+
name = "consumer"
48+
version = "0.1.0"
49+
[modules]
50+
sources = ["src/**/*.cpp"]
51+
[dependencies]
52+
mydep = { path = "../mydep" }
53+
[targets.consumer]
54+
kind = "bin"
55+
main = "src/main.cpp"
56+
EOF
57+
58+
cd consumer
59+
"$MCPP" build > build.log 2>&1 || {
60+
cat build.log
61+
echo "FAIL: build failed (expected: same-named main across dep must link the disambiguated object)"
62+
exit 1
63+
}
64+
65+
ninja_file="$(find target -name build.ninja | head -1)"
66+
[[ -n "$ninja_file" ]] || { echo "no build.ninja generated"; exit 1; }
67+
68+
# The link edge must NOT reference a bare obj/main.o (the stale, unproduced
69+
# flat path). It must reference the consumer main's real, disambiguated object.
70+
link_line="$(grep -E 'bin/consumer *:' "$ninja_file")"
71+
if echo "$link_line" | grep -qE '(^| )obj/main\.o( |$)'; then
72+
echo "FAIL: link still references stale flat obj/main.o"
73+
echo "$link_line"
74+
cat "$ninja_file"
75+
exit 1
76+
fi
77+
78+
out="$("$MCPP" run 2>&1 | tail -1)"
79+
[[ "$out" == "val=41" ]] || { echo "unexpected output: $out"; exit 1; }
80+
81+
echo "OK"
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bash
2+
# requires: elf gcc
3+
# mcpp#239 (follow-up to #233): a dependency's build.mcpp can emit generated
4+
# sources into OUT_DIR (<consumer>/target/.build-mcpp/deps/<name>@<ver>/out),
5+
# which live OUTSIDE the dependency's package root. Their scanner relPath is
6+
# `relative(<abs OUT_DIR src>, <dep root>)` and therefore carries `..`
7+
# components. #233's collision-disambiguation pasted that relPath straight
8+
# into the object path:
9+
# cc1plus: fatal error: opening output file
10+
# obj/<dep>/../<consumer>/target/.../out/gen.o: No such file or directory
11+
# i.e. `obj/<dep>/../…` climbs out of obj/ (and with enough `..`, mirrors the
12+
# whole absolute tree under CWD). The collision prefix must be sanitized so
13+
# every object stays under obj/.
14+
set -e
15+
16+
TMP=$(mktemp -d)
17+
trap "rm -rf $TMP" EXIT
18+
cd "$TMP"
19+
20+
# Dependency whose build.mcpp writes gen.cpp into OUT_DIR (absolute path,
21+
# outside the package root).
22+
mkdir -p gdep/src
23+
cat > gdep/build.mcpp <<'EOF'
24+
#include <cstdio>
25+
#include <string>
26+
import mcpp;
27+
int main() {
28+
std::string p = std::string(mcpp::out_dir()) + "/gen.cpp";
29+
std::FILE* f = std::fopen(p.c_str(), "w");
30+
std::fputs("int gen_val() { return 7; }\n", f);
31+
std::fclose(f);
32+
mcpp::generated("gen.cpp");
33+
return 0;
34+
}
35+
EOF
36+
cat > gdep/src/lib.cpp <<'EOF'
37+
extern int gen_val();
38+
int gdep_use() { return gen_val(); }
39+
EOF
40+
cat > gdep/mcpp.toml <<'EOF'
41+
[package]
42+
name = "gdep"
43+
version = "0.1.0"
44+
[modules]
45+
sources = ["src/**/*.cpp"]
46+
[targets.gdep]
47+
kind = "lib"
48+
EOF
49+
50+
# Consumer with its OWN src/gen.cpp — same basename as gdep's generated
51+
# gen.cpp, forcing the disambiguation branch on the out-of-root abs source.
52+
mkdir -p consumer/src
53+
cat > consumer/src/gen.cpp <<'EOF'
54+
int consumer_gen() { return 100; }
55+
EOF
56+
cat > consumer/src/main.cpp <<'EOF'
57+
import std;
58+
extern int gdep_use();
59+
extern int consumer_gen();
60+
int main() {
61+
std::println("sum={}", gdep_use() + consumer_gen());
62+
return (gdep_use() == 7 && consumer_gen() == 100) ? 0 : 1;
63+
}
64+
EOF
65+
cat > consumer/mcpp.toml <<'EOF'
66+
[package]
67+
name = "consumer"
68+
version = "0.1.0"
69+
[modules]
70+
sources = ["src/**/*.cpp"]
71+
[dependencies]
72+
gdep = { path = "../gdep" }
73+
[targets.consumer]
74+
kind = "bin"
75+
main = "src/main.cpp"
76+
EOF
77+
78+
cd consumer
79+
"$MCPP" build > build.log 2>&1 || {
80+
cat build.log
81+
echo "FAIL: build failed (expected: out-of-root generated source must map under obj/)"
82+
exit 1
83+
}
84+
85+
ninja_file="$(find target -name build.ninja | head -1)"
86+
[[ -n "$ninja_file" ]] || { echo "no build.ninja generated"; exit 1; }
87+
88+
# No object output may escape obj/ via `..` or an absolute root.
89+
bad="$(grep -oE 'build [^ ]+\.o : cxx_object' "$ninja_file" | awk '{print $2}' \
90+
| grep -E '(^|/)\.\.(/|$)|^/' || true)"
91+
if [[ -n "$bad" ]]; then
92+
echo "FAIL: object path escapes obj/:"
93+
echo "$bad"
94+
exit 1
95+
fi
96+
97+
out="$("$MCPP" run 2>&1 | tail -1)"
98+
[[ "$out" == "sum=107" ]] || { echo "unexpected output: $out"; exit 1; }
99+
100+
echo "OK"

0 commit comments

Comments
 (0)