Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions libmamba/src/api/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,35 @@
#include "mamba/solver/libsolv/repo_info.hpp"
#include "mamba/util/string.hpp"

#include "utils.hpp"

namespace mamba
{
namespace
{
auto
repoquery_init(Context& ctx, Configuration& config, QueryResultFormat format, bool use_local)
auto repoquery_root_packages(QueryType type, const std::vector<std::string>& queries)
-> std::vector<std::string>
{
if (type == QueryType::Depends)
{
return extract_package_names_from_specs(queries);
}
if (type == QueryType::Search)
{
// Search queries that are not exact names (e.g. globs) require full repodata.
return extract_exact_package_names_from_specs(queries);
}
return {};
}

auto repoquery_init(
Context& ctx,
Configuration& config,
QueryType type,
QueryResultFormat format,
bool use_local,
const std::vector<std::string>& queries
)
{
config.at("use_target_prefix_fallback").set_value(true);
config.at("use_default_prefix_fallback").set_value(true);
Expand Down Expand Up @@ -77,7 +100,10 @@ namespace mamba
{
Console::stream() << "Getting repodata from channels..." << std::endl;
}
auto exp_load = load_channels(ctx, channel_context, db, package_caches, {});
const auto root_packages = ctx.repodata_use_shards
? repoquery_root_packages(type, queries)
: std::vector<std::string>{};
auto exp_load = load_channels(ctx, channel_context, db, package_caches, root_packages);
if (!exp_load)
{
throw std::runtime_error(exp_load.error().what());
Expand Down Expand Up @@ -189,7 +215,7 @@ namespace mamba
)
{
auto& ctx = config.context();
auto db = repoquery_init(ctx, config, format, use_local);
auto db = repoquery_init(ctx, config, type, format, use_local, queries);
return make_repoquery(
db,
type,
Expand Down
22 changes: 22 additions & 0 deletions libmamba/src/api/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,28 @@ namespace mamba
return names;
}

std::vector<std::string>
extract_exact_package_names_from_specs(const std::vector<std::string>& specs)
{
std::vector<std::string> names;
names.reserve(specs.size());
for (const auto& s : specs)
{
auto parsed = specs::MatchSpec::parse(s);
if (!parsed || parsed->name().is_free() || !parsed->name().is_exact())
{
return {};
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to log the failing parsing and continue?

auto name = parsed->name().to_string();
if (name.empty())
{
return {};
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark here.

names.push_back(std::move(name));
}
return names;
}

void add_pip_if_python(std::vector<std::string>& root_packages)
{
bool has_python = false;
Expand Down
7 changes: 7 additions & 0 deletions libmamba/src/api/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ namespace mamba
*/
std::vector<std::string> extract_package_names_from_specs(const std::vector<std::string>& specs);

/**
* Extract exact package names from matchspec strings.
* Returns an empty vector if any query is not an exact package name.
*/
std::vector<std::string>
extract_exact_package_names_from_specs(const std::vector<std::string>& specs);

/**
* Ensure that ``"pip"`` is present in ``root_packages`` when ``"python"`` is requested.
*
Expand Down
15 changes: 14 additions & 1 deletion libmamba/src/core/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "mamba/specs/version.hpp"
#include "mamba/util/string.hpp"

#include "transaction_context.hpp"

namespace mamba
{
/******************************
Expand Down Expand Up @@ -230,7 +232,18 @@ namespace mamba
.value();
database.for_each_package_matching(
ms,
[&](specs::PackageInfo&& pkg) { g.add_node(std::move(pkg)); }
[&](specs::PackageInfo&& pkg)
{
if (pkg.name == "python")
{
if (auto effective = effective_python_site_packages_path(pkg);
!effective.empty())
{
pkg.python_site_packages_path = std::move(effective);
}
}
g.add_node(std::move(pkg));
}
);
}

Expand Down
68 changes: 0 additions & 68 deletions libmamba/src/core/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,74 +116,6 @@ namespace mamba
return out;
}

/**
* Prefer repodata's `python_site_packages_path`; if missing, infer free-threaded layout
* (`lib/pythonX.Yt/site-packages`) from CPython's conda build string segment (`cpVVVt`).
*
* On Unix-like platforms, repodata may still advertise `lib/pythonX.Y/site-packages` for
* `cpVVVt` builds; in that case use the free-threaded layout so `noarch` paths match the
* interpreter (`lib/pythonX.Yt/...`).
*
* On Windows, always use the canonical `Lib/site-packages` location.
*/
auto effective_python_site_packages_path(const specs::PackageInfo& python_pkg) -> std::string
{
#ifdef _WIN32
// On Windows, `libmamba` should install all python packages into the
// canonical `Lib/site-packages` location (independently of the python version
// and of freethreading).
const std::string canonical_site_packages = (fs::u8path("Lib") / "site-packages")
.generic_string();

return canonical_site_packages;
#else
const std::string short_ver = compute_short_python_version(python_pkg.version);
std::string compact = short_ver;
util::replace_all(compact, ".", "");
const bool freethreaded_build = !python_pkg.build_string.empty() && !compact.empty()
&& util::contains(
python_pkg.build_string,
util::concat("cp", compact, "t")
);

const std::string ft_site_packages = (short_ver.empty()
|| python_pkg.build_string.empty())
? std::string{}
: (fs::u8path("lib")
/ util::concat("python", short_ver, "t")
/ "site-packages")
.generic_string();

if (!python_pkg.python_site_packages_path.empty())
{
if (freethreaded_build && !ft_site_packages.empty())
{
const std::string std_site_packages = short_ver.empty()
? std::string{}
: (fs::u8path("lib")
/ util::concat("python", short_ver)
/ "site-packages")
.generic_string();
if (python_pkg.python_site_packages_path == std_site_packages)
{
return ft_site_packages;
}
}
return python_pkg.python_site_packages_path;
}

if (short_ver.empty() || python_pkg.build_string.empty())
{
return {};
}
if (!freethreaded_build)
{
return {};
}
return ft_site_packages;
#endif
}

auto find_python_versions_and_site_packages(
const solver::Solution& solution,
const solver::libsolv::Database& database
Expand Down
57 changes: 57 additions & 0 deletions libmamba/src/core/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,63 @@ namespace mamba
return util::concat(sv[0], '.', sv[1]);
}

auto effective_python_site_packages_path(const specs::PackageInfo& python_pkg) -> std::string
{
#ifdef _WIN32
// On Windows, `libmamba` should install all python packages into the
// canonical `Lib/site-packages` location (independently of the python version
// and of freethreading).
const std::string canonical_site_packages = (fs::u8path("Lib") / "site-packages")
.generic_string();

return canonical_site_packages;
#else
const std::string short_ver = compute_short_python_version(python_pkg.version);
std::string compact = short_ver;
util::replace_all(compact, ".", "");
const bool freethreaded_build = !python_pkg.build_string.empty() && !compact.empty()
&& util::contains(
python_pkg.build_string,
util::concat("cp", compact, "t")
);

const std::string ft_site_packages = (short_ver.empty() || python_pkg.build_string.empty())
? std::string{}
: (fs::u8path("lib")
/ util::concat("python", short_ver, "t")
/ "site-packages")
.generic_string();

if (!python_pkg.python_site_packages_path.empty())
{
if (freethreaded_build && !ft_site_packages.empty())
{
const std::string std_site_packages = short_ver.empty()
? std::string{}
: (fs::u8path("lib")
/ util::concat("python", short_ver)
/ "site-packages")
.generic_string();
if (python_pkg.python_site_packages_path == std_site_packages)
{
return ft_site_packages;
}
}
return python_pkg.python_site_packages_path;
}

if (short_ver.empty() || python_pkg.build_string.empty())
{
return {};
}
if (!freethreaded_build)
{
return {};
}
return ft_site_packages;
#endif
}

// supply short python version, e.g. 2.7, 3.5...
fs::u8path get_python_short_path(const std::string& python_version [[maybe_unused]])
{
Expand Down
2 changes: 2 additions & 0 deletions libmamba/src/core/transaction_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#include "mamba/core/util.hpp"
#include "mamba/fs/filesystem.hpp"
#include "mamba/specs/match_spec.hpp"
#include "mamba/specs/package_info.hpp"

namespace mamba
{
std::string compute_short_python_version(const std::string& long_version);
std::string effective_python_site_packages_path(const specs::PackageInfo& python_pkg);
// supply short python version, e.g. 2.7, 3.5...
fs::u8path get_python_short_path(const std::string& python_version);
fs::u8path get_python_site_packages_short_path(const std::string& python_version);
Expand Down
1 change: 1 addition & 0 deletions libmamba/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ set(
src/core/test_pinning.cpp
src/core/test_progress_bar.cpp
src/core/test_query.cpp
src/core/test_repoquery.cpp
src/core/test_shell_init.cpp
src/core/test_shard_python_minor_prefilter.cpp
src/core/test_shards.cpp
Expand Down
90 changes: 90 additions & 0 deletions libmamba/tests/src/core/test_repoquery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2026, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.

#include <vector>

#include <catch2/catch_all.hpp>

namespace mamba
{
std::vector<std::string> extract_package_names_from_specs(const std::vector<std::string>& specs);
std::vector<std::string>
extract_exact_package_names_from_specs(const std::vector<std::string>& specs);
}

using namespace mamba;

TEST_CASE("repoquery sharded roots extraction", "[mamba::api][repoquery]")
{
SECTION("extract names from valid MatchSpecs")
{
const std::vector<std::string> specs = {
"xtensor=0.24.5",
"conda-forge::python>=3.11",
"xsimd[version='>=13']",
};

const auto names = extract_package_names_from_specs(specs);
REQUIRE(names == std::vector<std::string>{ "xtensor", "python", "xsimd" });
}

SECTION("ignore name-free specs and keep valid names")
{
const std::vector<std::string> specs = {
"numpy<2",
">=1.0",
};

const auto names = extract_package_names_from_specs(specs);
REQUIRE(names == std::vector<std::string>{ "numpy" });
}

SECTION("empty when all specs are name-free")
{
const std::vector<std::string> specs = {
">=1.0",
"<2",
};

const auto names = extract_package_names_from_specs(specs);
REQUIRE(names.empty());
}
}

TEST_CASE("repoquery search exact roots extraction", "[mamba::api][repoquery]")
{
SECTION("extract exact names")
{
const std::vector<std::string> specs = {
"xtensor",
"python=3.12",
};

const auto names = extract_exact_package_names_from_specs(specs);
REQUIRE(names == std::vector<std::string>{ "xtensor", "python" });
}

SECTION("reject glob queries")
{
const std::vector<std::string> specs = {
"xtensor*",
};

const auto names = extract_exact_package_names_from_specs(specs);
REQUIRE(names.empty());
}

SECTION("reject mixed exact and glob queries")
{
const std::vector<std::string> specs = {
"xtensor",
"xsimd*",
};

const auto names = extract_exact_package_names_from_specs(specs);
REQUIRE(names.empty());
}
}
5 changes: 4 additions & 1 deletion micromamba/tests/test_repoquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ def test_remote_search_python_site_packages_path(yaml_env: Path):
assert package_info["name"] == "python"
assert package_info["version"] == "3.13.1"
assert package_info["track_features"] == "py_freethreading"
assert package_info["python_site_packages_path"] == "lib/python3.13t/site-packages"
if platform.system() == "Windows":
assert package_info["python_site_packages_path"] == "Lib/site-packages"
else:
assert package_info["python_site_packages_path"] == "lib/python3.13t/site-packages"
Comment on lines +264 to +267
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? This is based on:

// On Windows, `libmamba` should install all python packages into the
// canonical `Lib/site-packages` location (independently of the python version
// and of freethreading).
const std::string canonical_site_packages = (fs::u8path("Lib") / "site-packages")
.generic_string();
return canonical_site_packages;

which is based on what I have observed.



@pytest.mark.parametrize("wildcard", [False, True])
Expand Down
Loading