From 8014671210fee96895239f7c04fef76dfea143d0 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 9 May 2025 00:32:41 +0200 Subject: [PATCH] Improve build failure error messages --- src/libstore/build/derivation-goal.cc | 35 +++++++++++++++++++++++---- tests/functional/build.sh | 6 +++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/libstore/build/derivation-goal.cc b/src/libstore/build/derivation-goal.cc index d7f8846bd..33a4af7f0 100644 --- a/src/libstore/build/derivation-goal.cc +++ b/src/libstore/build/derivation-goal.cc @@ -322,6 +322,22 @@ Goal::Co DerivationGoal::haveDerivation() } +static std::string showKnownOutputs(Store & store, const Derivation & drv) +{ + std::string msg; + StorePathSet expectedOutputPaths; + for (auto & i : drv.outputsAndOptPaths(store)) + if (i.second.second) + expectedOutputPaths.insert(*i.second.second); + if (!expectedOutputPaths.empty()) { + msg += "\nOutput paths:"; + for (auto & p : expectedOutputPaths) + msg += fmt("\n %s", Magenta(store.printStorePath(p))); + } + return msg; +} + + /* At least one of the output paths could not be produced using a substitute. So we have to build instead. */ Goal::Co DerivationGoal::gaveUpOnSubstitution() @@ -392,9 +408,14 @@ Goal::Co DerivationGoal::gaveUpOnSubstitution() if (nrFailed != 0) { if (!useDerivation) throw Error("some dependencies of '%s' are missing", worker.store.printStorePath(drvPath)); - co_return done(BuildResult::DependencyFailed, {}, Error( - "%s dependencies of derivation '%s' failed to build", - nrFailed, worker.store.printStorePath(drvPath))); + auto msg = fmt( + "Cannot build '%s'.\n" + "Reason: " ANSI_RED "%d %s failed" ANSI_NORMAL ".", + Magenta(worker.store.printStorePath(drvPath)), + nrFailed, + nrFailed == 1 ? "dependency" : "dependencies"); + msg += showKnownOutputs(worker.store, *drv); + co_return done(BuildResult::DependencyFailed, {}, Error(msg)); } if (retrySubstitution == RetrySubstitution::YesNeed) { @@ -955,12 +976,16 @@ Goal::Co DerivationGoal::buildDone() diskFull |= cleanupDecideWhetherDiskFull(); - auto msg = fmt("builder for '%s' %s", + auto msg = fmt( + "Cannot build '%s'.\n" + "Reason: " ANSI_RED "builder %s" ANSI_NORMAL ".", Magenta(worker.store.printStorePath(drvPath)), statusToString(status)); + msg += showKnownOutputs(worker.store, *drv); + if (!logger->isVerbose() && !logTail.empty()) { - msg += fmt(";\nlast %d log lines:\n", logTail.size()); + msg += fmt("\nLast %d log lines:\n", logTail.size()); for (auto & line : logTail) { msg += "> "; msg += line; diff --git a/tests/functional/build.sh b/tests/functional/build.sh index 3f65a7c2c..d65ac6854 100755 --- a/tests/functional/build.sh +++ b/tests/functional/build.sh @@ -179,12 +179,14 @@ test "$(<<<"$out" grep -cE '^error:')" = 4 out="$(nix build -f fod-failing.nix -L x4 2>&1)" && status=0 || status=$? test "$status" = 1 test "$(<<<"$out" grep -cE '^error:')" = 2 -<<<"$out" grepQuiet -E "error: 1 dependencies of derivation '.*-x4\\.drv' failed to build" +<<<"$out" grepQuiet -E "error: Cannot build '.*-x4\\.drv'" +<<<"$out" grepQuiet -E "Reason: 1 dependency failed." <<<"$out" grepQuiet -E "hash mismatch in fixed-output derivation '.*-x2\\.drv'" out="$(nix build -f fod-failing.nix -L x4 --keep-going 2>&1)" && status=0 || status=$? test "$status" = 1 test "$(<<<"$out" grep -cE '^error:')" = 3 -<<<"$out" grepQuiet -E "error: 2 dependencies of derivation '.*-x4\\.drv' failed to build" +<<<"$out" grepQuiet -E "error: Cannot build '.*-x4\\.drv'" +<<<"$out" grepQuiet -E "Reason: 2 dependencies failed." <<<"$out" grepQuiet -vE "hash mismatch in fixed-output derivation '.*-x3\\.drv'" <<<"$out" grepQuiet -vE "hash mismatch in fixed-output derivation '.*-x2\\.drv'"