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
5 changes: 4 additions & 1 deletion docs/51-supported-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ mcpp emit sbom -o sbom.json
It reads `mcpp.lock` rather than resolving again, because a document describing
a different graph from the one that was built is worse than no document. A
component whose licence mcpp does not know is emitted as `NOASSERTION` rather
than omitted: an absent field reads as "not examined".
than omitted: an absent field reads as "not examined". With no `mcpp.lock` the
document names the root package alone, and a note on standard error says that
nothing is recorded: that is the whole truth for a project with no dependencies,
and not for one that has never been built.

## Offline and air-gapped use

Expand Down
3 changes: 2 additions & 1 deletion docs/zh/51-supported-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ mcpp emit sbom -o sbom.json

它读取 `mcpp.lock` 而不是重新解析,因为一份描述了与实际构建不同的依赖图的
文档,比没有文档更糟。许可证 mcpp 无法识别的组件会被写成 `NOASSERTION` 而不是
省略:缺失的字段读起来像「未检查」。
省略:缺失的字段读起来像「未检查」。没有 `mcpp.lock` 时,文档只列出根包,并在标准
错误上提示没有任何记录:这对没有依赖的工程是全部事实,对从未构建过的工程则不是。

## 离线与隔网使用

Expand Down
97 changes: 56 additions & 41 deletions src/build/prepare/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,60 @@ static std::optional<std::string> target_drift(const PrepareState& state,
c.why.empty() ? std::string{} : "; " + c.why);
}

// THE `--locked` ASSERTION: this resolution is the recorded one, or the
// failure names what moved. `partialWorkspace` is a plan of some of a
// workspace's members, whose unresolved entries are not drift.
static std::expected<void, std::string> check_locked(const PrepareState& state,
const mcpp::lockfile::Lockfile& lock,
const std::optional<mcpp::lockfile::LockedTarget>& priorTarget,
const std::filesystem::path& lockPath,
bool partialWorkspace) {
// `load` reads a missing file as an empty lock, which would
// report every package as drift from a file that is not there.
auto prior = mcpp::lockfile::load(lockPath);
if (!prior || !std::filesystem::exists(lockPath)) {
return std::unexpected(std::format(
"--locked was given and there is no readable mcpp.lock at {}\n"
" Run the same command without --locked once to record "
"this resolution, then commit mcpp.lock.",
lockPath.string()));
}
auto key = [](const mcpp::lockfile::LockedPackage& p) {
const std::string sn = lock_short_name(p);
return p.namespace_.empty() ? sn
: p.namespace_ + "." + sn;
};
std::map<std::string, std::string> was, now;
// An override is part of the recorded resolution (T2): the
// same version chosen by a merge or by the root is drift.
auto recorded = [](const mcpp::lockfile::LockedPackage& p) {
return p.overridden ? p.version + " (override)" : p.version;
};
for (auto const& p : prior->packages) was[key(p)] = recorded(p);
for (auto const& p : lock.packages) now[key(p)] = recorded(p);
std::vector<std::string> drift;
if (auto d = target_drift(state, priorTarget)) drift.push_back(std::move(*d));
for (auto const& [k, v] : now) {
auto it = was.find(k);
if (it == was.end()) drift.push_back(k + " " + v + " (not in the lock)");
else if (it->second != v) drift.push_back(k + " " + it->second + " -> " + v);
}
// A plan of some of a workspace's members resolves some of
// the lock; what it did not resolve is not drift.
if (!partialWorkspace)
for (auto const& [k, v] : was)
if (!now.contains(k)) drift.push_back(k + " " + v + " (no longer resolved)");
if (!drift.empty()) {
std::string msg = "--locked was given and this resolution "
"differs from mcpp.lock:";
for (auto const& d : drift) msg += "\n " + d;
msg += "\n Re-run without --locked to update the lock, "
"or pin the dependency that moved.";
return std::unexpected(msg);
}
return {};
}

std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildContext& ctx) {
// Write/update mcpp.lock for any version-based deps that succeeded.
// Path deps are intentionally NOT locked — their source is local filesystem.
Expand Down Expand Up @@ -459,47 +513,8 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
// true and useless; which package moved, from which version to
// which, is what the reader does something about.
if (mcpp::invocation::get("MCPP_LOCKED").value_or("") == "1") {
auto prior = mcpp::lockfile::load(lockPath);
if (!prior) {
return std::unexpected(std::format(
"--locked was given and there is no readable mcpp.lock at {}\n"
" Run the same command without --locked once to record "
"this resolution, then commit mcpp.lock.",
lockPath.string()));
}
auto key = [](const mcpp::lockfile::LockedPackage& p) {
const std::string sn = lock_short_name(p);
return p.namespace_.empty() ? sn
: p.namespace_ + "." + sn;
};
std::map<std::string, std::string> was, now;
// An override is part of the recorded resolution (T2): the
// same version chosen by a merge or by the root is drift.
auto recorded = [](const mcpp::lockfile::LockedPackage& p) {
return p.overridden ? p.version + " (override)" : p.version;
};
for (auto const& p : prior->packages) was[key(p)] = recorded(p);
for (auto const& p : lock.packages) now[key(p)] = recorded(p);
std::vector<std::string> drift;
if (auto d = target_drift(state, priorTarget)) drift.push_back(std::move(*d));
for (auto const& [k, v] : now) {
auto it = was.find(k);
if (it == was.end()) drift.push_back(k + " " + v + " (not in the lock)");
else if (it->second != v) drift.push_back(k + " " + it->second + " -> " + v);
}
// A plan of some of a workspace's members resolves some of
// the lock; what it did not resolve is not drift.
if (!partialWorkspace)
for (auto const& [k, v] : was)
if (!now.contains(k)) drift.push_back(k + " " + v + " (no longer resolved)");
if (!drift.empty()) {
std::string msg = "--locked was given and this resolution "
"differs from mcpp.lock:";
for (auto const& d : drift) msg += "\n " + d;
msg += "\n Re-run without --locked to update the lock, "
"or pin the dependency that moved.";
return std::unexpected(msg);
}
if (auto r = check_locked(state, lock, priorTarget, lockPath, partialWorkspace); !r)
return std::unexpected(std::move(r.error()));
}
// OPTIMISTIC: no lock is held while the graph resolves (it may
// reach the network). What this run read is compared with what is
Expand Down
14 changes: 9 additions & 5 deletions src/cli/cmd_sbom.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ int cmd_sbom(const mcpplibs::cmdline::ParsedArgs& parsed) {

const auto lockPath = *root / "mcpp.lock";
std::vector<mcpp::lockfile::LockedPackage> pkgs;
if (auto lock = mcpp::lockfile::load(lockPath)) {
// Asked before loading: `load` reads a file that is not there as an empty
// lock, so its answer cannot tell "nothing recorded" from "no dependencies".
if (std::filesystem::exists(lockPath)) {
auto lock = mcpp::lockfile::load(lockPath);
if (!lock) {
mcpp::ui::error(std::format(
"mcpp.lock exists but could not be read: {}", lock.error().message));
return 2;
}
pkgs = lock->packages;
} else if (std::filesystem::exists(lockPath)) {
mcpp::ui::error(std::format(
"mcpp.lock exists but could not be read: {}", lock.error().message));
return 2;
} else {
// NOT AN ERROR, AND NOT SILENCE EITHER. A project with no
// dependencies has no lock and its bill of materials is one component,
Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/run/runners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ E2E runners{"runners", [] {
expect(fatal(e2e::contains(s::text(r), "differs from mcpp.lock"))) << "the refusal does not say what is wrong: " << s::text(r);
expect(fatal(e2e::contains(s::text(r), "9.9.9 -> 0.0.1"))) << "the refusal does not name which package moved, and to what: " << s::text(r);
q.write("mcpp.lock", lock);
// A lock that is not there is not a lock that differs: the refusal says to record one,
// and a refused --locked writes none for the next run to pass against.
fs::remove(q.at("mcpp.lock"));
r = q.mcpp({"build", "--locked"});
expect(fatal(failed(r))) << "--locked accepted a project with no mcpp.lock";
expect(fatal(e2e::contains(s::text(r), "no readable mcpp.lock"))) << "the refusal does not say the lock is missing: " << s::text(r);
expect(fatal(!q.exists("mcpp.lock"))) << "a refused --locked wrote mcpp.lock";
q.write("mcpp.lock", lock);
r = q.mcpp({"emit", "sbom", "-o", "sbom.json"});
expect(fatal(e2e::succeeded(r))) << "mcpp emit sbom: " << s::text(r);
auto d = s::json_file(q.at("sbom.json"));
Expand All @@ -203,6 +211,13 @@ E2E runners{"runners", [] {
if (c["name"] == "cmdline") versions.push_back(c["version"]);
expect(fatal(versions == std::vector<std::string>{"7.7.7"})) << "sbom re-resolved instead of reading the lock";
q.write("mcpp.lock", lock);
// Nothing recorded is said on the narration stream, and the document stays valid.
fs::remove(q.at("mcpp.lock"));
r = q.mcpp({"emit", "sbom"});
expect(fatal(e2e::succeeded(r))) << "mcpp emit sbom with no lock: " << s::text(r);
expect(fatal(e2e::contains(r.err, "no mcpp.lock"))) << "sbom did not say that nothing was recorded: " << r.err;
expect(fatal(s::json(r.out)["components"].empty())) << "sbom with no lock is not a root-only document: " << r.out;
q.write("mcpp.lock", lock);

s::project(work / "bsp", {{"mcpp.toml", "[package]\nname = \"demo-board-rt\"\nversion = \"0.1.0\"\n\n[features]\ndefault = [\"emulator\"]\nemulator = []\nhardware = []\n"},
{"build.mcpp", R"(import mcpp;
Expand Down
Loading