Skip to content

Commit 8613f25

Browse files
committed
fix: a missing mcpp.lock is named by emit sbom and --locked
mcpp::pm::load returns an empty Lockfile when the file does not exist, and two callers took a successful load to mean the file was there, so the branches they wrote for a missing lock were never taken. `mcpp emit sbom` wrote a root-only document, exit 0, without the note its own comment requires: that document is true of a project with no dependencies and false of one that was never built. It now asks whether the file exists before loading it. The note goes to the narration stream, so standard output stays the document. `mcpp build --locked` reported every resolved package as "(not in the lock)" under "this resolution differs from mcpp.lock", naming a file that is not there. It now gives the refusal that says the lock is missing and how to record one. `load` keeps its meaning: the build's own readers want an empty lock from a project that has none. A lock that exists behaves as before in both commands, and one that cannot be read is still an error. Tests: tests/e2e/333 runs `--locked` and `emit sbom` with the lock moved aside. docs/51 states the note in both languages. Closes #891
1 parent 1e6d1b3 commit 8613f25

5 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎docs/51-supported-versions.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ mcpp emit sbom -o sbom.json
113113
It reads `mcpp.lock` rather than resolving again, because a document describing
114114
a different graph from the one that was built is worse than no document. A
115115
component whose licence mcpp does not know is emitted as `NOASSERTION` rather
116-
than omitted: an absent field reads as "not examined".
116+
than omitted: an absent field reads as "not examined". With no `mcpp.lock` the
117+
document names the root package alone, and a note on standard error says that
118+
nothing is recorded: that is the whole truth for a project with no dependencies,
119+
and not for one that has never been built.
117120

118121
## Offline and air-gapped use
119122

‎docs/zh/51-supported-versions.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ mcpp emit sbom -o sbom.json
9797

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

102103
## 离线与隔网使用
103104

‎src/build/prepare/records.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,10 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
426426
// true and useless; which package moved, from which version to
427427
// which, is what the reader does something about.
428428
if (mcpp::invocation::get("MCPP_LOCKED").value_or("") == "1") {
429+
// `load` reads a missing file as an empty lock, which would
430+
// report every package as drift from a file that is not there.
429431
auto prior = mcpp::lockfile::load(lockPath);
430-
if (!prior) {
432+
if (!prior || !std::filesystem::exists(lockPath)) {
431433
return std::unexpected(std::format(
432434
"--locked was given and there is no readable mcpp.lock at {}\n"
433435
" Run the same command without --locked once to record "

‎src/cli/cmd_sbom.cppm‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,16 @@ int cmd_sbom(const mcpplibs::cmdline::ParsedArgs& parsed) {
102102

103103
const auto lockPath = *root / "mcpp.lock";
104104
std::vector<mcpp::lockfile::LockedPackage> pkgs;
105-
if (auto lock = mcpp::lockfile::load(lockPath)) {
105+
// Asked before loading: `load` reads a file that is not there as an empty
106+
// lock, so its answer cannot tell "nothing recorded" from "no dependencies".
107+
if (std::filesystem::exists(lockPath)) {
108+
auto lock = mcpp::lockfile::load(lockPath);
109+
if (!lock) {
110+
mcpp::ui::error(std::format(
111+
"mcpp.lock exists but could not be read: {}", lock.error().message));
112+
return 2;
113+
}
106114
pkgs = lock->packages;
107-
} else if (std::filesystem::exists(lockPath)) {
108-
mcpp::ui::error(std::format(
109-
"mcpp.lock exists but could not be read: {}", lock.error().message));
110-
return 2;
111115
} else {
112116
// NOT AN ERROR, AND NOT SILENCE EITHER. A project with no
113117
// dependencies has no lock and its bill of materials is one component,

‎tests/e2e/333_named_runners_locked_and_sbom.sh‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ esac
101101
echo " ok --locked names the package that moved and both versions"
102102
mv mcpp.lock.bak mcpp.lock
103103

104+
# A LOCK THAT IS NOT THERE IS NOT A LOCK THAT DIFFERS. The refusal says what to
105+
# do about it, and writes no lock for the next run to pass against.
106+
mv mcpp.lock mcpp.lock.bak
107+
if out=$("$MCPP" build --locked 2>&1); then
108+
echo "FAIL: --locked accepted a project with no mcpp.lock"
109+
echo "$out" | tail -3; exit 1
110+
fi
111+
case "$out" in
112+
*"no readable mcpp.lock"*) ;;
113+
*) echo "FAIL: the refusal does not say the lock is missing"; echo "$out"; exit 1 ;;
114+
esac
115+
test ! -e mcpp.lock || { echo "FAIL: a refused --locked wrote mcpp.lock"; exit 1; }
116+
mv mcpp.lock.bak mcpp.lock
117+
echo " ok --locked says a missing lock is missing"
118+
104119
# ── E: the bill of materials describes the RECORDED resolution ─────────────
105120
"$MCPP" emit sbom -o sbom.json >/dev/null 2>&1 || { echo "FAIL: mcpp emit sbom"; exit 1; }
106121
python3 - <<'PY' || exit 1
@@ -135,6 +150,18 @@ print(" ok sbom reads the lock rather than re-resolving")
135150
PY
136151
mv mcpp.lock.bak mcpp.lock
137152

153+
# NOTHING RECORDED IS SAID ON THE NARRATION STREAM, AND THE DOCUMENT STAYS VALID.
154+
# A root-only bill of materials is true of a project with no dependencies and
155+
# false of one that was never built, and only the reader knows which this is.
156+
mv mcpp.lock mcpp.lock.bak
157+
"$MCPP" emit sbom > sbom3.json 2> sbom3.err || { echo "FAIL: mcpp emit sbom with no lock"; exit 1; }
158+
grep -q 'no mcpp.lock' sbom3.err \
159+
|| { echo "FAIL: sbom did not say that nothing was recorded"; cat sbom3.err; exit 1; }
160+
python3 -c 'import json; assert json.load(open("sbom3.json"))["components"] == []' \
161+
|| { echo "FAIL: sbom with no lock is not a root-only document"; exit 1; }
162+
mv mcpp.lock.bak mcpp.lock
163+
echo " ok sbom says when no lock records the resolution"
164+
138165
# ── B: one board package, two environments, chosen by the consumer ─────────
139166
#
140167
# THE EMULATOR/HARDWARE AXIS IS A FEATURE, NOT A FORK. A board reached

0 commit comments

Comments
 (0)