1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 06:01:48 +02:00

Parser: Respect the accessor of the source file for relative paths

This commit is contained in:
Eelco Dolstra 2025-02-06 16:31:42 +01:00
parent 38f391444b
commit 6e2fcb7e29
2 changed files with 13 additions and 10 deletions

View file

@ -359,11 +359,18 @@ string_parts_interpolated
path_start path_start
: PATH { : PATH {
Path path(absPath(std::string_view{$1.p, $1.l}, state->basePath.path.abs())); std::string_view literal({$1.p, $1.l});
Path path(absPath(literal, state->basePath.path.abs()));
/* add back in the trailing '/' to the first segment */ /* add back in the trailing '/' to the first segment */
if ($1.p[$1.l-1] == '/' && $1.l > 1) if (literal.size() > 1 && literal.back() == '/')
path += "/"; path += '/';
$$ = new ExprPath(ref<SourceAccessor>(state->rootFS), std::move(path)); $$ =
/* Absolute paths are always interpreted relative to the
root filesystem accessor, rather than the accessor of the
current Nix expression. */
literal.front() == '/'
? new ExprPath(state->rootFS, std::move(path))
: new ExprPath(state->basePath.accessor, std::move(path));
} }
| HPATH { | HPATH {
if (state->settings.pureEval) { if (state->settings.pureEval) {

View file

@ -186,9 +186,7 @@ static FlakeInput parseFlakeInput(
url = attr.value->string_view(); url = attr.value->string_view();
else if (attr.value->type() == nPath) { else if (attr.value->type() == nPath) {
auto path = attr.value->path(); auto path = attr.value->path();
if (path.accessor != flakeDir.accessor if (path.accessor != flakeDir.accessor)
// FIXME: hack necessary since the parser currently stores all paths as inside rootFS.
&& flakeDir.accessor == state.rootFS)
throw Error("input attribute path '%s' at %s must be in the same source tree as %s", throw Error("input attribute path '%s' at %s must be in the same source tree as %s",
path, state.positions[attr.pos], flakeDir); path, state.positions[attr.pos], flakeDir);
url = "path:" + flakeDir.path.makeRelative(path.path); url = "path:" + flakeDir.path.makeRelative(path.path);
@ -337,9 +335,7 @@ static Flake readFlake(
state.symbols[setting.name], state.symbols[setting.name],
std::string(state.forceStringNoCtx(*setting.value, setting.pos, ""))); std::string(state.forceStringNoCtx(*setting.value, setting.pos, "")));
else if (setting.value->type() == nPath) { else if (setting.value->type() == nPath) {
// FIXME: hack necessary since the parser currently stores all paths as inside rootFS. auto storePath = fetchToStore(*state.store, setting.value->path(), FetchMode::Copy);
SourcePath path(rootDir.accessor, setting.value->path().path);
auto storePath = fetchToStore(*state.store, path, FetchMode::Copy);
flake.config.settings.emplace( flake.config.settings.emplace(
state.symbols[setting.name], state.symbols[setting.name],
state.store->toRealPath(storePath)); state.store->toRealPath(storePath));