Merge branch 'pr/mopleen/5625'

This commit is contained in:
Max Headroom 2022-02-21 20:49:37 +01:00
commit 351e70ea68
4 changed files with 74 additions and 10 deletions

View file

@ -559,18 +559,39 @@ InstallableFlake::InstallableFlake(
throw UsageError("'--arg' and '--argstr' are incompatible with flakes");
}
std::tuple<std::string, FlakeRef, InstallableValue::DerivationInfo> InstallableFlake::toDerivation()
{
auto lockedFlake = getLockedFlake();
namespace {
std::optional<Path> getSourceFilePathFromInStorePath(Store & store, const Path & file, const fetchers::Input & flakeInput) {
if (!store.isInStore(file)) {
return std::nullopt;
}
auto cache = openEvalCache(*state, lockedFlake);
auto [sourceStorePath, relPath] = store.toStorePath(file);
auto realPath = store.toRealPath(sourceStorePath);
auto maybeFlakeSource = flakeInput.getSourcePath();
if (!maybeFlakeSource) {
return std::nullopt;
}
auto betterFilePath = replaceStrings(file, realPath, *maybeFlakeSource);
if (!pathExists(betterFilePath)) {
return std::nullopt;
}
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 : getActualAttrPaths()) {
for (auto & attrPath : flake.getActualAttrPaths()) {
debug("trying flake output attribute '%s'", attrPath);
auto attr = root->findAlongAttrPath(
parseAttrPath(*state, attrPath),
parseAttrPath(*flake.state, attrPath),
true
);
@ -581,17 +602,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",
flakeRef, showAttrPaths(getActualAttrPaths()));
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(store, pos->file, flakeInput)) {
e.replacePosFile(*sourceFilePath);
}
}
throw;
}
}
}
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

@ -164,6 +164,12 @@ public:
const string & msg() const { return calcWhat(); }
const ErrorInfo & info() const { calcWhat(); return err; }
void replacePosFile(std::string file) {
if (err.errPos) {
err.errPos->file = std::move(file);
}
}
template<typename... Args>
BaseError & addTrace(std::optional<ErrPos> e, const string &fs, const Args & ... args)

View file

@ -0,0 +1,14 @@
source common.sh
clearStore
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
cd $TEST_HOME
cat <<EOF > flake.nix
{
outputs = give me an error here;
}
EOF
nix build |& grep $TEST_HOME || fail "Path should point to home, not to the store"

View file

@ -49,6 +49,7 @@ nix_tests = \
flake-local-settings.sh \
flake-searching.sh \
flake-bundler.sh \
flake-print-local-path-error.sh \
build.sh \
repl.sh ca/repl.sh \
ca/build.sh \