From f856d4fd2501a904084f923fc0a2f0f47284a7b6 Mon Sep 17 00:00:00 2001 From: skye Date: Tue, 24 Feb 2026 16:30:37 -0500 Subject: [PATCH] libexpr: Migrate EvalState::evalFile to return a Value Change-Id: I19ce8623477dd96628476b3e3dd58cfd6a6a6964 --- lix/legacy/nix-env.cc | 2 +- lix/libcmd/cmd-profiles.cc | 3 +-- lix/libcmd/installables.cc | 2 +- lix/libcmd/repl.cc | 6 ++---- lix/libexpr/eval.cc | 22 ++++++++++++---------- lix/libexpr/eval.hh | 2 +- lix/libexpr/primops.cc | 2 +- lix/nix/prefetch.cc | 8 +++----- subprojects/nix-eval-jobs/src/worker.cc | 5 ++--- 9 files changed, 24 insertions(+), 28 deletions(-) diff --git a/lix/legacy/nix-env.cc b/lix/legacy/nix-env.cc index 409e76470..cac85fca7 100644 --- a/lix/legacy/nix-env.cc +++ b/lix/legacy/nix-env.cc @@ -172,7 +172,7 @@ static void loadSourceExpr(EvalState & state, const SourcePath & path_, Value & auto st = path.stat(); if (isNixExpr(state.ctx.paths, path, st)) - state.evalFile(path, v); + v = state.evalFile(path); /* The path is a directory. Put the Nix expressions in the directory in a set, with the file name of each expression as diff --git a/lix/libcmd/cmd-profiles.cc b/lix/libcmd/cmd-profiles.cc index aa06c0cfe..e41b37d37 100644 --- a/lix/libcmd/cmd-profiles.cc +++ b/lix/libcmd/cmd-profiles.cc @@ -20,8 +20,7 @@ DrvInfos queryInstalled(EvalState & state, const Path & userEnv) throw Error("profile '%s' is incompatible with 'nix-env'; please use 'nix profile' instead", userEnv); auto manifestFile = userEnv + "/manifest.nix"; if (pathExists(manifestFile)) { - Value v; - state.evalFile(CanonPath(manifestFile), v); + Value v = state.evalFile(CanonPath(manifestFile)); Bindings & bindings(*state.ctx.mem.allocBindings(0)); getDerivations(state, v, "", bindings, elems, false); } diff --git a/lix/libcmd/installables.cc b/lix/libcmd/installables.cc index 03a6556fe..d7c5d4a3d 100644 --- a/lix/libcmd/installables.cc +++ b/lix/libcmd/installables.cc @@ -455,7 +455,7 @@ Installables SourceExprCommand::parseInstallables( vFile = state.eval(e); } else if (file) - state.evalFile(state.aio.blockOn(lookupFileArg(*evaluator, *file)).unwrap(), vFile); + vFile = state.evalFile(state.aio.blockOn(lookupFileArg(*evaluator, *file)).unwrap()); else { auto & e = evaluator->parseExprFromString(*expr, CanonPath::fromCwd()); vFile = state.eval(e); diff --git a/lix/libcmd/repl.cc b/lix/libcmd/repl.cc index 34a0947a0..9a4d01f0f 100644 --- a/lix/libcmd/repl.cc +++ b/lix/libcmd/repl.cc @@ -1288,10 +1288,8 @@ void NixRepl::loadFile(const Path & path) try { loaded.remove(loadable); loaded.push_back(loadable); - Value v, v2; - state.evalFile( - state.aio.blockOn(lookupFileArg(evaluator, path)).unwrap(always_progresses), v - ); + Value v2; + Value v = state.evalFile(state.aio.blockOn(lookupFileArg(evaluator, path)).unwrap(always_progresses)); state.autoCallFunction(*autoArgs, v, v2, noPos); addAttrsToScope(v2); } catch (...) { diff --git a/lix/libexpr/eval.cc b/lix/libexpr/eval.cc index 71a760ef5..7856a3bec 100644 --- a/lix/libexpr/eval.cc +++ b/lix/libexpr/eval.cc @@ -931,19 +931,17 @@ struct CachedEvalFile explicit CachedEvalFile(Value result): result(result) {} }; -void EvalState::evalFile(const SourcePath & path_, Value & v) +Value EvalState::evalFile(const SourcePath & path_) { auto path = ctx.paths.checkSourcePath(path_); if (auto i = ctx.caches.fileEval.find(path); i != ctx.caches.fileEval.end()) { - v = i->second->result; - return; + return i->second->result; } auto resolvedPath = ctx.paths.resolveExprPath(path); if (auto i = ctx.caches.fileEval.find(resolvedPath); i != ctx.caches.fileEval.end()) { - v = i->second->result; - return; + return i->second->result; } debug("evaluating file '%1%'", resolvedPath); @@ -959,15 +957,19 @@ void EvalState::evalFile(const SourcePath & path_, Value & v) "while evaluating the file '%1%':", resolvedPath.to_string()) : nullptr; - v = eval(e); + Value v = eval(e); + + auto cache = std::allocate_shared(TraceableAllocator(), v); + ctx.caches.fileEval[resolvedPath] = cache; + if (path != resolvedPath) { + ctx.caches.fileEval[path] = cache; + } + + return v; } catch (Error & e) { e.addTrace(nullptr, "while evaluating the file '%1%':", resolvedPath.to_string()); throw; } - - auto cache = std::allocate_shared(TraceableAllocator(), v); - ctx.caches.fileEval[resolvedPath] = cache; - if (path != resolvedPath) ctx.caches.fileEval[path] = cache; } diff --git a/lix/libexpr/eval.hh b/lix/libexpr/eval.hh index 8693bcfb2..e571e8ba2 100644 --- a/lix/libexpr/eval.hh +++ b/lix/libexpr/eval.hh @@ -646,7 +646,7 @@ public: /** * Evaluate an expression read from the given file to normal form. */ - void evalFile(const SourcePath & path, Value & v); + Value evalFile(const SourcePath & path); void resetFileCache(); diff --git a/lix/libexpr/primops.cc b/lix/libexpr/primops.cc index 36b7ea240..635fd169a 100644 --- a/lix/libexpr/primops.cc +++ b/lix/libexpr/primops.cc @@ -225,7 +225,7 @@ static void import(EvalState & state, Value & vPath, Value * vScope, Value & v) else { if (!vScope) - state.evalFile(path, v); + v = state.evalFile(path); else { state.forceAttrs(*vScope, noPos, "while evaluating the first argument passed to builtins.scopedImport"); diff --git a/lix/nix/prefetch.cc b/lix/nix/prefetch.cc index f3a3624d4..db4f102e0 100644 --- a/lix/nix/prefetch.cc +++ b/lix/nix/prefetch.cc @@ -204,11 +204,9 @@ static int main_nix_prefetch_url(AsyncIoRoot & aio, std::string programName, Str throw UsageError("you must specify a URL"); url = args[0]; } else { - Value vRoot; - state->evalFile( - evaluator->paths.resolveExprPath( - aio.blockOn(lookupFileArg(*evaluator, args.empty() ? "." : args[0])).unwrap()), - vRoot); + Value vRoot = state->evalFile(evaluator->paths.resolveExprPath( + aio.blockOn(lookupFileArg(*evaluator, args.empty() ? "." : args[0])).unwrap() + )); Value v(findAlongAttrPath(*state, attrPath, autoArgs, vRoot).first); state->forceAttrs(v, noPos, "while evaluating the source attribute to prefetch"); diff --git a/subprojects/nix-eval-jobs/src/worker.cc b/subprojects/nix-eval-jobs/src/worker.cc index 886728547..20e75133c 100644 --- a/subprojects/nix-eval-jobs/src/worker.cc +++ b/subprojects/nix-eval-jobs/src/worker.cc @@ -52,10 +52,9 @@ static nix::Value releaseExprTopLevelValue(nix::EvalState &state, nix::CanonPath::fromCwd()); vTop = state.eval(e); } else { - state.evalFile( + vTop = state.evalFile( state.aio.blockOn(nix::lookupFileArg(state.ctx, args.releaseExpr)) - .unwrap(), - vTop); + .unwrap()); } nix::Value vRoot;