feat(daemon): materialize project dependencies for daemon build requests
This commit is contained in:
1 parent
42172a24f8
commit
f5c0c9f09d
12 files changed
+580
-287
No files matched your search
+14
-267
@@ -11,6 +11,7 @@
|
||||
#include <Void/Build/Package/Fetch.hpp>
|
||||
#include <Void/Build/Package/Lockfile.hpp>
|
||||
#include <Void/Build/Package/RegistryClient.hpp>
|
||||
#include <Void/Build/Package/Resolver.hpp>
|
||||
#include <Void/Build/Package/Solver.hpp>
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
@@ -260,279 +261,25 @@ SnapshotInputs collect_snapshot_inputs(const Graph::ProjectDescription &desc,
|
||||
return out;
|
||||
}
|
||||
|
||||
struct ResolvedDepMaterialization {
|
||||
Package::LockContextRecord lock_context;
|
||||
std::vector<std::pair<DependencyRecord, Package::MaterializedSource>> materialized;
|
||||
};
|
||||
using ResolvedDepMaterialization = Package::ResolvedDepMaterialization;
|
||||
|
||||
// Shared dependency resolution + live registry fetching + safe archive extraction + lock context
|
||||
// builder for `vbs deps resolve|update`, `vbs build`, and `vbs test`.
|
||||
// Thin CLI adapter over the shared Package resolver (environment-provided registry defaults).
|
||||
Void::Result::Result<ResolvedDepMaterialization, Void::Result::Error> resolve_and_materialize_deps(
|
||||
const ProfileContext &ctx, const std::vector<Package::NormalizedDependency> &merged_deps,
|
||||
const std::filesystem::path &project_root, const std::filesystem::path &build_root,
|
||||
const std::optional<Package::PinnedLock> &pinned_lock, bool offline, bool allow_upgrade,
|
||||
const std::vector<std::string> &trusted_public_keys) {
|
||||
const std::string ctx_key = Package::make_lock_context_key(ctx, "target");
|
||||
const std::string fp = Package::compute_declaration_fingerprint(ctx, "target", merged_deps);
|
||||
|
||||
ResolvedDepMaterialization out;
|
||||
out.lock_context.context_key = ctx_key;
|
||||
out.lock_context.profile_name = ctx.name;
|
||||
out.lock_context.compiler = std::string{compiler_name(ctx.compiler)};
|
||||
out.lock_context.os = ctx.os;
|
||||
out.lock_context.architecture = ctx.architecture;
|
||||
out.lock_context.machine_role = "target";
|
||||
out.lock_context.declaration_fingerprint = fp;
|
||||
|
||||
// Collect existing lock pins (used unless `deps update` requested an upgrade).
|
||||
Package::SolveOptions solve_opts;
|
||||
std::map<Package::PackageId, Package::LockedSourceEntry> locked_pkg_entries;
|
||||
if (!allow_upgrade && pinned_lock && pinned_lock->document.contexts.contains(ctx_key)) {
|
||||
for (const auto &entry : pinned_lock->document.contexts.at(ctx_key).entries) {
|
||||
if (entry.source_kind == "package" && !entry.package_name.empty()) {
|
||||
const Package::PackageId pid{entry.registry_authority, entry.package_name};
|
||||
locked_pkg_entries[pid] = entry;
|
||||
if (auto ver = Void::Semver::parse_strict(entry.version); ver) {
|
||||
solve_opts.pinned_versions[pid] = *ver;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Solve all registry package dependencies together via PubGrub.
|
||||
const std::string default_registry = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
Package::RegistryAuth reg_auth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
std::vector<Package::DependencyConstraint> root_constraints;
|
||||
std::map<std::string, Package::PackageId, std::less<>> alias_to_pkg_id;
|
||||
std::vector<const Package::NormalizedDependency *> package_deps;
|
||||
|
||||
for (const auto &nd : merged_deps) {
|
||||
if (nd.record.source_kind != DependencySourceKind::Package) {
|
||||
continue;
|
||||
}
|
||||
const std::string reg_url =
|
||||
nd.record.registry_authority.empty() ? default_registry : nd.record.registry_authority;
|
||||
if (reg_url.empty()) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("package dependency '{}' requires .registry(url) or VBS_REGISTRY_URL",
|
||||
nd.record.name))};
|
||||
}
|
||||
const std::string pkg_name = nd.record.package.empty() ? nd.record.name : nd.record.package;
|
||||
const Package::PackageId pid{reg_url, pkg_name};
|
||||
alias_to_pkg_id[nd.record.name] = pid;
|
||||
package_deps.push_back(&nd);
|
||||
root_constraints.push_back(Package::DependencyConstraint{
|
||||
pid, nd.record.version.empty() ? ">=0.0.0" : nd.record.version, nd.record.features,
|
||||
"root(" + nd.record.name + ")"});
|
||||
}
|
||||
|
||||
std::map<Package::PackageId, Package::SolvedPackage> solved_packages;
|
||||
if (!root_constraints.empty()) {
|
||||
Void::Log::info("Deps", "Solving {} registry package constraint(s)...",
|
||||
root_constraints.size());
|
||||
bool all_locked_cached = offline;
|
||||
const auto pkg_cache_root = build_root / ".vbs" / "cache" / "packages";
|
||||
if (all_locked_cached) {
|
||||
for (const auto *nd : package_deps) {
|
||||
const auto &pid = alias_to_pkg_id.at(nd->record.name);
|
||||
const auto it = locked_pkg_entries.find(pid);
|
||||
if (it == locked_pkg_entries.end() || it->second.sha256.empty() ||
|
||||
!std::filesystem::is_directory(pkg_cache_root / it->second.sha256 / "src")) {
|
||||
all_locked_cached = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (all_locked_cached) {
|
||||
Void::Log::info("Deps", "Using offline cached locked package graph");
|
||||
for (const auto *nd : package_deps) {
|
||||
const auto &pid = alias_to_pkg_id.at(nd->record.name);
|
||||
const auto &le = locked_pkg_entries.at(pid);
|
||||
auto parsed_ver = Void::Semver::parse_strict(le.version);
|
||||
if (!parsed_ver) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("corrupt locked version '{}' for package '{}'", le.version,
|
||||
pid.name))};
|
||||
}
|
||||
Package::SolvedPackage sp;
|
||||
sp.id = pid;
|
||||
sp.version = *parsed_ver;
|
||||
sp.source_sha256 = le.sha256;
|
||||
solved_packages[pid] = std::move(sp);
|
||||
}
|
||||
} else {
|
||||
if (offline) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::not_found,
|
||||
"offline mode cannot query live package registry; populate cache first")};
|
||||
}
|
||||
Package::PackageIndex index;
|
||||
std::set<Package::PackageId> fetched_ids;
|
||||
std::queue<Package::PackageId> pending;
|
||||
for (const auto &c : root_constraints) {
|
||||
if (fetched_ids.insert(c.target).second) {
|
||||
pending.push(c.target);
|
||||
}
|
||||
}
|
||||
while (!pending.empty()) {
|
||||
const auto pid = pending.front();
|
||||
pending.pop();
|
||||
Void::Log::debug("Deps", "Fetching release index for '{}' from {}", pid.name,
|
||||
pid.registry_authority);
|
||||
auto releases =
|
||||
Package::fetch_package_releases(pid.registry_authority, pid.name, reg_auth);
|
||||
if (!releases) {
|
||||
return std::unexpected{releases.error()};
|
||||
}
|
||||
for (auto &rel : *releases) {
|
||||
for (const auto &dep_c : rel.dependencies) {
|
||||
if (fetched_ids.insert(dep_c.target).second) {
|
||||
pending.push(dep_c.target);
|
||||
}
|
||||
}
|
||||
index.add_release(std::move(rel));
|
||||
}
|
||||
}
|
||||
|
||||
auto solved = Package::solve_dependencies(root_constraints, index, solve_opts);
|
||||
if (!solved) {
|
||||
return std::unexpected{solved.error()};
|
||||
}
|
||||
solved_packages = std::move(solved->packages);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Materialize every dependency (registry packages + git/path/archive/conan).
|
||||
const auto pkg_cache_root = build_root / ".vbs" / "cache" / "packages";
|
||||
std::error_code ec;
|
||||
for (const auto &nd : merged_deps) {
|
||||
if (nd.record.source_kind == DependencySourceKind::Package) {
|
||||
const auto &pid = alias_to_pkg_id.at(nd.record.name);
|
||||
const auto &sp = solved_packages.at(pid);
|
||||
const auto tar_path = pkg_cache_root / (sp.source_sha256 + ".tar");
|
||||
const auto extract_dir = pkg_cache_root / sp.source_sha256 / "src";
|
||||
|
||||
std::string package_signature;
|
||||
std::string package_public_key;
|
||||
if (const auto it = locked_pkg_entries.find(pid);
|
||||
it != locked_pkg_entries.end() && it->second.sha256 == sp.source_sha256) {
|
||||
package_signature = it->second.signature.value_or(std::string{});
|
||||
package_public_key = it->second.public_key.value_or(std::string{});
|
||||
} else {
|
||||
package_signature = sp.signature.value_or(std::string{});
|
||||
package_public_key = sp.public_key.value_or(std::string{});
|
||||
}
|
||||
|
||||
if (!trusted_public_keys.empty()) {
|
||||
auto decision = Provenance::verify_trusted_package_signature(
|
||||
pid.name, sp.version.to_string(),
|
||||
Provenance::PackageSignature{sp.source_sha256, package_signature,
|
||||
package_public_key},
|
||||
trusted_public_keys);
|
||||
if (!decision) {
|
||||
return std::unexpected{decision.error()};
|
||||
}
|
||||
if (*decision != Provenance::TrustDecision::Verified) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::permission_denied,
|
||||
std::format("package {}@{} is not signed by a trusted key ({})", pid.name,
|
||||
sp.version.to_string(),
|
||||
Provenance::trust_decision_name(*decision)))};
|
||||
}
|
||||
}
|
||||
|
||||
if (!std::filesystem::is_directory(extract_dir, ec)) {
|
||||
bool need_download = true;
|
||||
if (std::filesystem::is_regular_file(tar_path, ec)) {
|
||||
auto have_sha = Util::sha256_file_hex(tar_path);
|
||||
if (have_sha && *have_sha == sp.source_sha256) {
|
||||
need_download = false;
|
||||
}
|
||||
}
|
||||
if (need_download) {
|
||||
Void::Log::info("Fetch", "Downloading {}@{} from {}", pid.name,
|
||||
sp.version.to_string(), pid.registry_authority);
|
||||
auto dl = Package::download_release_archive(
|
||||
pid.registry_authority, pid.name, sp.version.to_string(), sp.source_sha256,
|
||||
tar_path, reg_auth);
|
||||
if (!dl) {
|
||||
return std::unexpected{dl.error()};
|
||||
}
|
||||
}
|
||||
Void::Log::info("Fetch", "Extracting {}@{} ({})", pid.name, sp.version.to_string(),
|
||||
sp.source_sha256.substr(0, 12));
|
||||
auto ex = Package::extract_safe_tar(tar_path, extract_dir);
|
||||
if (!ex) {
|
||||
return std::unexpected{ex.error()};
|
||||
}
|
||||
} else {
|
||||
Void::Log::debug("Fetch", "Using cached package {}@{} ({})", pid.name,
|
||||
sp.version.to_string(), sp.source_sha256.substr(0, 12));
|
||||
}
|
||||
|
||||
Package::MaterializedSource mat{nd.record.name, extract_dir, sp.source_sha256, false};
|
||||
out.materialized.emplace_back(nd.record, std::move(mat));
|
||||
|
||||
Package::LockedSourceEntry le;
|
||||
le.alias = nd.record.name;
|
||||
le.source_kind = "package";
|
||||
le.registry_authority = pid.registry_authority;
|
||||
le.package_name = pid.name;
|
||||
le.version = sp.version.to_string();
|
||||
le.sha256 = sp.source_sha256;
|
||||
le.builder = nd.record.builder;
|
||||
le.features = nd.record.features;
|
||||
le.build_dependency = nd.record.build_dependency;
|
||||
if (!package_signature.empty()) {
|
||||
le.signature = package_signature;
|
||||
}
|
||||
if (!package_public_key.empty()) {
|
||||
le.public_key = package_public_key;
|
||||
}
|
||||
out.lock_context.entries.push_back(std::move(le));
|
||||
} else {
|
||||
Package::FetchOptions fopt{build_root / ".vbs" / "cache", project_root, offline, {}};
|
||||
auto mat = Package::materialize_dependency_source(nd.record, fopt);
|
||||
if (!mat) {
|
||||
return std::unexpected{mat.error()};
|
||||
}
|
||||
Package::LockedSourceEntry le;
|
||||
le.alias = nd.record.name;
|
||||
switch (nd.record.source_kind) {
|
||||
case DependencySourceKind::Git:
|
||||
le.source_kind = "git";
|
||||
break;
|
||||
case DependencySourceKind::Path:
|
||||
le.source_kind = "path";
|
||||
break;
|
||||
case DependencySourceKind::Archive:
|
||||
le.source_kind = "archive";
|
||||
break;
|
||||
case DependencySourceKind::Conan:
|
||||
le.source_kind = "conan";
|
||||
break;
|
||||
case DependencySourceKind::Package:
|
||||
le.source_kind = "package";
|
||||
break;
|
||||
case DependencySourceKind::Unspecified:
|
||||
le.source_kind = "unspecified";
|
||||
break;
|
||||
}
|
||||
le.location = nd.record.location;
|
||||
le.revision = nd.record.revision;
|
||||
le.sha256 = mat->content_identity_hash;
|
||||
le.builder = nd.record.builder;
|
||||
le.features = nd.record.features;
|
||||
le.build_dependency = nd.record.build_dependency;
|
||||
out.lock_context.entries.push_back(std::move(le));
|
||||
out.materialized.emplace_back(nd.record, std::move(*mat));
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
Package::ResolveOptions options;
|
||||
options.project_root = project_root;
|
||||
options.build_root = build_root;
|
||||
options.pinned_lock = pinned_lock;
|
||||
options.offline = offline;
|
||||
options.allow_upgrade = allow_upgrade;
|
||||
options.trusted_public_keys = trusted_public_keys;
|
||||
options.default_registry_url = Void::Configuration::get_env_or("VBS_REGISTRY_URL", "");
|
||||
options.registry_auth =
|
||||
Package::RegistryAuth{Void::Configuration::get_env_or("VBS_REGISTRY_TOKEN", "")};
|
||||
return Package::resolve_and_materialize_dependencies(ctx, merged_deps, options);
|
||||
}
|
||||
|
||||
struct BuildCmdOptions {
|
||||
|
||||
Reference in new issue
Block a user