Skip to content

Commit 53ffa02

Browse files
committed
prepare: move the --locked assertion out of step13_lockfile
The missing-lock check pushed step13_lockfile past the 400 line function-size limit that the lint job enforces under src/build/prepare. The assertion is now its own helper next to target_drift; behaviour is unchanged.
1 parent 8b79c9d commit 53ffa02

1 file changed

Lines changed: 56 additions & 43 deletions

File tree

‎src/build/prepare/records.cpp‎

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,60 @@ static std::optional<std::string> target_drift(const PrepareState& state,
129129
c.why.empty() ? std::string{} : "; " + c.why);
130130
}
131131

132+
// THE `--locked` ASSERTION: this resolution is the recorded one, or the
133+
// failure names what moved. `partialWorkspace` is a plan of some of a
134+
// workspace's members, whose unresolved entries are not drift.
135+
static std::expected<void, std::string> check_locked(const PrepareState& state,
136+
const mcpp::lockfile::Lockfile& lock,
137+
const std::optional<mcpp::lockfile::LockedTarget>& priorTarget,
138+
const std::filesystem::path& lockPath,
139+
bool partialWorkspace) {
140+
// `load` reads a missing file as an empty lock, which would
141+
// report every package as drift from a file that is not there.
142+
auto prior = mcpp::lockfile::load(lockPath);
143+
if (!prior || !std::filesystem::exists(lockPath)) {
144+
return std::unexpected(std::format(
145+
"--locked was given and there is no readable mcpp.lock at {}\n"
146+
" Run the same command without --locked once to record "
147+
"this resolution, then commit mcpp.lock.",
148+
lockPath.string()));
149+
}
150+
auto key = [](const mcpp::lockfile::LockedPackage& p) {
151+
const std::string sn = lock_short_name(p);
152+
return p.namespace_.empty() ? sn
153+
: p.namespace_ + "." + sn;
154+
};
155+
std::map<std::string, std::string> was, now;
156+
// An override is part of the recorded resolution (T2): the
157+
// same version chosen by a merge or by the root is drift.
158+
auto recorded = [](const mcpp::lockfile::LockedPackage& p) {
159+
return p.overridden ? p.version + " (override)" : p.version;
160+
};
161+
for (auto const& p : prior->packages) was[key(p)] = recorded(p);
162+
for (auto const& p : lock.packages) now[key(p)] = recorded(p);
163+
std::vector<std::string> drift;
164+
if (auto d = target_drift(state, priorTarget)) drift.push_back(std::move(*d));
165+
for (auto const& [k, v] : now) {
166+
auto it = was.find(k);
167+
if (it == was.end()) drift.push_back(k + " " + v + " (not in the lock)");
168+
else if (it->second != v) drift.push_back(k + " " + it->second + " -> " + v);
169+
}
170+
// A plan of some of a workspace's members resolves some of
171+
// the lock; what it did not resolve is not drift.
172+
if (!partialWorkspace)
173+
for (auto const& [k, v] : was)
174+
if (!now.contains(k)) drift.push_back(k + " " + v + " (no longer resolved)");
175+
if (!drift.empty()) {
176+
std::string msg = "--locked was given and this resolution "
177+
"differs from mcpp.lock:";
178+
for (auto const& d : drift) msg += "\n " + d;
179+
msg += "\n Re-run without --locked to update the lock, "
180+
"or pin the dependency that moved.";
181+
return std::unexpected(msg);
182+
}
183+
return {};
184+
}
185+
132186
std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildContext& ctx) {
133187
// Write/update mcpp.lock for any version-based deps that succeeded.
134188
// Path deps are intentionally NOT locked — their source is local filesystem.
@@ -459,49 +513,8 @@ std::expected<void, std::string> step13_lockfile(PrepareState& state, BuildConte
459513
// true and useless; which package moved, from which version to
460514
// which, is what the reader does something about.
461515
if (mcpp::invocation::get("MCPP_LOCKED").value_or("") == "1") {
462-
// `load` reads a missing file as an empty lock, which would
463-
// report every package as drift from a file that is not there.
464-
auto prior = mcpp::lockfile::load(lockPath);
465-
if (!prior || !std::filesystem::exists(lockPath)) {
466-
return std::unexpected(std::format(
467-
"--locked was given and there is no readable mcpp.lock at {}\n"
468-
" Run the same command without --locked once to record "
469-
"this resolution, then commit mcpp.lock.",
470-
lockPath.string()));
471-
}
472-
auto key = [](const mcpp::lockfile::LockedPackage& p) {
473-
const std::string sn = lock_short_name(p);
474-
return p.namespace_.empty() ? sn
475-
: p.namespace_ + "." + sn;
476-
};
477-
std::map<std::string, std::string> was, now;
478-
// An override is part of the recorded resolution (T2): the
479-
// same version chosen by a merge or by the root is drift.
480-
auto recorded = [](const mcpp::lockfile::LockedPackage& p) {
481-
return p.overridden ? p.version + " (override)" : p.version;
482-
};
483-
for (auto const& p : prior->packages) was[key(p)] = recorded(p);
484-
for (auto const& p : lock.packages) now[key(p)] = recorded(p);
485-
std::vector<std::string> drift;
486-
if (auto d = target_drift(state, priorTarget)) drift.push_back(std::move(*d));
487-
for (auto const& [k, v] : now) {
488-
auto it = was.find(k);
489-
if (it == was.end()) drift.push_back(k + " " + v + " (not in the lock)");
490-
else if (it->second != v) drift.push_back(k + " " + it->second + " -> " + v);
491-
}
492-
// A plan of some of a workspace's members resolves some of
493-
// the lock; what it did not resolve is not drift.
494-
if (!partialWorkspace)
495-
for (auto const& [k, v] : was)
496-
if (!now.contains(k)) drift.push_back(k + " " + v + " (no longer resolved)");
497-
if (!drift.empty()) {
498-
std::string msg = "--locked was given and this resolution "
499-
"differs from mcpp.lock:";
500-
for (auto const& d : drift) msg += "\n " + d;
501-
msg += "\n Re-run without --locked to update the lock, "
502-
"or pin the dependency that moved.";
503-
return std::unexpected(msg);
504-
}
516+
if (auto r = check_locked(state, lock, priorTarget, lockPath, partialWorkspace); !r)
517+
return std::unexpected(std::move(r.error()));
505518
}
506519
// OPTIMISTIC: no lock is held while the graph resolves (it may
507520
// reach the network). What this run read is compared with what is

0 commit comments

Comments
 (0)