test(external): a sibling's pointers to its own Configuration hold

SiblingOwnPointers loads a fixture through LocalProject, on a fresh copy
outside the checkout: a library whose project file declares a test with
cfg.AddTest("OwnTest").Dependencies({ &cfg }). The one dependency of
that test has to be the Configuration LocalProject returns. The test
reads the dependency out of that Configuration and compares the two
addresses, without following the dependency.

Against the loader as it was before the fix the test fails: the
dependency is not that Configuration.
This commit is contained in:
Jorijn van der Graaf 2026-11-14 23:42:47 +01:00
commit 1440e20231
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 77 additions and 0 deletions

View file

@ -334,6 +334,8 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("DependencyNotes").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("SiblingOwnPointers").Dependencies({ CrafterBuildLib.get() })
.LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl");
cfg.AddTest("ConcurrentDependencyReset").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("JobGroup").Dependencies({ CrafterBuildLib.get() });
cfg.AddTest("JobLimit").Dependencies({ CrafterBuildLib.get() });

View file

@ -0,0 +1,20 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
import std;
import Crafter.Build;
using namespace Crafter;
// A library whose test depends on it: the test's dependency is a pointer to
// the Configuration this function returns. A library with no sources is
// enough, since the test that loads this file reads the pointer and builds
// nothing.
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view>) {
Configuration cfg;
cfg.path = "./";
cfg.name = "own";
cfg.outputName = "own";
cfg.type = ConfigurationType::LibraryStatic;
cfg.AddTest("OwnTest").Dependencies({ &cfg });
return cfg;
}

View file

@ -0,0 +1,6 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
int main() {
return 0;
}

View file

@ -0,0 +1,49 @@
// 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;
// A project file may keep pointers to the Configuration it returns: a test
// that links the library the file describes is declared with
// cfg.AddTest("T").Dependencies({ &cfg }). Loaded by another project through
// LocalProject or GitProject, that Configuration has to stay where the
// project file built it.
//
// The loader moved it: out of the call's return value, and then into its
// cache. The test's dependency was left pointing at an object that no
// longer existed, and whatever followed it read freed memory.
// `crafter-build lint` follows it for a sibling inside the project's own
// directory, when it works out the compile command of each of the sibling's
// tests for a rule that needs an AST.
int main() {
const fs::path installedHome = GetCrafterBuildHome();
if (!fs::exists(installedHome / "Crafter.Build.cppm")) {
std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string());
return 77;
}
// Staged because the load drops a build/ tree next to the project file.
const fs::path scratch = fs::temp_directory_path() / "crafter-build-sibling-own-pointers";
std::error_code ec;
fs::remove_all(scratch, ec);
fs::copy(fs::current_path() / "tests" / "SiblingOwnPointers" / "fixture", scratch, fs::copy_options::recursive);
Configuration* sibling = nullptr;
try {
sibling = LocalProject({ .projectFile = scratch / "project.cpp" });
} catch (const std::exception& e) {
std::println(std::cerr, "FAIL: loading the sibling threw: {}", e.what());
return 1;
}
// Compared, never followed: where the two differ, the pointer leads
// nowhere.
if (sibling->tests.size() != 1 || sibling->tests[0].config.dependencies != std::vector<Configuration*>{sibling}) {
std::println(std::cerr, "FAIL: the pointer a sibling's project file took to the Configuration it returns is not the Configuration its consumers get");
return 1;
}
fs::remove_all(scratch, ec);
return 0;
}