Skip to content

Commit 6e2fcb7

Browse files
committed
Parser: Respect the accessor of the source file for relative paths
1 parent 38f3914 commit 6e2fcb7

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/libexpr/parser.y

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,18 @@ string_parts_interpolated
359359

360360
path_start
361361
: PATH {
362-
Path path(absPath(std::string_view{$1.p, $1.l}, state->basePath.path.abs()));
362+
std::string_view literal({$1.p, $1.l});
363+
Path path(absPath(literal, state->basePath.path.abs()));
363364
/* add back in the trailing '/' to the first segment */
364-
if ($1.p[$1.l-1] == '/' && $1.l > 1)
365-
path += "/";
366-
$$ = new ExprPath(ref<SourceAccessor>(state->rootFS), std::move(path));
365+
if (literal.size() > 1 && literal.back() == '/')
366+
path += '/';
367+
$$ =
368+
/* Absolute paths are always interpreted relative to the
369+
root filesystem accessor, rather than the accessor of the
370+
current Nix expression. */
371+
literal.front() == '/'
372+
? new ExprPath(state->rootFS, std::move(path))
373+
: new ExprPath(state->basePath.accessor, std::move(path));
367374
}
368375
| HPATH {
369376
if (state->settings.pureEval) {

src/libflake/flake/flake.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ static FlakeInput parseFlakeInput(
186186
url = attr.value->string_view();
187187
else if (attr.value->type() == nPath) {
188188
auto path = attr.value->path();
189-
if (path.accessor != flakeDir.accessor
190-
// FIXME: hack necessary since the parser currently stores all paths as inside rootFS.
191-
&& flakeDir.accessor == state.rootFS)
189+
if (path.accessor != flakeDir.accessor)
192190
throw Error("input attribute path '%s' at %s must be in the same source tree as %s",
193191
path, state.positions[attr.pos], flakeDir);
194192
url = "path:" + flakeDir.path.makeRelative(path.path);
@@ -337,9 +335,7 @@ static Flake readFlake(
337335
state.symbols[setting.name],
338336
std::string(state.forceStringNoCtx(*setting.value, setting.pos, "")));
339337
else if (setting.value->type() == nPath) {
340-
// FIXME: hack necessary since the parser currently stores all paths as inside rootFS.
341-
SourcePath path(rootDir.accessor, setting.value->path().path);
342-
auto storePath = fetchToStore(*state.store, path, FetchMode::Copy);
338+
auto storePath = fetchToStore(*state.store, setting.value->path(), FetchMode::Copy);
343339
flake.config.settings.emplace(
344340
state.symbols[setting.name],
345341
state.store->toRealPath(storePath));

0 commit comments

Comments
 (0)