Evaluate nix-shell -i args relative to script

When writing a shebang script, you expect your path to be relative to
the script, not the cwd. We previously handled this correctly for
relative file paths, but not for expressions.

This handles both -p & -E args. My understanding is this should be
what we want in any cases I can think of - people run scripts from
many different working directories. @edolstra is there any reason to
handle -p args differently in this case?

Fixes #4232
This commit is contained in:
Matthew Bauer 2021-08-03 15:25:15 -05:00 committed by Tom Bereknyei
parent f0180487a0
commit 04023360ed

View File

@ -298,7 +298,9 @@ static void main_nix_build(int argc, char * * argv)
else
for (auto i : left) {
if (fromArgs)
exprs.push_back(state->parseExprFromString(std::move(i), state->rootPath(CanonPath::fromCwd())));
exprs.push_back(state->parseExprFromString(
std::move(i),
state->rootPath(CanonPath::fromCwd(inShebang ? dirOf(script) : "."))));
else {
auto absolute = i;
try {
@ -311,7 +313,7 @@ static void main_nix_build(int argc, char * * argv)
/* If we're in a #! script, interpret filenames
relative to the script. */
exprs.push_back(state->parseExprFromFile(resolveExprPath(state->checkSourcePath(lookupFileArg(*state,
inShebang && !packages ? absPath(i, absPath(dirOf(script))) : i)))));
inShebang ? absPath(i, absPath(dirOf(script))) : i)))));
}
}