From ba9c09dea315cc24950157b3551d449c591cd243 Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Sat, 5 Dec 2026 13:33:17 +0900 Subject: [PATCH 1/2] fix(loader): a recipe outside an index is read with the host's index libs, as its hooks run (0.0.69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit load_package re-runs a spec 3 recipe's top level in the spec 3 environment (check_spec3_top_level), and that run took index libs only from the recipe's own index, three levels up. The executor's top level already read the host's index for a recipe in no index with libs (ReadTarget::pkgindex_dir), so a recipe the host keeps outside an index -- xlings' local overlay (`config --add-xpkg`), a recipe given by path -- installed but could not be read: "import: "xim.pkgindex.sysroot": this recipe is not in an index, which has no libs". Found by xim-pkgindex's registration test with xlings 2026.12.5.1. LoaderContext gains pkgindex_dir, the host's answer, and the check uses it the way the executor does: the recipe's own index first. The specification states it (ยง6.2); 0.0.69. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com> --- docs/spec/xpkg.md | 7 +++++-- mcpp.toml | 2 +- src/xpkg-loader.cppm | 22 +++++++++++++++++----- tests/test_spec3.cpp | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/docs/spec/xpkg.md b/docs/spec/xpkg.md index 1900dc1..d9813a3 100644 --- a/docs/spec/xpkg.md +++ b/docs/spec/xpkg.md @@ -1,6 +1,6 @@ # The xpkg format -> Status: in force. This is the specification of libxpkg 0.0.68. +> Status: in force. This is the specification of libxpkg 0.0.69. > Source: libxpkg `docs/spec/xpkg.md` (https://github.com/openxlings/libxpkg), > where it is edited. A copy, byte for byte the one of the libxpkg version > xlings builds with, is xlings `docs/specs/xpkg.md` @@ -410,7 +410,10 @@ its own: signature, what holds after, the version that added it and what differs by platform). A module that is not in the interface is an error, not a stub. - `import("xim.pkgindex.")` runs the index's `libs/.lua` in an - environment of the same rules and gives what it returns. + environment of the same rules and gives what it returns. A recipe in no + index with libs of its own (one given by path, a host's local overlay) + reads the libs of the index the host names, when it is read and in its + hooks alike. - The modules: `fs` (files), `system` (programs and the install's directories), `pkginfo` (what is installed), `path` (path strings), `log`, `xvm`, `subos`, `pkgmanager`, `elfpatch`, `json`, `base64`. diff --git a/mcpp.toml b/mcpp.toml index a46c9fe..f2d04ea 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "xpkg" -version = "0.0.68" +version = "0.0.69" description = "C++23 reference implementation of the xpkg format" license = "Apache-2.0" repo = "https://github.com/openxlings/libxpkg" diff --git a/src/xpkg-loader.cppm b/src/xpkg-loader.cppm index 1756554..9b34b99 100644 --- a/src/xpkg-loader.cppm +++ b/src/xpkg-loader.cppm @@ -26,6 +26,11 @@ using BuildOutput = std::function; struct LoaderContext { std::string platform; std::string arch; + // The index whose libs a recipe in no index with libs of its own imports + // at its top level (a recipe given by path, a host's local overlay): the + // host's answer, as the executor's ReadTarget::pkgindex_dir. A recipe's + // own index comes first. + std::string pkgindex_dir; }; struct MetadataInvocation { @@ -1137,7 +1142,8 @@ std::expected template_for(const fs::path& recipe) { // -- path, json and base64 are the real ones -- and index libs read as they are // imported. What fails there is a load error of the recipe: the same answer // search, an index build and an install get. -std::optional check_spec3_top_level(const fs::path& pkg_path, const fs::path& recipe_template) { +std::optional check_spec3_top_level(const fs::path& pkg_path, const fs::path& recipe_template, + const fs::path& host_index) { lua::State* L = lua::L_newstate(); if (!L) return "failed to create lua state"; lua::L_openlibs(L); @@ -1185,12 +1191,18 @@ std::optional check_spec3_top_level(const fs::path& pkg_path, const if (!problem) run(detail::spec3_lua, "the spec 3 environment", 0); if (!problem) { // Index libs are read from the recipe's index (pkgs//), - // three levels up the absolute path (a relative one has no parent there). + // three levels up the absolute path (a relative one has no parent there); + // a recipe in no index with libs reads the host's (host_index), as the + // executor's top level does. std::error_code ec; const auto absolute = fs::absolute(pkg_path, ec).lexically_normal(); - const auto index = (ec ? pkg_path : absolute).parent_path().parent_path().parent_path(); + auto index = (ec ? pkg_path : absolute).parent_path().parent_path().parent_path(); ec.clear(); - if (fs::is_directory(index / "libs", ec)) { + if (!fs::is_directory(index / "libs", ec)) { + index = host_index; + ec.clear(); + } + if (!index.empty() && fs::is_directory(index / "libs", ec)) { lua::pushstring(L, index.string().c_str()); lua::setglobal(L, "_PKGINDEX_DIR"); } @@ -1363,7 +1375,7 @@ load_package(const fs::path& pkg_path, const LoaderContext& context) { } } if (const auto spec = spec_number(p.spec); spec && *spec == 3) { - if (auto problem = loader_detail::check_spec3_top_level(pkg_path, *recipe_template)) + if (auto problem = loader_detail::check_spec3_top_level(pkg_path, *recipe_template, context.pkgindex_dir)) return std::unexpected("lua error: " + *problem); } return p; diff --git a/tests/test_spec3.cpp b/tests/test_spec3.cpp index 6929656..9581255 100644 --- a/tests/test_spec3.cpp +++ b/tests/test_spec3.cpp @@ -118,6 +118,38 @@ function install() return probelib.word() == "lib" end EXPECT_TRUE(dotted.success) << dotted.error; } +// A recipe in no index with libs of its own (a recipe given by path, a host's +// local overlay) is read with the libs of the index the host names +// (LoaderContext::pkgindex_dir), as its executor's top level is; its own +// index comes first. Without one, a top-level import is a load error. +TEST(Spec3EnvTest, ALoadedRecipeOutsideAnIndexReadsTheHostsIndexLibs) { + Index host; + host.lib("probelib", "local M = {}\nfunction M.word() return 'host' end\nreturn M\n"); + const auto body = std::string(R"LUA(package = { spec = "3", name = "loose", xpm = { linux = { ["1.0.0"] = {} } } } +import("xim.pkgindex.probelib") +local word = probelib.word() +function install() return true end +)LUA"); + const auto loose = host.root / "loose.lua"; // two levels from any index + std::ofstream(loose, std::ios::binary) << body; + + auto read = load_package(loose, LoaderContext{"linux", "x86_64", host.root.string()}); + EXPECT_TRUE(read.has_value()) << read.error(); + + auto alone = load_package(loose, LoaderContext{"linux", "x86_64", ""}); + ASSERT_FALSE(alone.has_value()); + EXPECT_NE(alone.error().find("not in an index"), std::string::npos) << alone.error(); + + // Its own index's lib is the one read: here it raises when called. + Index own; + own.lib("probelib", "local M = {}\nfunction M.word() error('own lib read') end\nreturn M\n"); + const auto inside = own.root / "pkgs" / "p" / "loose.lua"; + std::ofstream(inside, std::ios::binary) << body; + auto first = load_package(inside, LoaderContext{"linux", "x86_64", host.root.string()}); + ASSERT_FALSE(first.has_value()); + EXPECT_NE(first.error().find("own lib read"), std::string::npos) << first.error(); +} + TEST(Spec3EnvTest, TheSameTextWithSpec2HasTheCompatibilityLayer) { Index index; auto recipe = index.recipe(R"LUA( From b2bf8d66bae4b3996e4781236f748129ac3bff88 Mon Sep 17 00:00:00 2001 From: Sunrisepeak Date: Sat, 5 Dec 2026 13:35:15 +0900 Subject: [PATCH 2/2] fix(loader): an index built in process reads each recipe with the host's context build_index_impl's branch without boundaries read each recipe with an empty context and skipped what failed to load, so a host building its local overlay in process (xlings with no policy, the common home) left out a recipe that imports the host's index libs at its top level, silently. build_index(repo, ns, context, output) is that builder with the host's context (platform, architecture, pkgindex_dir); the three-argument one is unchanged (an empty context). Test: an index without libs built with the host's context has the recipe, and without it does not. Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com> --- src/xpkg-loader-boundary.cpp | 2 +- src/xpkg-loader.cppm | 9 +++++++++ tests/test_spec3.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/xpkg-loader-boundary.cpp b/src/xpkg-loader-boundary.cpp index 5bca056..292a378 100644 --- a/src/xpkg-loader-boundary.cpp +++ b/src/xpkg-loader-boundary.cpp @@ -79,7 +79,7 @@ build_index_impl(const fs::path& repo_dir, const std::string& defaultNamespace, for (auto& packagePath : packagePaths) { auto result = boundaries ? load_package(packagePath, context, boundaries->metadata) - : load_package(packagePath); + : load_package(packagePath, context); if (!result) { if (boundaries) return std::unexpected(packagePath.string() + ": " + result.error()); continue; // legacy callers skip malformed packages diff --git a/src/xpkg-loader.cppm b/src/xpkg-loader.cppm index 9b34b99..32b7e94 100644 --- a/src/xpkg-loader.cppm +++ b/src/xpkg-loader.cppm @@ -1422,6 +1422,15 @@ build_index(const fs::path& repo_dir, const std::string& defaultNamespace, return loader_detail::build_index_impl(repo_dir, defaultNamespace, buildOutput, {}, nullptr); } +// The same builder, each recipe read with the host's context: the platform +// and architecture its top level is told, and the index whose libs a recipe +// in an index without libs imports (a host's local overlay). +std::expected +build_index(const fs::path& repo_dir, const std::string& defaultNamespace, + const LoaderContext& context, const BuildOutput& buildOutput) { + return loader_detail::build_index_impl(repo_dir, defaultNamespace, buildOutput, context, nullptr); +} + // Both executable paths (metadata and pkgindex-build.lua) cross the supplied // boundaries. Unlike the legacy builder, a boundary failure fails the build. std::expected diff --git a/tests/test_spec3.cpp b/tests/test_spec3.cpp index 9581255..5840105 100644 --- a/tests/test_spec3.cpp +++ b/tests/test_spec3.cpp @@ -150,6 +150,30 @@ function install() return true end EXPECT_NE(first.error().find("own lib read"), std::string::npos) << first.error(); } +// An index without libs of its own (a host's local overlay) built in process +// reads each recipe with the context it is given: a recipe that imports a lib +// of the host's index at its top level is in the index. The builder without a +// context still skips what it cannot read. +TEST(Spec3EnvTest, AnIndexWithoutLibsIsBuiltWithTheHostsIndexLibs) { + Index host; + host.lib("probelib", "local M = {}\nfunction M.word() return 'host' end\nreturn M\n"); + const auto overlay = host.root / "overlay"; + fs::create_directories(overlay / "pkgs" / "l"); + std::ofstream(overlay / "pkgs" / "l" / "loose.lua", std::ios::binary) + << R"LUA(package = { spec = "3", name = "loose", xpm = { linux = { ["1.0.0"] = {} } } } +import("xim.pkgindex.probelib") +local word = probelib.word() +function install() return true end +)LUA"; + auto built = build_index(overlay, "local", LoaderContext{"linux", "x86_64", host.root.string()}, BuildOutput{}); + ASSERT_TRUE(built.has_value()) << built.error(); + EXPECT_EQ(built->entries.count("local:loose"), 1u); + + auto blind = build_index(overlay, "local"); + ASSERT_TRUE(blind.has_value()) << blind.error(); + EXPECT_EQ(blind->entries.count("local:loose"), 0u); +} + TEST(Spec3EnvTest, TheSameTextWithSpec2HasTheCompatibilityLayer) { Index index; auto recipe = index.recipe(R"LUA(