Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag to start the REPL on evaluation errors #3901

Merged
merged 2 commits into from May 26, 2022
Merged

Add a flag to start the REPL on evaluation errors #3901

merged 2 commits into from May 26, 2022

Conversation

edolstra
Copy link
Member

@edolstra edolstra commented Aug 5, 2020

This allows interactively inspecting the state of the evaluator at the point of failure.

Example:

$ nix eval path:///home/eelco/Dev/nix/flake2#modules.hello-closure._final --start-repl-on-eval-errors
error: --- TypeError -------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
at: (20:53) in file: /nix/store/4264z41dxfdiqr95svmpnxxxwhfplhy0-source/flake.nix

    19|
    20|           _final = builtins.foldl' (xs: mod: xs // (mod._module.config { config = _final; })) _defaults _allModules;
      |                                                     ^
    21|         };

attempt to call something which is not a function but a set

Starting REPL to allow you to inspect the current state of the evaluator.

The following extra variables are in scope: arg, fun

Welcome to Nix version 2.4. Type :? for help.

nix-repl> fun
error: --- EvalError -------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
at: (150:28) in file: /nix/store/4264z41dxfdiqr95svmpnxxxwhfplhy0-source/flake.nix

   149|
   150|           tarballClosure = (module {
      |                            ^
   151|             extends = [ self.modules.derivation ];

attribute 'derivation' missing

nix-repl> :t fun
a set

nix-repl> builtins.attrNames fun
[ "tarballClosure" ]

nix-repl>

TODO:

  • Add the lexical environment at the failure site to the scope of the repl.
  • Add many more places where the repl can be invoked.
  • Add a breakpoint builtin?

This allows interactively inspecting the state of the evaluator at the
point of failure.

Example:

  $ nix eval path:///home/eelco/Dev/nix/flake2#modules.hello-closure._final --start-repl-on-eval-errors
  error: --- TypeError -------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
  at: (20:53) in file: /nix/store/4264z41dxfdiqr95svmpnxxxwhfplhy0-source/flake.nix

      19|
      20|           _final = builtins.foldl' (xs: mod: xs // (mod._module.config { config = _final; })) _defaults _allModules;
        |                                                     ^
      21|         };

  attempt to call something which is not a function but a set

  Starting REPL to allow you to inspect the current state of the evaluator.

  The following extra variables are in scope: arg, fun

  Welcome to Nix version 2.4. Type :? for help.

  nix-repl> fun
  error: --- EvalError -------------------------------------------------------------------------------------------------------------------------------------------------------------------- nix
  at: (150:28) in file: /nix/store/4264z41dxfdiqr95svmpnxxxwhfplhy0-source/flake.nix

     149|
     150|           tarballClosure = (module {
        |                            ^
     151|             extends = [ self.modules.derivation ];

  attribute 'derivation' missing

  nix-repl> :t fun
  a set

  nix-repl> builtins.attrNames fun
  [ "tarballClosure" ]

  nix-repl>
@edolstra edolstra marked this pull request as draft August 5, 2020 19:47
@lf-
Copy link
Member

lf- commented Aug 15, 2020

It's a bit long. Maybe something like?

         .longName = "repl-on-error",

--debugger?

@domenkozar
Copy link
Member

domenkozar commented Feb 1, 2021

This would be the easiest fix for #963

src/nix/command.cc Outdated Show resolved Hide resolved
@domenkozar
Copy link
Member

It would be useful to show for such errors at the end To interacitvely inspect the scope pass --debugger.

Something like TypeErrorDebugger that also takes fun & args.

Co-authored-by: Domen Kožar <domen@dev.si>
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-extract-signal-from-almost-useless-error-message/11733/13

@lf-
Copy link
Member

lf- commented Jun 21, 2021

What's the status on this PR? I'd really like to be able to use this feature.

@domenkozar
Copy link
Member

cc @bburdette

@bburdette
Copy link
Contributor

@lf- I have been hacking on this problem a bit. i have a branch that builds and kicks you into the repl on most eval exceptions, and doesn't appear to significantly impact stack size.

I'm still grappling with how best to get an appropriate set of symbols in the repl though. Usually its easy enough to access an Env at exception time, but Envs contain only values, not symbols, and those values are stored in segfault-terminated arrays. Envs are mainly for looking up symbol values where the location of the value within the env is known already, not introspecting to discover what symbols are available.

My current thinking is maybe adding a value count to the Env would be inexpensive and allow iterating over the values to find Attr sets to add to the repl namespace.

@edolstra edolstra mentioned this pull request Oct 26, 2021
@stale
Copy link

stale bot commented Jan 3, 2022

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jan 3, 2022
@jakubgs
Copy link

jakubgs commented Jan 4, 2022

This is really cool and could be extremely useful.

@stale stale bot removed the stale label Jan 4, 2022
@edolstra edolstra merged commit b466d51 into master May 26, 2022
@edolstra edolstra deleted the debugger branch May 26, 2022 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants