Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print worktree path instead of store path #5625

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 50 additions & 18 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,33 +521,65 @@ InstallableFlake::InstallableFlake(
throw UsageError("'--arg' and '--argstr' are incompatible with flakes");
}

namespace {
std::optional<Path> getSourceFilePathFromInStorePath(Store & store, const Path & file, const fetchers::Input & flakeInput) {
if (!store.isInStore(file)) {
return std::nullopt;
}

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> InstallableFlake::toDerivation()
{
auto lockedFlake = getLockedFlake();
try {
auto lockedFlake = getLockedFlake();

auto cache = openEvalCache(*state, lockedFlake);
auto root = cache->getRoot();
auto cache = openEvalCache(*state, lockedFlake);
auto root = cache->getRoot();

for (auto & attrPath : getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(
parseAttrPath(*state, attrPath),
true
);
for (auto & attrPath : getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(
parseAttrPath(*state, attrPath),
true
);

if (!attr) continue;
if (!attr) continue;

if (!attr->isDerivation())
throw Error("flake output attribute '%s' is not a derivation", attrPath);
if (!attr->isDerivation())
throw Error("flake output attribute '%s' is not a derivation", attrPath);

auto drvPath = attr->forceDerivation();
auto drvPath = attr->forceDerivation();

auto drvInfo = DerivationInfo{
std::move(drvPath),
state->store->maybeParseStorePath(attr->getAttr(state->sOutPath)->getString()),
attr->getAttr(state->sOutputName)->getString()
};
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)};
return {attrPath, lockedFlake->flake.lockedRef, std::move(drvInfo)};
}
} catch (BaseError & e) {
if (auto pos = e.info().errPos) {
if (auto sourceFilePath = getSourceFilePathFromInStorePath(*state->store, pos->file, flakeRef.input)) {
e.setBetterErrPosFile(*sourceFilePath);
}
}
throw;
}

throw Error("flake '%s' does not provide attribute %s",
Expand Down
6 changes: 6 additions & 0 deletions src/libutil/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public:

const string & msg() const { return calcWhat(); }
const ErrorInfo & info() const { calcWhat(); return err; }

void setBetterErrPosFile(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)
Expand Down
14 changes: 14 additions & 0 deletions tests/flake-print-local-path-error.sh
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions tests/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ nix_tests = \
describe-stores.sh \
flakes.sh \
flake-local-settings.sh \
flake-print-local-path-error.sh \
build.sh \
compute-levels.sh \
repl.sh ca/repl.sh \
Expand Down