Skip to content

Commit 8014671

Browse files
committed
Improve build failure error messages
1 parent 7c47777 commit 8014671

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/libstore/build/derivation-goal.cc

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,22 @@ Goal::Co DerivationGoal::haveDerivation()
322322
}
323323

324324

325+
static std::string showKnownOutputs(Store & store, const Derivation & drv)
326+
{
327+
std::string msg;
328+
StorePathSet expectedOutputPaths;
329+
for (auto & i : drv.outputsAndOptPaths(store))
330+
if (i.second.second)
331+
expectedOutputPaths.insert(*i.second.second);
332+
if (!expectedOutputPaths.empty()) {
333+
msg += "\nOutput paths:";
334+
for (auto & p : expectedOutputPaths)
335+
msg += fmt("\n %s", Magenta(store.printStorePath(p)));
336+
}
337+
return msg;
338+
}
339+
340+
325341
/* At least one of the output paths could not be
326342
produced using a substitute. So we have to build instead. */
327343
Goal::Co DerivationGoal::gaveUpOnSubstitution()
@@ -392,9 +408,14 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution()
392408
if (nrFailed != 0) {
393409
if (!useDerivation)
394410
throw Error("some dependencies of '%s' are missing", worker.store.printStorePath(drvPath));
395-
co_return done(BuildResult::DependencyFailed, {}, Error(
396-
"%s dependencies of derivation '%s' failed to build",
397-
nrFailed, worker.store.printStorePath(drvPath)));
411+
auto msg = fmt(
412+
"Cannot build '%s'.\n"
413+
"Reason: " ANSI_RED "%d %s failed" ANSI_NORMAL ".",
414+
Magenta(worker.store.printStorePath(drvPath)),
415+
nrFailed,
416+
nrFailed == 1 ? "dependency" : "dependencies");
417+
msg += showKnownOutputs(worker.store, *drv);
418+
co_return done(BuildResult::DependencyFailed, {}, Error(msg));
398419
}
399420

400421
if (retrySubstitution == RetrySubstitution::YesNeed) {
@@ -955,12 +976,16 @@ Goal::Co DerivationGoal::buildDone()
955976

956977
diskFull |= cleanupDecideWhetherDiskFull();
957978

958-
auto msg = fmt("builder for '%s' %s",
979+
auto msg = fmt(
980+
"Cannot build '%s'.\n"
981+
"Reason: " ANSI_RED "builder %s" ANSI_NORMAL ".",
959982
Magenta(worker.store.printStorePath(drvPath)),
960983
statusToString(status));
961984

985+
msg += showKnownOutputs(worker.store, *drv);
986+
962987
if (!logger->isVerbose() && !logTail.empty()) {
963-
msg += fmt(";\nlast %d log lines:\n", logTail.size());
988+
msg += fmt("\nLast %d log lines:\n", logTail.size());
964989
for (auto & line : logTail) {
965990
msg += "> ";
966991
msg += line;

tests/functional/build.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,14 @@ test "$(<<<"$out" grep -cE '^error:')" = 4
179179
out="$(nix build -f fod-failing.nix -L x4 2>&1)" && status=0 || status=$?
180180
test "$status" = 1
181181
test "$(<<<"$out" grep -cE '^error:')" = 2
182-
<<<"$out" grepQuiet -E "error: 1 dependencies of derivation '.*-x4\\.drv' failed to build"
182+
<<<"$out" grepQuiet -E "error: Cannot build '.*-x4\\.drv'"
183+
<<<"$out" grepQuiet -E "Reason: 1 dependency failed."
183184
<<<"$out" grepQuiet -E "hash mismatch in fixed-output derivation '.*-x2\\.drv'"
184185

185186
out="$(nix build -f fod-failing.nix -L x4 --keep-going 2>&1)" && status=0 || status=$?
186187
test "$status" = 1
187188
test "$(<<<"$out" grep -cE '^error:')" = 3
188-
<<<"$out" grepQuiet -E "error: 2 dependencies of derivation '.*-x4\\.drv' failed to build"
189+
<<<"$out" grepQuiet -E "error: Cannot build '.*-x4\\.drv'"
190+
<<<"$out" grepQuiet -E "Reason: 2 dependencies failed."
189191
<<<"$out" grepQuiet -vE "hash mismatch in fixed-output derivation '.*-x3\\.drv'"
190192
<<<"$out" grepQuiet -vE "hash mismatch in fixed-output derivation '.*-x2\\.drv'"

0 commit comments

Comments
 (0)