1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-10 04:26:01 +01:00

DerivationBuilderImpl::unprepareBuild Just throw error

Aftet the previous simplifications, there is no reason to catch the
error and immediately return it with a `std::variant` --- just let the
caller catch it instead.
This commit is contained in:
John Ericson 2025-08-28 11:58:28 -04:00
parent 0b85b023d8
commit 374f8e79a1
4 changed files with 45 additions and 50 deletions

View file

@ -804,21 +804,22 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
trace("build done"); trace("build done");
auto res = builder->unprepareBuild(); SingleDrvOutputs builtOutputs;
// N.B. cannot use `std::visit` with co-routine return try {
if (auto * ste = std::get_if<0>(&res)) { builtOutputs = builder->unprepareBuild();
} catch (BuildError & e) {
outputLocks.unlock(); outputLocks.unlock();
// Allow selecting a subset of enum values // Allow selecting a subset of enum values
# pragma GCC diagnostic push # pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wswitch-enum" # pragma GCC diagnostic ignored "-Wswitch-enum"
switch (ste->status) { switch (e.status) {
case BuildResult::HashMismatch: case BuildResult::HashMismatch:
worker.hashMismatch = true; worker.hashMismatch = true;
/* See header, the protocols don't know about `HashMismatch` /* See header, the protocols don't know about `HashMismatch`
yet, so change it to `OutputRejected`, which they expect yet, so change it to `OutputRejected`, which they expect
for this case (hash mismatch is a type of output for this case (hash mismatch is a type of output
rejection). */ rejection). */
ste->status = BuildResult::OutputRejected; e.status = BuildResult::OutputRejected;
break; break;
case BuildResult::NotDeterministic: case BuildResult::NotDeterministic:
worker.checkMismatch = true; worker.checkMismatch = true;
@ -828,10 +829,11 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
break; break;
} }
# pragma GCC diagnostic pop # pragma GCC diagnostic pop
co_return doneFailure(std::move(*ste)); co_return doneFailure(std::move(e));
} else if (auto * builtOutputs = std::get_if<1>(&res)) { }
{
StorePathSet outputPaths; StorePathSet outputPaths;
for (auto & [_, output] : *builtOutputs) for (auto & [_, output] : builtOutputs)
outputPaths.insert(output.outPath); outputPaths.insert(output.outPath);
runPostBuildHook(worker.store, *logger, drvPath, outputPaths); runPostBuildHook(worker.store, *logger, drvPath, outputPaths);
@ -841,9 +843,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild()
(unlinked) lock files. */ (unlinked) lock files. */
outputLocks.setDeletion(true); outputLocks.setDeletion(true);
outputLocks.unlock(); outputLocks.unlock();
co_return doneSuccess(BuildResult::Built, std::move(*builtOutputs)); co_return doneSuccess(BuildResult::Built, std::move(builtOutputs));
} else {
unreachable();
} }
#endif #endif
} }

View file

@ -187,8 +187,10 @@ struct DerivationBuilder : RestrictionContext
* processing. A status code and exception are returned, providing * processing. A status code and exception are returned, providing
* more information. The second case indicates success, and * more information. The second case indicates success, and
* realisations for each output of the derivation are returned. * realisations for each output of the derivation are returned.
*
* @throws BuildError
*/ */
virtual std::variant<BuildError, SingleDrvOutputs> unprepareBuild() = 0; virtual SingleDrvOutputs unprepareBuild() = 0;
/** /**
* Stop the in-process nix daemon thread. * Stop the in-process nix daemon thread.

View file

@ -191,7 +191,7 @@ public:
void startBuilder() override; void startBuilder() override;
std::variant<BuildError, SingleDrvOutputs> unprepareBuild() override; SingleDrvOutputs unprepareBuild() override;
protected: protected:
@ -426,7 +426,7 @@ bool DerivationBuilderImpl::prepareBuild()
return true; return true;
} }
std::variant<BuildError, SingleDrvOutputs> DerivationBuilderImpl::unprepareBuild() SingleDrvOutputs DerivationBuilderImpl::unprepareBuild()
{ {
// FIXME: get rid of this, rely on RAII. // FIXME: get rid of this, rely on RAII.
Finally releaseBuildUser([&]() { Finally releaseBuildUser([&]() {
@ -477,49 +477,42 @@ std::variant<BuildError, SingleDrvOutputs> DerivationBuilderImpl::unprepareBuild
bool diskFull = false; bool diskFull = false;
try { /* Check the exit status. */
if (!statusOk(status)) {
/* Check the exit status. */ diskFull |= decideWhetherDiskFull();
if (!statusOk(status)) {
diskFull |= decideWhetherDiskFull(); cleanupBuild();
cleanupBuild(); auto msg =
fmt("Cannot build '%s'.\n"
"Reason: " ANSI_RED "builder %s" ANSI_NORMAL ".",
Magenta(store.printStorePath(drvPath)),
statusToString(status));
auto msg = msg += showKnownOutputs(store, drv);
fmt("Cannot build '%s'.\n"
"Reason: " ANSI_RED "builder %s" ANSI_NORMAL ".",
Magenta(store.printStorePath(drvPath)),
statusToString(status));
msg += showKnownOutputs(store, drv); miscMethods->appendLogTailErrorMsg(msg);
miscMethods->appendLogTailErrorMsg(msg); if (diskFull)
msg += "\nnote: build failure may have been caused by lack of free disk space";
if (diskFull) throw BuildError(
msg += "\nnote: build failure may have been caused by lack of free disk space"; !derivationType.isSandboxed() || diskFull ? BuildResult::TransientFailure : BuildResult::PermanentFailure,
msg);
throw BuildError(
!derivationType.isSandboxed() || diskFull ? BuildResult::TransientFailure
: BuildResult::PermanentFailure,
msg);
}
/* Compute the FS closure of the outputs and register them as
being valid. */
auto builtOutputs = registerOutputs();
/* Delete unused redirected outputs (when doing hash rewriting). */
for (auto & i : redirectedOutputs)
deletePath(store.Store::toRealPath(i.second));
deleteTmpDir(true);
return std::move(builtOutputs);
} catch (BuildError & e) {
return std::move(e);
} }
/* Compute the FS closure of the outputs and register them as
being valid. */
auto builtOutputs = registerOutputs();
/* Delete unused redirected outputs (when doing hash rewriting). */
for (auto & i : redirectedOutputs)
deletePath(store.Store::toRealPath(i.second));
deleteTmpDir(true);
return builtOutputs;
} }
void DerivationBuilderImpl::cleanupBuild() void DerivationBuilderImpl::cleanupBuild()

View file

@ -659,7 +659,7 @@ struct ChrootLinuxDerivationBuilder : ChrootDerivationBuilder, LinuxDerivationBu
throw SysError("setuid failed"); throw SysError("setuid failed");
} }
std::variant<BuildError, SingleDrvOutputs> unprepareBuild() override SingleDrvOutputs unprepareBuild() override
{ {
sandboxMountNamespace = -1; sandboxMountNamespace = -1;
sandboxUserNamespace = -1; sandboxUserNamespace = -1;