Skip to content

Commit

Permalink
builtins.toPath: Rewrite virtual paths
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed May 14, 2024
1 parent 4d39908 commit e255be6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,10 @@ public:
materializing those store paths. This is a backward
compatibility hack to make buggy derivation attributes like
`tostring ./bla` produce the same evaluation result. */
std::string rewriteVirtualPaths(std::string_view s, PosIdx pos);
std::string rewriteVirtualPaths(
std::string_view s,
std::string_view warning,
PosIdx pos);

/* Replace all virtual paths (i.e. `/nix/store/lazylazy...`) in a
string by a pretty-printed rendition of the corresponding input
Expand Down
4 changes: 2 additions & 2 deletions src/libexpr/paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ std::string EvalState::prettyPrintPaths(std::string_view s)
}
}

std::string EvalState::rewriteVirtualPaths(std::string_view s, PosIdx pos)
std::string EvalState::rewriteVirtualPaths(std::string_view s, std::string_view warning, PosIdx pos)
{
std::string res;

Expand Down Expand Up @@ -120,7 +120,7 @@ std::string EvalState::rewriteVirtualPaths(std::string_view s, PosIdx pos)
assert(accessor != sourceAccessors.end()); // FIXME

warn(
"derivation at %s has an attribute that refers to source tree '%s' without context; this does not work correctly",
std::string(warning), // FIXME: should accept a string_view
positions[pos], accessor->second->showPath(CanonPath::root));

// FIXME: cache this.
Expand Down
10 changes: 9 additions & 1 deletion src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,10 @@ static void derivationStrictInternal(
don't have a context, so aren't accessible from a sandbox) but
we don't want to change evaluation results. */
for (auto & [name, value] : drv.env)
value = state.rewriteVirtualPaths(value, pos);
value = state.rewriteVirtualPaths(
value,
"derivation at %s has an attribute that refers to source tree '%s' without context; this does not work correctly",
pos);

/* Do we have all required attributes? */
if (drv.builder == "")
Expand Down Expand Up @@ -2170,6 +2173,11 @@ static void prim_toFile(EvalState & state, const PosIdx pos, Value * * args, Val
std::string name(state.forceStringNoCtx(*args[0], pos, "while evaluating the first argument passed to builtins.toFile"));
std::string contents(state.forceString(*args[1], context, pos, "while evaluating the second argument passed to builtins.toFile"));

contents = state.rewriteVirtualPaths(
contents,
"call to `builtins.toFile` at %s refers to source tree '%s' without context; this does not work correctly",
pos);

StorePathSet refs;

for (auto c : context) {
Expand Down

0 comments on commit e255be6

Please sign in to comment.