From 8baf14068ea434b46cf63e5c2565374755e7014c Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sat, 14 Nov 2026 20:19:19 +0100 Subject: [PATCH] fix(external): keep a sibling project's Configuration where it was built A project file may take pointers to the Configuration it returns. The usual one belongs to a test that links the library the file describes, cfg.AddTest("T").Dependencies({ &cfg }): where the compiler constructs the local the file returns in place of the return value, &cfg is the address of what CrafterBuildProject returns. For the project crafter-build is pointed at, that address stays good: Run() initialises its Configuration with the call. For a project loaded through GitProject or LocalProject it did not. LoadProjectFromRoot assigned the call's result to a local, and from there the Configuration was moved into the project cache, so what a consumer got was not the object the project file had built, and every pointer the file had taken to that one pointed at an object that no longer existed. RunLint follows such a pointer: for a sibling whose path lies inside the project's directory it works out the compile command of each of the sibling's tests when a rule needs an AST, and GetCompileCommand walks the dependencies of the configuration it is given. LoadProjectFromRoot now makes the Configuration on the heap with the call as its initialiser, and StoreProject takes the unique_ptr, so the Configuration is constructed where the cache keeps it and is never moved. --- implementations/Crafter.Build-External.cpp | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/implementations/Crafter.Build-External.cpp b/implementations/Crafter.Build-External.cpp index 3fb8fe9..0a0c7a2 100644 --- a/implementations/Crafter.Build-External.cpp +++ b/implementations/Crafter.Build-External.cpp @@ -348,17 +348,28 @@ namespace { // absolute so Build() can run from any cwd without misresolving them. // The mutex is NOT held across LoadProject — the dep may itself call // GitProject/LocalProject, and a non-recursive lock would deadlock. - Configuration LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span args) { + // + // The Configuration is made on the heap with the call as its + // initialiser, so it is constructed where the cache keeps it and is + // never moved. A project file takes pointers to the Configuration it + // returns, for a test that links the library it describes + // (cfg.AddTest(...).Dependencies({ &cfg })), and they are pointers to + // this object when the compiler constructed the local the file returns + // in place of the return value, as clang does for a function that + // returns one local. Assigned to a local here and moved into the cache + // from there, the Configuration left every such pointer behind. + std::unique_ptr LoadProjectFromRoot(const fs::path& projectPath, const fs::path& depRoot, std::span args) { fs::path savedCwd = fs::current_path(); - Configuration cfg; + std::unique_ptr made; fs::current_path(depRoot); try { - cfg = LoadProject(projectPath, args); + made.reset(new Configuration(LoadProject(projectPath, args))); } catch (...) { fs::current_path(savedCwd); throw; } fs::current_path(savedCwd); + Configuration& cfg = *made; cfg.path = fs::absolute(depRoot / cfg.path).lexically_normal(); auto abs = [&](const fs::path& p) -> fs::path { return p.is_absolute() ? p : fs::absolute(depRoot / p).lexically_normal(); @@ -378,20 +389,19 @@ namespace { for (auto& entry : cfg.flatpak.share) { if (!entry.source.empty()) entry.source = abs(entry.source); } - return cfg; + return made; } // Into ProjectCache under `fullHash`, with `loaded` recorded beside it. // When another thread stored the same spec first, its Configuration // wins and `cfg` is dropped. - Configuration* StoreProject(std::string fullHash, Configuration cfg, LoadedProject loaded) { + Configuration* StoreProject(std::string fullHash, std::unique_ptr cfg, LoadedProject loaded) { std::lock_guard lock(ProjectCacheMutex); if (auto it = ProjectCache.find(fullHash); it != ProjectCache.end()) { return it->second.get(); } - auto stored = std::make_unique(std::move(cfg)); - Configuration* ptr = stored.get(); - ProjectCache.emplace(std::move(fullHash), std::move(stored)); + Configuration* ptr = cfg.get(); + ProjectCache.emplace(std::move(fullHash), std::move(cfg)); LoadedProjects.emplace(ptr, std::move(loaded)); return ptr; } @@ -461,7 +471,7 @@ Configuration* Crafter::GitProject(const GitProjectSpec& spec) { } std::vector argViews(spec.args.begin(), spec.args.end()); - Configuration cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews); + std::unique_ptr cfg = LoadProjectFromRoot(projectPath, cloneDir, argViews); const fs::path projectInRepo = spec.projectFile.lexically_normal(); const fs::path below = projectInRepo.parent_path(); @@ -522,7 +532,7 @@ Configuration* Crafter::LocalProject(const LocalProjectSpec& spec) { fs::path depRoot = canonical.parent_path(); std::vector argViews(spec.args.begin(), spec.args.end()); - Configuration cfg = LoadProjectFromRoot(canonical, depRoot, argViews); + std::unique_ptr cfg = LoadProjectFromRoot(canonical, depRoot, argViews); return StoreProject(std::move(fullHash), std::move(cfg), { .projectFile = canonical,