From ee331377a415e08ae46aa55cc529ba5b8302e76c Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Fri, 13 Nov 2026 17:59:55 +0100 Subject: [PATCH] test(build): interface units that import each other are refused ImportCycle builds three fixtures as static libraries, each on a fresh copy outside the checkout: two partitions that import each other, a partition that imports its own module, and two modules that import each other. Each build has to return an error that holds its round, "Ring:First -> Ring:Second -> Ring:First", "Own -> Own:Part -> Own" and "East -> West -> East", and leave neither a build nor a bin directory. Before the refusal the test ran into its timeout: the first of those builds never returned. --- project.cpp | 1 + .../fixture/own-module/Own-Part.cppm | 7 ++ tests/ImportCycle/fixture/own-module/Own.cppm | 5 ++ .../fixture/partitions/Ring-First.cppm | 7 ++ .../fixture/partitions/Ring-Second.cppm | 7 ++ .../ImportCycle/fixture/partitions/Ring.cppm | 6 ++ tests/ImportCycle/fixture/siblings/East.cppm | 7 ++ tests/ImportCycle/fixture/siblings/West.cppm | 7 ++ tests/ImportCycle/main.cpp | 79 +++++++++++++++++++ 9 files changed, 126 insertions(+) create mode 100644 tests/ImportCycle/fixture/own-module/Own-Part.cppm create mode 100644 tests/ImportCycle/fixture/own-module/Own.cppm create mode 100644 tests/ImportCycle/fixture/partitions/Ring-First.cppm create mode 100644 tests/ImportCycle/fixture/partitions/Ring-Second.cppm create mode 100644 tests/ImportCycle/fixture/partitions/Ring.cppm create mode 100644 tests/ImportCycle/fixture/siblings/East.cppm create mode 100644 tests/ImportCycle/fixture/siblings/West.cppm create mode 100644 tests/ImportCycle/main.cpp diff --git a/project.cpp b/project.cpp index cd1e206..6674515 100644 --- a/project.cpp +++ b/project.cpp @@ -332,6 +332,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("DependencyNotes").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("ImportCycle").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("Lint").Dependencies({ CrafterBuildLib.get() }); } diff --git a/tests/ImportCycle/fixture/own-module/Own-Part.cppm b/tests/ImportCycle/fixture/own-module/Own-Part.cppm new file mode 100644 index 0000000..5da0aee --- /dev/null +++ b/tests/ImportCycle/fixture/own-module/Own-Part.cppm @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Own:Part; +import Own; + +export bool Part(); diff --git a/tests/ImportCycle/fixture/own-module/Own.cppm b/tests/ImportCycle/fixture/own-module/Own.cppm new file mode 100644 index 0000000..89bcb52 --- /dev/null +++ b/tests/ImportCycle/fixture/own-module/Own.cppm @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Own; +export import :Part; diff --git a/tests/ImportCycle/fixture/partitions/Ring-First.cppm b/tests/ImportCycle/fixture/partitions/Ring-First.cppm new file mode 100644 index 0000000..808b35e --- /dev/null +++ b/tests/ImportCycle/fixture/partitions/Ring-First.cppm @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Ring:First; +import :Second; + +export bool First(); diff --git a/tests/ImportCycle/fixture/partitions/Ring-Second.cppm b/tests/ImportCycle/fixture/partitions/Ring-Second.cppm new file mode 100644 index 0000000..5bee378 --- /dev/null +++ b/tests/ImportCycle/fixture/partitions/Ring-Second.cppm @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Ring:Second; +import :First; + +export bool Second(); diff --git a/tests/ImportCycle/fixture/partitions/Ring.cppm b/tests/ImportCycle/fixture/partitions/Ring.cppm new file mode 100644 index 0000000..34c7c71 --- /dev/null +++ b/tests/ImportCycle/fixture/partitions/Ring.cppm @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Ring; +export import :First; +export import :Second; diff --git a/tests/ImportCycle/fixture/siblings/East.cppm b/tests/ImportCycle/fixture/siblings/East.cppm new file mode 100644 index 0000000..141d14b --- /dev/null +++ b/tests/ImportCycle/fixture/siblings/East.cppm @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module East; +import West; + +export bool East(); diff --git a/tests/ImportCycle/fixture/siblings/West.cppm b/tests/ImportCycle/fixture/siblings/West.cppm new file mode 100644 index 0000000..0f0e7d3 --- /dev/null +++ b/tests/ImportCycle/fixture/siblings/West.cppm @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module West; +import East; + +export bool West(); diff --git a/tests/ImportCycle/main.cpp b/tests/ImportCycle/main.cpp new file mode 100644 index 0000000..618d8e3 --- /dev/null +++ b/tests/ImportCycle/main.cpp @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +namespace fs = std::filesystem; +using namespace Crafter; + +// Interface units that import each other are refused before their +// configuration builds anything, with an error naming them. No order builds +// them. Before the refusal such a build hung with no compiler running, each +// unit waiting for the units it imports, which is how this test failed then: +// by running into its timeout. +// +// Three ways round: two partitions that import each other, a partition that +// imports its own module, whose primary interface is built after all of its +// partitions, and two modules of one configuration that import each other. + +namespace { + std::int32_t Failures = 0; + + void Check(bool cond, std::string_view msg) { + if (!cond) { + std::println(std::cerr, "FAIL: {}", msg); + ++Failures; + } + } + + // Builds the interfaces of the fixture in `dir` as a library and returns + // Build()'s error. On a copy outside the checkout, made anew each run: a + // build that gets past the refusal has made its directories before it + // fails, and in the checkout the next run would find them there and + // report them against a refusal that works. + std::string BuildError(std::string_view dir, std::span interfaces) { + const fs::path staged = fs::temp_directory_path() / "crafter-build-import-cycle" / dir; + fs::remove_all(staged); + fs::create_directories(staged.parent_path()); + fs::copy(fs::current_path() / "tests" / "ImportCycle" / "fixture" / dir, staged, fs::copy_options::recursive); + + Configuration cfg; + cfg.path = staged; + cfg.name = std::format("cycle-{}", dir); + cfg.outputName = cfg.name; + cfg.target = HostTarget(); + cfg.type = ConfigurationType::LibraryStatic; + std::array implementations = {}; + cfg.GetInterfacesAndImplementations(interfaces, implementations); + + std::unordered_map> depResults; + std::mutex depMutex; + const BuildResult built = Build(cfg, depResults, depMutex); + // Refused like a graph with a module built twice: before a directory + // is made for it. + Check(!fs::exists(cfg.BuildDir()) && !fs::exists(cfg.BinDir()), std::format("{}: the refused build left {} or {} behind", dir, cfg.BuildDir().string(), cfg.BinDir().string())); + return built.result; + } +} + +int main() { + { + std::array interfaces = { "Ring", "Ring-First", "Ring-Second" }; + const std::string error = BuildError("partitions", interfaces); + Check(error.contains("Ring:First -> Ring:Second -> Ring:First"), std::format("partitions that import each other are named in the order they wait in, got: '{}'", error)); + // The scanner reads imports the compiler does not see, so the error + // has to say where a round nobody wrote can come from. + Check(error.contains("string literal") && error.contains("#if 0"), std::format("the error says which text the scanner reads as an import, got: '{}'", error)); + } + { + std::array interfaces = { "Own", "Own-Part" }; + const std::string error = BuildError("own-module", interfaces); + Check(error.contains("Own -> Own:Part -> Own"), std::format("a partition that imports its own module is named with it, got: '{}'", error)); + } + { + std::array interfaces = { "East", "West" }; + const std::string error = BuildError("siblings", interfaces); + Check(error.contains("East -> West -> East"), std::format("modules that import each other are named, got: '{}'", error)); + } + return Failures == 0 ? 0 : 1; +}