From e87dc4f015487f6820a61268e4fa5a9335a765cc Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Fri, 13 Nov 2026 17:59:55 +0100 Subject: [PATCH] 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. --- AGENTS.template.md | 1 + implementations/Crafter.Build-Clang.cpp | 71 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/AGENTS.template.md b/AGENTS.template.md index a99827a..b6b1678 100644 --- a/AGENTS.template.md +++ b/AGENTS.template.md @@ -198,6 +198,7 @@ Local builds are two scripts in `project.cpp`. Run them with the native binary: - A `#pragma crafter dependency` target is rebuilt on every load on purpose: clang crashes rather than erroring when it imports a BMI whose own imported PCM was rebuilt underneath it, and the depfile check is what catches a regenerated host cache. Do not optimise the rebuild away. - A host-tooling library such as Crafter.LintRules may `import Crafter.Build;` without declaring it as a dependency, because `LoadProject` fills the cache it resolves from. Listing Crafter.Build in its `cfg.dependencies` would clone and build all of crafter-build for every consumer. Its test does link crafter-build, so that test is wired behind `IsTopLevelProject()`: without the guard, crafter-build loading the library as a dependency would load its own `project.cpp` again through the test's `GitProject` and hit the cycle guard. - `crafter-build clean` leaves a pragma dependency's build alone; it lives with the clone in the cache. +- The source scanner follows an `import` it reads in a string literal or under `#if 0` like any other (issue #45), so interface units can import each other by its account alone. `ImportCycleError` refuses such a round, naming the units, at the top of the `Build()` of the configuration whose units form it: before that configuration makes a directory or builds a dependency, though a configuration built on it has started its own build by then. - Units see a std module rebuilt in place, because their depfiles list the BMIs they import (`-fmodule-file-deps`), but not one that moved to another directory: their old depfiles still name the old one. So anything that changes how the std module is compiled goes into `StdPcmFlags`, which `BuildStdPcm` compiles with and `StdPcmKey` hashes. `VariantId()` hashes `StdPcmKey`, so the build moves with it. A flag added to the command anywhere else leaves both directories where they are. - A CMake external whose recorded configure inputs (`/.crafter-options`) differ from this build's is built again from an empty build directory, not reconfigured in place: CMake keeps what an old compiler or `--sysroot` compiled, and the check results it cached. - The bootstrap binary is compiled without `CRAFTER_BUILD_HAS_ASSET`, so `cfg.assets` only works in the self-hosted binary. What an asset is compressed to is decided in one place, `CompressedAssetName` in `Crafter.Build-Clang.cpp`, which an executable's own assets, the ones its dependencies forward and the browser runtime's `files.json` all use; a Flatpak's `files/bin` keeps what those copies record as placed. `CompressAsset` in `Crafter.Build-Asset.cpp` does the compressing and has to load every extension that function maps. diff --git a/implementations/Crafter.Build-Clang.cpp b/implementations/Crafter.Build-Clang.cpp index 7ca09c0..a2fe898 100644 --- a/implementations/Crafter.Build-Clang.cpp +++ b/implementations/Crafter.Build-Clang.cpp @@ -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 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 units; + std::vector declared; + for (const std::unique_ptr& 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& 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 chain; + std::string round; + std::function 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