Skip to content

Commit 74d1df4

Browse files
fix(build): two packages of one name both link; a later edge walks the features it activates
The plan found a package by its bare name too, and the bare name of `ns.foo` replaced `foo`'s, so a program reaching both linked `ns.foo` alone (#896). The bare name now finds a package only where it is no other package's. A package already walked with the features of its first edge took a later edge's features into its sources but never walked their feature-deps or forwards, so `v` selected beside `s` built `a` without the feature `s`'s `x` asks of it (#897). The feature-deps and forwards of a feature a later edge activates are walked when the edge arrives. e2e 1018 and 1019. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 8f02110 commit 74d1df4

5 files changed

Lines changed: 170 additions & 6 deletions

File tree

‎src/build/plan.cppm‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,10 +2431,19 @@ make_plan(const mcpp::manifest::Manifest& manifest,
24312431
// of the root's, by name and by the shared package that takes them.
24322432
std::set<std::string> placedInImage;
24332433
std::map<std::size_t, std::vector<std::size_t>> staticsByImagePackage;
2434-
for (std::size_t i = 0; i < packages.size(); ++i) {
2435-
auto const& p = packages[i];
2436-
packageIndexByName[qualified_package_name(p.manifest)] = i;
2437-
packageIndexByName[p.manifest.package.name] = i;
2434+
// A package is found by its qualified name, and by its bare name only where
2435+
// that name is no other package's: `foo` and `ns.foo` are two packages, and
2436+
// the bare `foo` of the second used to replace the first, so an edge to
2437+
// `foo` reached `ns.foo` and `foo`'s objects were left out of the link
2438+
// (mcpp#896).
2439+
for (std::size_t i = 0; i < packages.size(); ++i)
2440+
packageIndexByName[qualified_package_name(packages[i].manifest)] = i;
2441+
{
2442+
std::map<std::string, std::vector<std::size_t>, std::less<>> byBareName;
2443+
for (std::size_t i = 0; i < packages.size(); ++i)
2444+
byBareName[packages[i].manifest.package.name].push_back(i);
2445+
for (auto const& [bare, holders] : byBareName)
2446+
if (holders.size() == 1) packageIndexByName.emplace(bare, holders.front());
24382447
}
24392448

24402449
for (std::size_t i = 1; i < packages.size(); ++i) {

‎src/build/prepare/graph.cpp‎

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,61 @@ step4b_identity_version_merge(PrepareState& state, WorklistItemCtx& ctx,
11501150
return {};
11511151
}
11521152

1153+
// A LATER EDGE THAT ASKS MORE FEATURES OF A RESOLVED PACKAGE (mcpp#897). The
1154+
// package was walked with the features its first edge asked; the union of its
1155+
// edges is what it is built with, so the feature-deps and the forwards of each
1156+
// feature that only a later edge activates are walked now, as the first walk
1157+
// would have walked them. Measured: `v` selected as a member and reached by
1158+
// `s` with `x` compiled `x`'s sources while `x`'s feature-deps never reached
1159+
// the graph.
1160+
static std::expected<void, std::string>
1161+
step4b_widen_features(PrepareState& state, std::size_t depIndex, const WorkItem& item,
1162+
const mcpp::manifest::DependencySpec& spec) {
1163+
if (depIndex >= state.dep_manifests.size()) return {};
1164+
auto requested = state.depFeatureRequests.find(depIndex);
1165+
if (requested == state.depFeatureRequests.end()) return {};
1166+
auto& [features, seeded] = requested->second;
1167+
std::vector<std::string> wider = features;
1168+
for (auto const& f : spec.features)
1169+
if (std::ranges::find(wider, f) == wider.end()) wider.push_back(f);
1170+
const bool widerSeed = seeded || spec.defaultFeatures;
1171+
if (wider == features && widerSeed == seeded) return {};
1172+
auto& dm = *state.dep_manifests[depIndex];
1173+
const auto before = feature_closure(dm, features, seeded);
1174+
const auto after = feature_closure(dm, wider, widerSeed);
1175+
features = std::move(wider);
1176+
seeded = widerSeed;
1177+
if (after == before) return {};
1178+
if (auto fe = validateForwards(state, dm, after, dm.package.name); !fe)
1179+
return std::unexpected(fe.error());
1180+
const auto previous = dm.dependencies;
1181+
if (auto fm = mergeActiveFeatureDeps(dm, features, seeded); !fm)
1182+
return std::unexpected(fm.error());
1183+
if (depIndex + 1 < state.packages.size())
1184+
state.packages[depIndex + 1].manifest.dependencies = dm.dependencies;
1185+
std::string label = dm.package.name;
1186+
for (auto const& [k, r] : state.resolved)
1187+
if (r.depIndex == depIndex) {
1188+
label = std::format("{}{}{}@{}", k.ns, k.ns.empty() ? "" : ".", k.shortName,
1189+
r.source == "version" ? r.version : r.source);
1190+
break;
1191+
}
1192+
const auto root = depIndex + 1 < state.packages.size() ? state.packages[depIndex + 1].root
1193+
: std::filesystem::path{};
1194+
for (auto const& [childName, childSpec] : dm.dependencies) {
1195+
auto was = previous.find(childName);
1196+
auto now = childSpec;
1197+
auto then = was == previous.end() ? childSpec : was->second;
1198+
injectForwards(dm, after, childName, now);
1199+
injectForwards(dm, before, childName, then);
1200+
if (was != previous.end() && now.features == then.features && now.tools == then.tools)
1201+
continue;
1202+
state.worklist.push_back({childName, now, label, now.version, depIndex, root,
1203+
item.devOnly, item.buildOnly});
1204+
}
1205+
return {};
1206+
}
1207+
11531208
static std::expected<void, std::string>
11541209
step4b_handle_already_resolved(PrepareState& state, WorklistItemCtx& ctx,
11551210
std::map<ResolvedKey, ResolvedRecord>::iterator it) {
@@ -1267,7 +1322,7 @@ step4b_handle_already_resolved(PrepareState& state, WorklistItemCtx& ctx,
12671322
it->second.depIndex + 1,
12681323
spec, item.fromBuildTable, name);
12691324
}
1270-
return {};
1325+
return step4b_widen_features(state, it->second.depIndex, item, spec);
12711326
}
12721327
if (sourceKind == "version" && it->second.version != spec.version) {
12731328
if (auto r = step4b_identity_version_merge(state, ctx, it); !r)
@@ -1343,7 +1398,7 @@ step4b_handle_already_resolved(PrepareState& state, WorklistItemCtx& ctx,
13431398
it->second.depIndex + 1,
13441399
spec, item.fromBuildTable, name);
13451400
}
1346-
return {};
1401+
return step4b_widen_features(state, it->second.depIndex, item, spec);
13471402
return {};
13481403
}
13491404

@@ -1924,6 +1979,7 @@ step4b_finalize_dependency(PrepareState& state, WorklistItemCtx& ctx) {
19241979
key.shortName,
19251980
sourceKind == "version" ? spec.version : sourceKind);
19261981
const std::size_t selfIdx = state.dep_manifests.size() - 1;
1982+
state.depFeatureRequests[selfIdx] = {spec.features, spec.defaultFeatures};
19271983
// #243: forward this dep's active features to ITS children before they
19281984
// are pushed (transitive dep->dep forwarding rides the BFS forward
19291985
// edge). Uses the SAME closure inputs as mergeActiveFeatureDeps above

‎src/build/prepare/state.cppm‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ struct PrepareState {
812812
loadVersionDepVariant;
813813

814814
std::map<ResolvedKey, ResolvedRecord> resolved;
815+
// The features asked of each resolved dependency so far (by dep_manifests
816+
// slot) and whether its default set is seeded: the union of its edges, as
817+
// its feature-deps and forwards were walked (mcpp#897).
818+
std::map<std::size_t, std::pair<std::vector<std::string>, bool>> depFeatureRequests;
815819
std::map<std::size_t, std::vector<std::pair<std::string, std::string>>>
816820
toolEnvByConsumer;
817821
// The programs the graph built for this machine at the ROOT's request
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
# A PACKAGE AND A NAMESPACED PACKAGE OF ONE NAME BOTH LINK (mcpp#896).
3+
# `foo` and `ns.foo` are two packages. The plan found a package by its bare
4+
# name too, and the bare `foo` of `ns.foo` replaced `foo`'s, so a member that
5+
# depended on both reached `ns.foo` twice and its link left `foo`'s objects
6+
# out (`undefined reference to foo::one`). Each package is now reached by the
7+
# edge that names it.
8+
set -e
9+
TMP=$(mktemp -d)
10+
trap 'rm -rf "$TMP"' EXIT
11+
fail() { echo "FAIL: $1"; shift; for f in "$@"; do echo "--- $f ---"; cat "$f" 2>/dev/null; done; exit 1; }
12+
MCPP="${MCPP:-mcpp}"
13+
14+
cd "$TMP"
15+
mkdir -p foo/src nsfoo/src app/src
16+
printf '[workspace]\nmembers = ["foo", "nsfoo", "app"]\n' > mcpp.toml
17+
printf '[package]\nname = "foo"\nversion = "0.1.0"\n[targets.foo]\nkind = "lib"\n[build]\nsources = ["src/*.cppm", "src/*.cpp"]\n' > foo/mcpp.toml
18+
printf '[package]\nnamespace = "ns"\nname = "foo"\nversion = "0.1.0"\n[targets.ns-foo]\nkind = "lib"\n[build]\nsources = ["src/*.cppm", "src/*.cpp"]\n' > nsfoo/mcpp.toml
19+
printf '[package]\nname = "app"\nversion = "0.1.0"\n[targets.app]\nkind = "bin"\nmain = "src/main.cpp"\n[dependencies]\nfoo = { path = "../foo" }\nns.foo = { path = "../nsfoo" }\n' > app/mcpp.toml
20+
printf 'export module foo;\nexport namespace foo { int one(); }\n' > foo/src/foo.cppm
21+
printf 'module foo;\nnamespace foo { int one() { return 1; } }\n' > foo/src/foo.cpp
22+
printf 'export module ns.foo;\nexport namespace ns::foo { int two(); }\n' > nsfoo/src/foo.cppm
23+
printf 'module ns.foo;\nnamespace ns::foo { int two() { return 2; } }\n' > nsfoo/src/foo.cpp
24+
printf 'import std;\nimport foo;\nimport ns.foo;\nint main() { std::println("sum {}", foo::one() + ns::foo::two()); }\n' > app/src/main.cpp
25+
26+
"$MCPP" run -p app > run.log 2>&1 || fail "the member reaching foo and ns.foo did not link and run" run.log
27+
grep -q "sum 3" run.log || fail "the program's output" run.log
28+
echo "PASS: 1018 a package and a namespaced package of one name both link"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
# A LATER EDGE WALKS THE FEATURE-DEPS IT ACTIVATES (mcpp#897).
3+
# `v` asks `a` for `f1`, and its feature `x` asks `a` for `f2` through
4+
# `[feature-deps.x]`; `s` asks `v` for `x`. With `v` selected beside `s`, `v`
5+
# was walked as a member first, without `x`, and `s`'s edge later added `x` to
6+
# it: `x`'s sources were compiled, `x`'s feature-deps never reached the graph,
7+
# and `a` was built without `f2` (`a.f2: failed to read compiled module`). The
8+
# feature-deps and forwards of a feature a later edge activates are walked now.
9+
set -e
10+
TMP=$(mktemp -d)
11+
trap 'rm -rf "$TMP"' EXIT
12+
fail() { echo "FAIL: $1"; shift; for f in "$@"; do echo "--- $f ---"; cat "$f" 2>/dev/null; done; exit 1; }
13+
MCPP="${MCPP:-mcpp}"
14+
15+
cd "$TMP"
16+
mkdir -p a/src/f1 a/src/f2 v/src/base v/src/x s/src
17+
printf '[workspace]\nmembers = ["a", "v", "s"]\n' > mcpp.toml
18+
cat > a/mcpp.toml <<'EOF'
19+
[package]
20+
name = "a"
21+
version = "0.1.0"
22+
[targets.a]
23+
kind = "lib"
24+
[build]
25+
sources = []
26+
[features]
27+
f1 = { sources = ["src/f1/*.cppm"] }
28+
f2 = { sources = ["src/f2/*.cppm"] }
29+
EOF
30+
printf 'export module a.f1;\nexport int f1() { return 1; }\n' > a/src/f1/f1.cppm
31+
printf 'export module a.f2;\nexport int f2() { return 2; }\n' > a/src/f2/f2.cppm
32+
cat > v/mcpp.toml <<'EOF'
33+
[package]
34+
name = "v"
35+
version = "0.1.0"
36+
[targets.v]
37+
kind = "lib"
38+
[build]
39+
sources = ["src/base/*.cppm"]
40+
[dependencies]
41+
a = { path = "../a", features = ["f1"] }
42+
[features]
43+
x = { sources = ["src/x/*.cppm"] }
44+
[feature-deps.x]
45+
a = { path = "../a", features = ["f2"] }
46+
EOF
47+
printf 'export module v;\nimport a.f1;\nexport int vv() { return f1(); }\n' > v/src/base/v.cppm
48+
printf 'export module v.x;\nimport a.f2;\nexport int vx() { return f2(); }\n' > v/src/x/x.cppm
49+
cat > s/mcpp.toml <<'EOF'
50+
[package]
51+
name = "s"
52+
version = "0.1.0"
53+
[targets.s]
54+
kind = "bin"
55+
main = "src/main.cpp"
56+
[dependencies]
57+
v = { path = "../v", features = ["x"] }
58+
EOF
59+
printf 'import std;\nimport v;\nimport v.x;\nint main() { std::println("sum {}", vv() + vx()); }\n' > s/src/main.cpp
60+
61+
for order in "-p s -p v" "-p v -p s"; do
62+
rm -rf target
63+
"$MCPP" build $order > build.log 2>&1 || fail "mcpp build $order" build.log
64+
done
65+
"$MCPP" run -p s > run.log 2>&1 || fail "the program" run.log
66+
grep -q "sum 3" run.log || fail "the program's output" run.log
67+
echo "PASS: 1019 a later edge walks the feature-deps it activates"

0 commit comments

Comments
 (0)