Skip to content

Commit 3a0e411

Browse files
refactor: the key graph and the locked targets leave their phase functions
step13_dependency_cache and step13_lockfile had passed the 400-line gate of #722 with this release's additions. The package graph the keys fold along (key_graph, with serves_root_tests) and the lock's target entries (lock_targets, target_drift) are functions of their own; behaviour is unchanged. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 0a073b8 commit 3a0e411

2 files changed

Lines changed: 61 additions & 43 deletions

File tree

‎src/build/prepare/plan.cpp‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,36 @@ static std::expected<void, std::string> step13_assembly_units(PrepareState& stat
15881588
}
15891589

15901590

1591+
// The root's own `[dev-dependencies]` serve its tests, not its library, which a
1592+
// framework that uses the package under test reaches (names_the_root,
1593+
// graph_edges.cpp): the root's key does not fold them in.
1594+
static bool serves_root_tests(const DependencyEdge& e) {
1595+
return e.consumerPackageIndex == 0 && e.devTable;
1596+
}
1597+
1598+
// The package graph the keys fold along: the declared edges, and the imports no
1599+
// declared edge covers. An import a declared edge already covers adds nothing.
1600+
// One that a declared path runs the other way is a module this build generates
1601+
// for the importer below its consumer's root, filed under the consumer by its
1602+
// path (e2e 849): not an upstream, and left out of `importedFrom` rather than
1603+
// closing a cycle.
1604+
static mcpp::graph::AdjacencyList key_graph(const PrepareState& state,
1605+
std::vector<std::set<std::size_t>>& importedFrom) {
1606+
mcpp::graph::AdjacencyList pkgDeps(state.packages.size());
1607+
for (auto& e : state.dependencyEdges)
1608+
if (e.consumerPackageIndex < pkgDeps.size() && !serves_root_tests(e))
1609+
pkgDeps[e.consumerPackageIndex].push_back(e.dependencyPackageIndex);
1610+
for (std::size_t p = 0; p < importedFrom.size(); ++p)
1611+
std::erase_if(importedFrom[p], [&](std::size_t q) {
1612+
if (std::ranges::find(pkgDeps[p], q) != pkgDeps[p].end()) return true;
1613+
const auto above = mcpp::graph::closure(pkgDeps, std::array{q}, /*include_roots=*/false);
1614+
return std::ranges::find(above, p) != above.end();
1615+
});
1616+
for (std::size_t p = 0; p < importedFrom.size(); ++p)
1617+
pkgDeps[p].insert(pkgDeps[p].end(), importedFrom[p].begin(), importedFrom[p].end());
1618+
return pkgDeps;
1619+
}
1620+
15911621
static std::expected<void, std::string> step13_dependency_cache(PrepareState& state, BuildContext& ctx) {
15921622
// ─── Global dependency cache: per-package keys, hit → stage edges ──
15931623
//
@@ -1728,30 +1758,7 @@ static std::expected<void, std::string> step13_dependency_cache(PrepareState& st
17281758
// below walks `mcpp::graph::topological_order` — dependencies before
17291759
// dependents — instead of recursing, with an explicit cycle check up
17301760
// front standing in for the old in-progress guard.
1731-
// The root's own `[dev-dependencies]` serve its tests, not its
1732-
// library, which a framework that uses the package under test reaches
1733-
// (names_the_root, graph_edges.cpp): the root's key does not fold them in.
1734-
auto serves_root_tests = [](const auto& e) {
1735-
return e.consumerPackageIndex == 0 && e.devTable;
1736-
};
1737-
mcpp::graph::AdjacencyList pkgDeps(state.packages.size());
1738-
for (auto& e : state.dependencyEdges)
1739-
if (e.consumerPackageIndex < pkgDeps.size() && !serves_root_tests(e))
1740-
pkgDeps[e.consumerPackageIndex].push_back(e.dependencyPackageIndex);
1741-
// An import a declared edge already covers adds nothing. One that a
1742-
// declared path runs the other way is a module this build generates
1743-
// for the importer below its consumer's root, filed under the
1744-
// consumer by its path (e2e 849): not an upstream, and left out
1745-
// rather than closing a cycle.
1746-
for (std::size_t p = 0; p < importedFrom.size(); ++p)
1747-
std::erase_if(importedFrom[p], [&](std::size_t q) {
1748-
if (std::ranges::find(pkgDeps[p], q) != pkgDeps[p].end()) return true;
1749-
const auto above = mcpp::graph::closure(pkgDeps, std::array{q}, /*include_roots=*/false);
1750-
return std::ranges::find(above, p) != above.end();
1751-
});
1752-
for (std::size_t p = 0; p < importedFrom.size(); ++p)
1753-
pkgDeps[p].insert(pkgDeps[p].end(), importedFrom[p].begin(), importedFrom[p].end());
1754-
auto keyOrder = mcpp::graph::topological_order(pkgDeps);
1761+
auto keyOrder = mcpp::graph::topological_order(key_graph(state, importedFrom));
17551762
if (!keyOrder) {
17561763
keyCycleError = std::format(
17571764
"dependency cycle through package '{}' while computing "

‎src/build/prepare/records.cpp‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ static std::string lock_identity(const mcpp::lockfile::LockedPackage& p) {
101101
return p.namespace_ + "\x1f" + lock_short_name(p);
102102
}
103103

104+
// THE IDENTITY THIS CONFIGURATION'S REQUEST WAS GIVEN (SPEC-016 §4.5), into
105+
// `lock`, and the other requests' as the lock had them. Returns what the lock
106+
// had for this request.
107+
static std::optional<mcpp::lockfile::LockedTarget> lock_targets(const PrepareState& state,
108+
mcpp::lockfile::Lockfile& lock) {
109+
std::optional<mcpp::lockfile::LockedTarget> priorTarget;
110+
auto const& c = state.targetChoice;
111+
if (auto prior = mcpp::lockfile::load(state.workRoot / "mcpp.lock"))
112+
for (auto const& t : prior->targets) {
113+
if (!c.request.empty() && t.request == c.request) priorTarget = t;
114+
else lock.targets.push_back(t);
115+
}
116+
if (!c.request.empty()) lock.targets.push_back({ c.request, c.identity, c.provider });
117+
std::ranges::sort(lock.targets, {}, &mcpp::lockfile::LockedTarget::request);
118+
return priorTarget;
119+
}
120+
121+
// The drift line of a request the lock gave another identity, if it did.
122+
static std::optional<std::string> target_drift(const PrepareState& state,
123+
const std::optional<mcpp::lockfile::LockedTarget>& prior) {
124+
auto const& c = state.targetChoice;
125+
if (!prior || prior->identity == c.identity) return std::nullopt;
126+
return std::format("target {}: {} ({}) -> {} ({}){}", c.request, prior->identity,
127+
prior->provider.empty() ? "the platform's" : prior->provider,
128+
c.identity, c.provider.empty() ? "the platform's" : c.provider,
129+
c.why.empty() ? std::string{} : "; " + c.why);
130+
}
131+
104132
std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildContext& ctx) {
105133
// Write/update mcpp.lock for any version-based deps that succeeded.
106134
// Path deps are intentionally NOT locked — their source is local filesystem.
@@ -413,19 +441,7 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
413441
}
414442
// One order whichever configuration wrote the file last: by identity.
415443
std::ranges::stable_sort(lock.packages, {}, [](auto const& p) { return lock_identity(p); });
416-
// THE IDENTITY THIS CONFIGURATION'S REQUEST WAS GIVEN (SPEC-016 §4.5),
417-
// and the other requests' as the lock had them.
418-
std::optional<mcpp::lockfile::LockedTarget> priorTarget;
419-
{
420-
auto const& c = state.targetChoice;
421-
if (auto prior = mcpp::lockfile::load(state.workRoot / "mcpp.lock"))
422-
for (auto const& t : prior->targets) {
423-
if (!c.request.empty() && t.request == c.request) priorTarget = t;
424-
else lock.targets.push_back(t);
425-
}
426-
if (!c.request.empty()) lock.targets.push_back({ c.request, c.identity, c.provider });
427-
std::ranges::sort(lock.targets, {}, &mcpp::lockfile::LockedTarget::request);
428-
}
444+
const auto priorTarget = lock_targets(state, lock);
429445
if (!lock.packages.empty() || !lock.indices.empty()) {
430446
auto lockPath = state.workRoot / "mcpp.lock";
431447
// `--locked` ASSERTS THAT THIS RESOLUTION IS THE RECORDED ONE.
@@ -459,12 +475,7 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
459475
for (auto const& p : prior->packages) was[key(p)] = p.version;
460476
for (auto const& p : lock.packages) now[key(p)] = p.version;
461477
std::vector<std::string> drift;
462-
if (auto const& c = state.targetChoice; priorTarget && priorTarget->identity != c.identity)
463-
drift.push_back(std::format(
464-
"target {}: {} ({}) -> {} ({}){}", c.request, priorTarget->identity,
465-
priorTarget->provider.empty() ? "the platform's" : priorTarget->provider,
466-
c.identity, c.provider.empty() ? "the platform's" : c.provider,
467-
c.why.empty() ? std::string{} : "; " + c.why));
478+
if (auto d = target_drift(state, priorTarget)) drift.push_back(std::move(*d));
468479
for (auto const& [k, v] : now) {
469480
auto it = was.find(k);
470481
if (it == was.end()) drift.push_back(k + " " + v + " (not in the lock)");

0 commit comments

Comments
 (0)