Skip to content

Commit 5c6785e

Browse files
authored
Merge pull request #12392 from obsidiansystems/simplify-state-machine
Simplify state machine
2 parents c73096b + 2297cc0 commit 5c6785e

File tree

2 files changed

+40
-66
lines changed

2 files changed

+40
-66
lines changed

src/libstore/build/derivation-goal.cc

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@
3636

3737
namespace nix {
3838

39-
Goal::Co DerivationGoal::init() {
40-
if (useDerivation) {
41-
co_return getDerivation();
42-
} else {
43-
co_return haveDerivation();
44-
}
45-
}
46-
4739
DerivationGoal::DerivationGoal(const StorePath & drvPath,
4840
const OutputsSpec & wantedOutputs, Worker & worker, BuildMode buildMode)
4941
: Goal(worker, DerivedPath::Built { .drvPath = makeConstantStorePathRef(drvPath), .outputs = wantedOutputs })
@@ -141,50 +133,44 @@ void DerivationGoal::addWantedOutputs(const OutputsSpec & outputs)
141133
}
142134

143135

144-
Goal::Co DerivationGoal::getDerivation()
145-
{
136+
Goal::Co DerivationGoal::init() {
146137
trace("init");
147138

148-
/* The first thing to do is to make sure that the derivation
149-
exists. If it doesn't, it may be created through a
150-
substitute. */
151-
if (buildMode == bmNormal && worker.evalStore.isValidPath(drvPath)) {
152-
co_return loadDerivation();
153-
}
154-
155-
addWaitee(upcast_goal(worker.makePathSubstitutionGoal(drvPath)));
156-
157-
co_await Suspend{};
158-
co_return loadDerivation();
159-
}
139+
if (useDerivation) {
140+
/* The first thing to do is to make sure that the derivation
141+
exists. If it doesn't, it may be created through a
142+
substitute. */
160143

144+
if (buildMode != bmNormal || !worker.evalStore.isValidPath(drvPath)) {
145+
addWaitee(upcast_goal(worker.makePathSubstitutionGoal(drvPath)));
146+
co_await Suspend{};
147+
}
161148

162-
Goal::Co DerivationGoal::loadDerivation()
163-
{
164-
trace("loading derivation");
149+
trace("loading derivation");
165150

166-
if (nrFailed != 0) {
167-
co_return done(BuildResult::MiscFailure, {}, Error("cannot build missing derivation '%s'", worker.store.printStorePath(drvPath)));
168-
}
151+
if (nrFailed != 0) {
152+
co_return done(BuildResult::MiscFailure, {}, Error("cannot build missing derivation '%s'", worker.store.printStorePath(drvPath)));
153+
}
169154

170-
/* `drvPath' should already be a root, but let's be on the safe
171-
side: if the user forgot to make it a root, we wouldn't want
172-
things being garbage collected while we're busy. */
173-
worker.evalStore.addTempRoot(drvPath);
155+
/* `drvPath' should already be a root, but let's be on the safe
156+
side: if the user forgot to make it a root, we wouldn't want
157+
things being garbage collected while we're busy. */
158+
worker.evalStore.addTempRoot(drvPath);
174159

175-
/* Get the derivation. It is probably in the eval store, but it might be inthe main store:
160+
/* Get the derivation. It is probably in the eval store, but it might be inthe main store:
176161
177-
- Resolved derivation are resolved against main store realisations, and so must be stored there.
162+
- Resolved derivation are resolved against main store realisations, and so must be stored there.
178163
179-
- Dynamic derivations are built, and so are found in the main store.
180-
*/
181-
for (auto * drvStore : { &worker.evalStore, &worker.store }) {
182-
if (drvStore->isValidPath(drvPath)) {
183-
drv = std::make_unique<Derivation>(drvStore->readDerivation(drvPath));
184-
break;
164+
- Dynamic derivations are built, and so are found in the main store.
165+
*/
166+
for (auto * drvStore : { &worker.evalStore, &worker.store }) {
167+
if (drvStore->isValidPath(drvPath)) {
168+
drv = std::make_unique<Derivation>(drvStore->readDerivation(drvPath));
169+
break;
170+
}
185171
}
172+
assert(drv);
186173
}
187-
assert(drv);
188174

189175
co_return haveDerivation();
190176
}
@@ -235,12 +221,14 @@ Goal::Co DerivationGoal::haveDerivation()
235221
}
236222
});
237223

238-
/* Check what outputs paths are not already valid. */
239-
auto [allValid, validOutputs] = checkPathValidity();
224+
{
225+
/* Check what outputs paths are not already valid. */
226+
auto [allValid, validOutputs] = checkPathValidity();
240227

241-
/* If they are all valid, then we're done. */
242-
if (allValid && buildMode == bmNormal) {
243-
co_return done(BuildResult::AlreadyValid, std::move(validOutputs));
228+
/* If they are all valid, then we're done. */
229+
if (allValid && buildMode == bmNormal) {
230+
co_return done(BuildResult::AlreadyValid, std::move(validOutputs));
231+
}
244232
}
245233

246234
/* We are first going to try to create the invalid output paths
@@ -268,12 +256,7 @@ Goal::Co DerivationGoal::haveDerivation()
268256
}
269257

270258
if (!waitees.empty()) co_await Suspend{}; /* to prevent hang (no wake-up event) */
271-
co_return outputsSubstitutionTried();
272-
}
273259

274-
275-
Goal::Co DerivationGoal::outputsSubstitutionTried()
276-
{
277260
trace("all outputs substituted (maybe)");
278261

279262
assert(!drv->type().isImpure());
@@ -460,18 +443,13 @@ Goal::Co DerivationGoal::repairClosure()
460443
co_return done(BuildResult::AlreadyValid, assertPathValidity());
461444
} else {
462445
co_await Suspend{};
463-
co_return closureRepaired();
464-
}
465-
}
466-
467446

468-
Goal::Co DerivationGoal::closureRepaired()
469-
{
470-
trace("closure repaired");
471-
if (nrFailed > 0)
472-
throw Error("some paths in the output closure of derivation '%s' could not be repaired",
473-
worker.store.printStorePath(drvPath));
474-
co_return done(BuildResult::AlreadyValid, assertPathValidity());
447+
trace("closure repaired");
448+
if (nrFailed > 0)
449+
throw Error("some paths in the output closure of derivation '%s' could not be repaired",
450+
worker.store.printStorePath(drvPath));
451+
co_return done(BuildResult::AlreadyValid, assertPathValidity());
452+
}
475453
}
476454

477455

src/libstore/build/derivation-goal.hh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,8 @@ struct DerivationGoal : public Goal
233233
* The states.
234234
*/
235235
Co init() override;
236-
Co getDerivation();
237-
Co loadDerivation();
238236
Co haveDerivation();
239-
Co outputsSubstitutionTried();
240237
Co gaveUpOnSubstitution();
241-
Co closureRepaired();
242238
Co inputsRealised();
243239
Co tryToBuild();
244240
virtual Co tryLocalBuild();

0 commit comments

Comments
 (0)