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

View file

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