diff --git a/src/libcmd/include/nix/cmd/repl.hh b/src/libcmd/include/nix/cmd/repl.hh index b72a9b7d1d7..7d78c04c049 100644 --- a/src/libcmd/include/nix/cmd/repl.hh +++ b/src/libcmd/include/nix/cmd/repl.hh @@ -25,7 +25,7 @@ struct AbstractNixRepl * @todo this is a layer violation * * @param programName Name of the command, e.g. `nix` or `nix-env`. - * @param args aguments to the command. + * @param args arguments to the command. */ using RunNix = void(const std::string & programName, const Strings & args, const std::optional & input); @@ -37,7 +37,6 @@ struct AbstractNixRepl */ static std::unique_ptr create( const LookupPath & lookupPath, - nix::ref store, ref state, std::function getValues, RunNix * runNix = nullptr); diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 47d0127a322..6cc8eb1cff4 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -65,7 +65,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc const static int envSize = 32768; std::shared_ptr staticEnv; - Value lastLoaded; + std::optional lastLoaded; Env * env; int displ; StringSet varNames; @@ -78,7 +78,6 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc NixRepl( const LookupPath & lookupPath, - nix::ref store, ref state, std::function getValues, RunNix * runNix); @@ -133,7 +132,6 @@ std::string removeWhitespace(std::string s) NixRepl::NixRepl( const LookupPath & lookupPath, - nix::ref store, ref state, std::function getValues, RunNix * runNix) @@ -773,11 +771,19 @@ void NixRepl::initEnv() void NixRepl::showLastLoaded() { - RunPager pager; + if (!lastLoaded) + throw Error("nothing has been loaded yet"); - for (auto & i : *lastLoaded.attrs()) { - std::string_view name = state->symbols[i.name]; - logger->cout(name); + RunPager pager; + try { + for (auto & i : *lastLoaded->attrs()) { + std::string_view name = state->symbols[i.name]; + logger->cout(name); + } + } catch (SystemError & e) { + /* Ignore broken pipes when the pager gets interrupted. */ + if (!e.is(std::errc::broken_pipe)) + throw; } } @@ -900,13 +906,9 @@ void NixRepl::runNix(const std::string & program, const Strings & args, const st } std::unique_ptr AbstractNixRepl::create( - const LookupPath & lookupPath, - nix::ref store, - ref state, - std::function getValues, - RunNix * runNix) + const LookupPath & lookupPath, ref state, std::function getValues, RunNix * runNix) { - return std::make_unique(lookupPath, std::move(store), state, getValues, runNix); + return std::make_unique(lookupPath, state, getValues, runNix); } ReplExitStatus AbstractNixRepl::runSimple(ref evalState, const ValMap & extraEnv) @@ -919,7 +921,6 @@ ReplExitStatus AbstractNixRepl::runSimple(ref evalState, const ValMap // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete) auto repl = std::make_unique( lookupPath, - openStore(), evalState, getValues, /*runNix=*/nullptr); diff --git a/src/nix/repl.cc b/src/nix/repl.cc index 19f02e759c5..5450001e4e9 100644 --- a/src/nix/repl.cc +++ b/src/nix/repl.cc @@ -99,7 +99,7 @@ struct CmdRepl : RawInstallablesCommand } return values; }; - auto repl = AbstractNixRepl::create(lookupPath, openStore(), state, getValues, runNix); + auto repl = AbstractNixRepl::create(lookupPath, state, getValues, runNix); repl->autoArgs = getAutoArgs(*repl->state); repl->initEnv(); repl->mainLoop(); diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh index 7023f2b8a0d..315e2b5e3c2 100755 --- a/tests/functional/repl.sh +++ b/tests/functional/repl.sh @@ -311,6 +311,12 @@ testReplResponseNoRegex ' } ' +testReplResponseNoRegex ' +:ll +' \ +'error: nothing has been loaded yet +' + # Don't prompt for more input when getting unexpected EOF in imported files. testReplResponse " import $testDir/lang/parse-fail-eof-pos.nix