fix(build): refuse interface units that import each other

Two partitions that import each other, a partition that imports its own
module, and two modules of one configuration that import each other
cannot be built in any order. Build() compiles each stale unit on a
thread that first waits for the units its unit imports, and a primary
interface waits for every stale partition of its module as well, so a
build that found every unit of such a round stale, as a first build of
them does, hung: every thread waiting, no compiler running, no error.

ImportCycleError follows the imports of a configuration's interface
units at the top of that configuration's Build(), before it makes a
directory or builds a dependency, and Build() returns an error naming
the units of the first round it finds in the order they wait in,
"Ring:First -> Ring:Second -> Ring:First". A primary interface counts
as coming after each partition of its module, whether it imports that
one or not, since that is how it is built. A configuration built on the
one refused has started its own build by then: the check reads the
imports ResolvePendingImports has just placed, which are that Build()'s
to read while it runs.

The imports are the ones the source scanner reads, and it reads one in a
string literal, in a comment that spans lines or under #if 0 as it
reads any other (issue #45), so the units the error names are where to
look for one of those too. The error says so, since the compiler sees
no round in such a project, and AGENTS.template.md has it under Gotchas.
This commit is contained in:
Jorijn van der Graaf 2026-11-13 17:59:55 +01:00
commit e87dc4f015
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 72 additions and 0 deletions

View file

@ -533,6 +533,70 @@ namespace {
return error;
}
// Interface units of `config` that are each built after the next, round
// to the first again: partitions that import each other, a partition that
// imports its own module, sibling modules that import each other. Empty
// when there are none, otherwise the error naming the round. No order
// builds such units: each is compiled on a thread that first waits for
// the units it imports, a primary interface for the partitions of its
// module as well, so such a build hung with no compiler running.
//
// The imports are the scanner's, which reads one in a string literal, in
// a comment that spans lines or under `#if 0` as it reads any other
// (issue #45): the units named are where to look for one of those as
// well, and the error says so, since the compiler sees no round there.
std::string ImportCycleError(const Configuration& config) {
struct Unit {
std::string name;
std::vector<const void*> builtAfter;
bool entered = false;
bool left = false;
};
// By address: a module and a partition are different types, and a
// unit's imports name either. Walked in the order the units were
// declared, so the same project gets the same message every time.
std::unordered_map<const void*, Unit> units;
std::vector<const void*> declared;
for (const std::unique_ptr<Module>& module : config.interfaces) {
declared.push_back(module.get());
Unit& primary = units[module.get()];
primary.name = module->name;
for (const Module* imported : module->moduleDependencies) primary.builtAfter.push_back(imported);
for (const std::unique_ptr<ModulePartition>& partition : module->partitions) {
// The primary interface is built after every partition of
// its module, whether it imports that one or not.
primary.builtAfter.push_back(partition.get());
declared.push_back(partition.get());
Unit& unit = units[partition.get()];
unit.name = std::format("{}:{}", module->name, partition->name);
for (const ModulePartition* imported : partition->partitionDependencies) unit.builtAfter.push_back(imported);
for (const Module* imported : partition->moduleDependencies) unit.builtAfter.push_back(imported);
}
}
std::vector<const Unit*> chain;
std::string round;
std::function<void(Unit&)> visit = [&](Unit& unit) {
if (!round.empty() || unit.left) return;
if (unit.entered) {
// Back at a unit this walk is still inside of: the round is
// the chain from where it was entered.
for (auto it = std::ranges::find(chain, &unit); it != chain.end(); ++it) round += std::format("{} -> ", (*it)->name);
round += unit.name;
return;
}
unit.entered = true;
chain.push_back(&unit);
for (const void* before : unit.builtAfter) {
if (const auto found = units.find(before); found != units.end()) visit(found->second);
}
chain.pop_back();
unit.left = true;
};
for (const void* unit : declared) visit(units.at(unit));
if (round.empty()) return "";
return std::format("{}: these interface units are each built after the next, so none can be built first: {}. A unit is built after the modules and partitions it imports, and a module's primary interface after all of its partitions. The imports are read from the source text without compiling it: one in a string literal, in a comment that spans lines or under #if 0 counts too.", config.name, round);
}
// What a wasm32 configuration cannot be built with now that every unit
// has C++ exceptions, and what its sysroot has to have for that; empty
// for any other target and when nothing is wrong. Each would otherwise
@ -733,6 +797,13 @@ BuildResult Crafter::Build(Configuration& config, std::unordered_map<fs::path, s
// DAG fully wired — closes that window for every caller rather than for the
// ones that remember to declare in the right order.
config.ResolvePendingImports();
// With every import placed, and before this configuration makes a
// directory or builds a dependency. A configuration built on this one
// has started its own build by then: the imports just placed are this
// Build()'s to read, so no other configuration's can check them for it.
if (std::string cycle = ImportCycleError(config); !cycle.empty()) {
return {std::move(cycle), false, {}};
}
// Auto-detect the WASI sysroot before any compile step runs so BuildStdPcm
// and the main compile command see the same value. Covers all wasm32-*