Refactor: extract source file patching

This commit is contained in:
Alex Shabalin 2021-11-23 09:57:57 +01:00
parent 0c9c7d5d44
commit ab3c6a2295
2 changed files with 44 additions and 33 deletions

View file

@ -542,48 +542,59 @@ std::optional<Path> getSourceFilePathFromInStorePath(Store & store, const Path &
return betterFilePath; return betterFilePath;
} }
std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> toDerivationImpl(InstallableFlake & flake) {
auto lockedFlake = flake.getLockedFlake();
auto cache = openEvalCache(*flake.state, lockedFlake);
auto root = cache->getRoot();
for (auto & attrPath : flake.getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(
parseAttrPath(*flake.state, attrPath),
true
);
if (!attr) continue;
if (!attr->isDerivation())
throw Error("flake output attribute '%s' is not a derivation", attrPath);
auto drvPath = attr->forceDerivation();
auto drvInfo = InstallableValue::DerivationInfo{
std::move(drvPath),
flake.state->store->maybeParseStorePath(attr->getAttr(flake.state->sOutPath)->getString()),
attr->getAttr(flake.state->sOutputName)->getString()
};
return {attrPath, lockedFlake->flake.lockedRef, std::move(drvInfo)};
}
throw Error("flake '%s' does not provide attribute %s",
flake.flakeRef, showAttrPaths(flake.getActualAttrPaths()));
} }
std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableFlake::toDerivation() template <class F>
{ auto patchErrorSourceFile(Store & store, const fetchers::Input & flakeInput, F && func) {
try { try {
auto lockedFlake = getLockedFlake(); return func();
auto cache = openEvalCache(*state, lockedFlake);
auto root = cache->getRoot();
for (auto & attrPath : getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(
parseAttrPath(*state, attrPath),
true
);
if (!attr) continue;
if (!attr->isDerivation())
throw Error("flake output attribute '%s' is not a derivation", attrPath);
auto drvPath = attr->forceDerivation();
auto drvInfo = DerivationInfo{
std::move(drvPath),
state->store->maybeParseStorePath(attr->getAttr(state->sOutPath)->getString()),
attr->getAttr(state->sOutputName)->getString()
};
return {attrPath, lockedFlake->flake.lockedRef, std::move(drvInfo)};
}
} catch (BaseError & e) { } catch (BaseError & e) {
if (auto pos = e.info().errPos) { if (auto pos = e.info().errPos) {
if (auto sourceFilePath = getSourceFilePathFromInStorePath(*state->store, pos->file, flakeRef.input)) { if (auto sourceFilePath = getSourceFilePathFromInStorePath(store, pos->file, flakeInput)) {
e.setBetterErrPosFile(*sourceFilePath); e.replacePosFile(*sourceFilePath);
} }
} }
throw; throw;
} }
}
}
throw Error("flake '%s' does not provide attribute %s", std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableFlake::toDerivation()
flakeRef, showAttrPaths(getActualAttrPaths())); {
return patchErrorSourceFile(*state->store, flakeRef.input, [&] {
return toDerivationImpl(*this);
});
} }
std::vector<InstallableValue::DerivationInfo> InstallableFlake::toDerivations() std::vector<InstallableValue::DerivationInfo> InstallableFlake::toDerivations()

View file

@ -165,7 +165,7 @@ public:
const string & msg() const { return calcWhat(); } const string & msg() const { return calcWhat(); }
const ErrorInfo & info() const { calcWhat(); return err; } const ErrorInfo & info() const { calcWhat(); return err; }
void setBetterErrPosFile(std::string file) { void replacePosFile(std::string file) {
if (err.errPos) { if (err.errPos) {
err.errPos->file = std::move(file); err.errPos->file = std::move(file);
} }