feat(workspace): add multi-package workspace builds with shared lock and CAS
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s

- SDK: Workspace/workspace() declaration plus Project::workspace_dependency()
- Frontend: workspace.cpp discovery/evaluation alongside build.cpp
- Main: per-member builds sharing the root vbs.lock and build/.vbs CAS, --member and --changed-since selection
- Cross-member native linking; member builds do not steal the root compile_commands.json alias
- Path-dependency snapshots exclude generated editor metadata
- Verified in Docker GCC 16: 8/8 CTest suites; 3-member workspace fixture with unified lockfile
This commit is contained in:
faumaray committed 2026-12-03 13:07:09 +03:00
1 parent 09c0a91d1d
commit 2970fbe25c
12 files changed
+515 -19

No files matched your search

+39
View File
@@ -2,6 +2,7 @@
#include <Void/Build/Util/Json.hpp>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <type_traits>
@@ -1064,6 +1065,44 @@ Dependency Project::dependency(std::string name) {
state_->dependencies.push_back(std::move(record));
return Dependency{state_, state_->dependencies.size() - 1};
}
Dependency Project::workspace_dependency(std::string name) {
auto dep = dependency(name);
auto &record = state_->dependencies.back();
const std::string env_name = "VBS_WORKSPACE_MEMBER_" + record.name;
const char *member_path = std::getenv(env_name.c_str());
if (!member_path || member_path[0] == '\0') {
throw std::invalid_argument{"workspace member '" + record.name +
"' is not provided by the enclosing workspace"};
}
record.source_kind = DependencySourceKind::Path;
record.location = member_path;
record.builder = "native";
record.options.insert_or_assign("vbs.workspace.member", OptionValue{record.name});
return dep;
}
Workspace &Workspace::member(std::string name, std::string path) {
require_text(name, "workspace member name");
require_text(path, "workspace member path");
if (std::ranges::any_of(members_, [&](const auto &member) { return member.name == name; })) {
throw std::invalid_argument{"duplicate workspace member '" + name + "'"};
}
members_.push_back({std::move(name), std::move(path)});
return *this;
}
const std::vector<WorkspaceMemberRecord> &Workspace::members() const noexcept { return members_; }
std::string Workspace::to_json() const {
return "{\"schema_version\":1,\"members\":" +
json_array(members_,
[](const WorkspaceMemberRecord &member) {
return "{\"name\":" + json_quote(member.name) +
",\"path\":" + json_quote(member.path) + '}';
}) +
'}';
}
void Project::dependencies_file(std::string toml_path) {
require_text(toml_path, "dependencies manifest path");
state_->dependency_files.push_back(std::move(toml_path));
+130 -5
View File
@@ -32,7 +32,8 @@ bool is_cpp_file(const std::filesystem::path &p) {
return ext == ".cpp" || ext == ".cc" || ext == ".cxx";
}
std::string generate_driver_source(bool has_profiles_file) {
std::string generate_driver_source(bool has_profiles_file, bool has_build_file,
bool has_workspace_file) {
std::string code = R"cpp(#include <Void/Build.hpp>
#include <Void/Build/Util/Json.hpp>
@@ -49,6 +50,18 @@ std::string generate_driver_source(bool has_profiles_file) {
code += R"cpp(void profiles(Void::Build::Profiles &descriptions) {
descriptions = Void::Build::default_profiles();
}
)cpp";
}
if (!has_build_file) {
code += R"cpp(void build(Void::Build::Project &) {
throw std::runtime_error{"this descriptor declares no build() entry point"};
}
)cpp";
}
if (!has_workspace_file) {
code += R"cpp(void workspace(Void::Build::Workspace &) {
throw std::runtime_error{"this descriptor declares no workspace() entry point"};
}
)cpp";
}
code += R"cpp(
@@ -166,6 +179,16 @@ int main(int argc, char **argv) {
write_text(output_path, payload);
return 0;
}
if (mode == "workspace") {
Void::Build::Workspace ws;
workspace(ws);
const std::string payload =
"{\"protocol_version\":1,\"request_id\":" +
Void::Build::Util::json_quote(request_id) +
",\"payload\":" + ws.to_json() + "}\n";
write_text(output_path, payload);
return 0;
}
std::cerr << "vbs-config-driver: unknown mode '" << mode << "'\n";
return 2;
} catch (const std::exception &err) {
@@ -257,9 +280,13 @@ discover_config_sources(const std::filesystem::path &project_root,
}
const auto root_build = canonical_root / "build.cpp";
if (!std::filesystem::is_regular_file(root_build, ec)) {
const auto root_workspace = canonical_root / "workspace.cpp";
const bool has_build = std::filesystem::is_regular_file(root_build, ec);
const bool has_workspace = std::filesystem::is_regular_file(root_workspace, ec);
if (!has_build && !has_workspace) {
return std::unexpected{
make_error(std::format("missing required root description '{}'", root_build.string()))};
make_error(std::format("missing required root description '{}' or '{}'",
root_build.string(), root_workspace.string()))};
}
std::vector<std::filesystem::path> sources;
@@ -271,7 +298,12 @@ discover_config_sources(const std::filesystem::path &project_root,
}
};
push(root_build);
if (has_build) {
push(root_build);
}
if (has_workspace) {
push(root_workspace);
}
const auto profiles_cpp = canonical_root / "build.profiles.cpp";
if (std::filesystem::is_regular_file(profiles_cpp, ec)) {
@@ -349,7 +381,10 @@ compile_descriptor(const FrontendOptions &options) {
const bool has_profiles =
std::filesystem::is_regular_file(canonical_root / "build.profiles.cpp", ec);
const std::string driver_src = generate_driver_source(has_profiles);
const bool has_build = std::filesystem::is_regular_file(canonical_root / "build.cpp", ec);
const bool has_workspace =
std::filesystem::is_regular_file(canonical_root / "workspace.cpp", ec);
const std::string driver_src = generate_driver_source(has_profiles, has_build, has_workspace);
const auto driver_cpp = config_dir / "Driver.cpp";
if (auto wrote = Platform::atomic_write_file(driver_cpp, driver_src, false); !wrote) {
return std::unexpected{wrote.error()};
@@ -552,6 +587,11 @@ compile_descriptor(const FrontendOptions &options) {
return desc;
}
bool is_workspace_root(const std::filesystem::path &project_root) {
std::error_code ec;
return std::filesystem::is_regular_file(project_root / "workspace.cpp", ec);
}
Void::Result::Result<Profiles, Void::Result::Error>
discover_profiles(const CompiledDescriptor &descriptor, const FrontendOptions &options) {
std::error_code ec;
@@ -763,4 +803,89 @@ evaluate_project(const CompiledDescriptor &descriptor, const ProfileContext &con
return desc;
}
Void::Result::Result<WorkspaceDescription, Void::Result::Error>
evaluate_workspace(const CompiledDescriptor &descriptor, const FrontendOptions &options) {
std::error_code ec;
const auto run_dir =
std::filesystem::weakly_canonical(options.build_root, ec) / ".vbs" / "config" / "eval";
std::filesystem::create_directories(run_dir, ec);
const std::string req_id = std::format("ws_{}_{}", static_cast<long long>(::getpid()),
g_req_seq.fetch_add(1, std::memory_order_relaxed));
const auto out_file = run_dir / (req_id + ".json");
Platform::ProcessRequest req;
req.executable = descriptor.executable_path;
req.arguments = {"--mode", "workspace", "--output", out_file.string(), "--request-id", req_id};
req.working_directory = options.project_root;
req.timeout = options.eval_timeout;
auto proc = Platform::run_supervised(req);
auto cleanup = [&] { std::filesystem::remove(out_file, ec); };
if (!proc) {
cleanup();
return std::unexpected{proc.error()};
}
if (!proc->succeeded()) {
cleanup();
return std::unexpected{
make_error(std::format("workspace evaluation failed (exit={}, signal={}): {}",
proc->exit_code, proc->term_signal, proc->stderr_data))};
}
auto text = Void::FileSystem::read_file_bounded(out_file, {16ULL * 1024ULL * 1024ULL});
cleanup();
if (!text) {
return std::unexpected{make_error("workspace evaluation did not produce a result file")};
}
if (!Util::is_valid_utf8(*text)) {
return std::unexpected{make_error("workspace evaluation result contains invalid UTF-8")};
}
auto parsed = Void::Json::Strict::parse(*text);
if (!parsed) {
return std::unexpected{make_error("workspace evaluation emitted malformed JSON")};
}
const auto *root = std::get_if<JsonVal::Object>(&parsed->value);
if (!root) {
return std::unexpected{make_error("workspace evaluation envelope must be a JSON object")};
}
if (get_i64(*root, "protocol_version").value_or(0) != kFrontendProtocolVersion) {
return std::unexpected{make_error("workspace evaluation protocol version mismatch")};
}
if (get_str(*root, "request_id").value_or("") != req_id) {
return std::unexpected{make_error("workspace evaluation request_id mismatch")};
}
const auto *payload_v = find_key(*root, "payload");
const auto *payload = payload_v ? std::get_if<JsonVal::Object>(&payload_v->value) : nullptr;
if (!payload) {
return std::unexpected{make_error("workspace evaluation missing payload")};
}
const auto *members_v = find_key(*payload, "members");
const auto *arr = members_v ? std::get_if<JsonVal::Array>(&members_v->value) : nullptr;
if (!arr) {
return std::unexpected{make_error("workspace evaluation payload missing members array")};
}
WorkspaceDescription result;
std::set<std::string> seen;
for (const auto &mv : *arr) {
const auto *mobj = std::get_if<JsonVal::Object>(&mv.value);
if (!mobj) {
continue;
}
WorkspaceMember member;
member.name = get_str(*mobj, "name").value_or("");
member.path = get_str(*mobj, "path").value_or("");
if (member.name.empty() || member.path.empty()) {
return std::unexpected{make_error("workspace member requires nonempty name and path")};
}
if (!seen.insert(member.name).second) {
return std::unexpected{make_error("duplicate workspace member '" + member.name + "'")};
}
result.members.push_back(std::move(member));
}
return result;
}
} // namespace Void::Build::Config
+143 -8
View File
@@ -36,6 +36,7 @@
#include <array>
#include <charconv>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <iostream>
@@ -504,6 +505,11 @@ struct BuildCmdOptions {
std::optional<std::string> jobs_arg;
std::optional<std::string> remote_cache_arg;
std::optional<std::string> remote_cache_token_env;
std::vector<std::string> members;
std::optional<std::string> changed_since;
std::optional<std::filesystem::path> lock_root;
std::optional<std::filesystem::path> state_dir;
bool publish_editor_selection{true};
bool no_cache{false};
bool locked{false};
bool offline{false};
@@ -587,7 +593,7 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
return 1;
}
auto state = State::StateStore::open(build_root / ".vbs");
auto state = State::StateStore::open(opts.state_dir ? *opts.state_dir : build_root / ".vbs");
if (!state) {
std::cerr << "vbs: " << state.error().message() << '\n';
return 1;
@@ -608,7 +614,7 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
Void::Log::info("Cache", "Using shared remote action cache at {}", remote_cache_url);
}
const auto lock_path = project_root / "vbs.lock";
const auto lock_path = (opts.lock_root ? *opts.lock_root : project_root) / "vbs.lock";
auto pinned_lock = Package::load_pinned_lock(lock_path);
if (!pinned_lock) {
std::cerr << "vbs: " << pinned_lock.error().message() << '\n';
@@ -813,7 +819,8 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
Editor::PublishOptions popts;
popts.project_root = project_root;
popts.build_root = build_root;
popts.update_selection = (*opts.editor_profile == prof_name);
popts.update_selection =
opts.publish_editor_selection && (*opts.editor_profile == prof_name);
popts.reserved_ticket = *reserved_ticket;
popts.sdk_include_dir = fopts.sdk_include_dir;
popts.extra_build_sources = extra_build_sources;
@@ -888,6 +895,130 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
return all_ok ? 0 : 1;
}
int execute_workspace(BuildCmdOptions opts, bool run_tests) {
const std::filesystem::path project_root = std::filesystem::absolute(opts.project_arg);
const std::filesystem::path build_root =
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
std::vector<std::filesystem::path> extra_sources;
for (const auto &source : opts.extra_build_sources) {
extra_sources.emplace_back(source);
}
const auto fopts = make_frontend_opts(project_root, build_root, extra_sources);
auto compiled = Config::compile_descriptor(fopts);
if (!compiled) {
std::cerr << "vbs: " << compiled.error().message() << '\n';
return 1;
}
auto workspace = Config::evaluate_workspace(*compiled, fopts);
if (!workspace) {
std::cerr << "vbs: " << workspace.error().message() << '\n';
return 1;
}
if (workspace->members.empty()) {
std::cerr << "vbs: workspace declares no members\n";
return 1;
}
std::map<std::string, std::filesystem::path, std::less<>> member_dirs;
for (const auto &member : workspace->members) {
std::filesystem::path dir = member.path;
if (!dir.is_absolute()) {
dir = project_root / dir;
}
std::error_code ec;
const auto canonical = std::filesystem::weakly_canonical(dir, ec);
if (ec || !std::filesystem::is_directory(canonical)) {
std::cerr << "vbs: workspace member '" << member.name << "' path '" << dir.string()
<< "' is not a directory\n";
return 1;
}
member_dirs[member.name] = canonical;
const std::string env_name = "VBS_WORKSPACE_MEMBER_" + member.name;
(void)::setenv(env_name.c_str(), canonical.c_str(), 1);
}
std::set<std::string> selected;
if (!opts.members.empty()) {
for (const auto &name : opts.members) {
if (!member_dirs.contains(name)) {
std::cerr << "vbs: unknown workspace member '" << name << "'\n";
return 2;
}
selected.insert(name);
}
} else if (opts.changed_since) {
auto git = Platform::resolve_executable("git");
if (!git) {
std::cerr << "vbs: --changed-since requires git in PATH\n";
return 1;
}
Platform::ProcessRequest req;
req.executable = *git;
req.arguments = {"-C", project_root.string(), "diff", "--name-only", *opts.changed_since};
auto proc = Platform::run_supervised(req);
if (!proc || !proc->succeeded()) {
std::cerr << "vbs: git diff --name-only " << *opts.changed_since
<< " failed: " << (proc ? proc->stderr_data : proc.error().message()) << '\n';
return 1;
}
std::size_t pos = 0;
while (pos < proc->stdout_data.size()) {
const auto nl = proc->stdout_data.find('\n', pos);
const auto line = proc->stdout_data.substr(
pos, nl == std::string::npos ? std::string::npos : nl - pos);
if (!line.empty()) {
const auto changed = std::filesystem::weakly_canonical(project_root / line);
std::string best;
std::size_t best_len = 0;
for (const auto &[name, dir] : member_dirs) {
if (Platform::path_is_within(dir, changed) && dir.string().size() > best_len) {
best = name;
best_len = dir.string().size();
}
}
if (!best.empty()) {
selected.insert(best);
}
}
if (nl == std::string::npos) {
break;
}
pos = nl + 1;
}
if (selected.empty()) {
std::cout << "vbs: no workspace members changed since " << *opts.changed_since << '\n';
return 0;
}
} else {
for (const auto &[name, dir] : member_dirs) {
selected.insert(name);
}
}
bool all_ok = true;
for (const auto &member : workspace->members) {
if (!selected.contains(member.name)) {
continue;
}
BuildCmdOptions member_opts = opts;
member_opts.project_arg = member_dirs.at(member.name).string();
member_opts.build_arg = (build_root / "members" / member.name).string();
member_opts.lock_root = project_root;
member_opts.state_dir = build_root / ".vbs";
member_opts.members.clear();
member_opts.changed_since.reset();
// Members must not overwrite each other's project-root compile_commands.json alias.
member_opts.publish_editor_selection = false;
Void::Log::info("Workspace", "[{}] {}", member.name,
run_tests ? "building and testing member" : "building member");
if (execute_build_or_test(std::move(member_opts), run_tests) != 0) {
all_ok = false;
}
}
return all_ok ? 0 : 1;
}
struct CompDbCmdOptions {
std::string project_arg;
std::optional<std::string> build_arg;
@@ -1997,6 +2128,9 @@ int main(int argc, char **argv) {
.option_many("--build-source", "", "Additional build description source",
build_opts.extra_build_sources)
.option("--jobs", "-j", "Maximum parallel compilation/link jobs", build_opts.jobs_arg)
.option_many("--member", "", "Workspace member to build/test", build_opts.members)
.option("--changed-since", "", "Build/test workspace members changed since a git ref",
build_opts.changed_since)
.option("--remote-cache", "", "Shared HTTP action cache URL (or VBS_REMOTE_CACHE)",
build_opts.remote_cache_arg)
.option("--remote-cache-token-env", "",
@@ -2229,11 +2363,12 @@ int main(int argc, char **argv) {
: Void::Log::Level::Info);
ConsoleLogGuard log_guard{log_level};
if (cmd == "build") {
return execute_build_or_test(std::move(build_opts), false);
}
if (cmd == "test") {
return execute_build_or_test(std::move(build_opts), true);
if (cmd == "build" || cmd == "test") {
const bool run_tests = cmd == "test";
if (Config::is_workspace_root(std::filesystem::path{build_opts.project_arg})) {
return execute_workspace(std::move(build_opts), run_tests);
}
return execute_build_or_test(std::move(build_opts), run_tests);
}
if (cmd == "describe") {
return execute_describe_command(describe_opts);
+2
View File
@@ -161,6 +161,8 @@ materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &o
sopts.project_root = canon;
sopts.snapshot_store_dir = options.cache_dir / "path-snapshots";
sopts.materialize = false;
// Generated editor metadata is never a build input and may be an out-of-tree symlink.
sopts.excluded_names.push_back("compile_commands.json");
auto snap = Snapshot::capture_snapshot(sopts, {}, {canon});
if (!snap) {
return std::unexpected{snap.error()};