1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 23:13:14 +02:00

Don't allow appending a non-absolute path to the root of a source tree

This avoids an inconsistency where a '/' is implicitly inserted when
you append to the root of a source tree, but not when you append to
any other path.
This commit is contained in:
Eelco Dolstra 2022-12-08 16:39:27 +01:00
parent d162222f25
commit 6d104bbbac
2 changed files with 10 additions and 1 deletions

View file

@ -1998,6 +1998,11 @@ void ExprConcatStrings::eval(EvalState & state, Env & env, Value & v)
} else if (firstType == nPath) { } else if (firstType == nPath) {
if (!first) { if (!first) {
auto part = state.coerceToString(i_pos, *vTmp, context, false, false); auto part = state.coerceToString(i_pos, *vTmp, context, false, false);
if (sSize <= 1 && !hasPrefix(*part, "/"))
state.throwEvalError(i_pos,
"cannot append non-absolute path '%1%' to '%2%' (hint: change it to '/%1%')",
(std::string) *part, accessor->root().to_string(),
env, *this);
sSize += part->size(); sSize += part->size();
s.emplace_back(std::move(part)); s.emplace_back(std::move(part));
} }

View file

@ -3,6 +3,10 @@ source common.sh
mkdir -p $TEST_ROOT/foo mkdir -p $TEST_ROOT/foo
echo bla > $TEST_ROOT/foo/bar echo bla > $TEST_ROOT/foo/bar
[[ $(nix eval --raw --impure --expr "builtins.readFile (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; } + \"bar\"))") = bla ]] [[ $(nix eval --raw --impure --expr "builtins.readFile (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; } + \"/bar\"))") = bla ]]
[[ $(nix eval --raw --impure --expr "builtins.readFile (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; } + \"/b\" + \"ar\"))") = bla ]]
(! nix eval --raw --impure --expr "builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; } + \"bar\"")
[[ $(nix eval --json --impure --expr "builtins.readDir (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; }))") = '{"bar":"regular"}' ]] [[ $(nix eval --json --impure --expr "builtins.readDir (builtins.toString (builtins.fetchTree { type = \"path\"; path = \"$TEST_ROOT/foo\"; }))") = '{"bar":"regular"}' ]]