You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
$env does not apply in some situations where it should #10175
The following snippet is expected to set $env.config.table.mode = none for the scope of the do block:
# either thisdo { $env.config.table.mode = none; ls }# or this
with-env {config: {table: {mode: none}}} { ls }
In practice ls here is not working as expected, it does not use the 'none' configuration.
Replacing ls with print (ls) does print the expected results, but I don't understand why.
Replacing ls with echo $env.config.table.mode does print the expected result (none).
In addition, but not exactly related: The output of table --help for table --list does not tell you where/how you can use the
results from table --list. Especially since I just wanted to test it ad-hoc and not actually change my config.nu (The mode setting there looks pretty obvious, but I didn't even look there)
How to reproduce
Run do { $env.config.table.mode = none; ls }
See the outputted table is not shown in the expected mode/theme.
Expected behavior
I expect nu to use the set configuration inside the do block.
In my initial example using with-env, I set it using a dictionary to avoid this. But then there is the actual issue imo, that it didn't apply then, even when it was set.
Errors like this are caused by fetching the config during evaluation using engine_config.get_config() which is not correct because it doesn't take into account the latest environment updates. Instead, nu_engine::env::get_config() should be used. I fixed a similar issue in #9353.
If what I wrote above is true, then it should be an easy PR, if someone wants to pick it up, to replace engine_config.get_config() with nu_engine::env::get_config() wherever possible.
So looking at occurrences in the code, I couldn't find any of engine_config.get_config() but I see occurrences regarding engine_state.get_config(). Is it these occurrences that need changing?
Apologies if I'm misunderstanding, new to this codebase.
Okay been giving this a go and I can't find the issue with this. I think it might be something to do with the do command but as a newcomer to the project I can't be sure. Wondering if I can pass this on to someone else?
Alright, this is not as simple as it seems. Commands in nushell don't print things to stdout, like in other shells; they're just functions and all they do return is data. The data itself is only printed to stdout when it reaches the repl, so something like.
do { ls; ls; ls; () }
Won't actually print anything, as the return of the do statement is (), and the ls commands are just being unused.
This behavior of nushell manifests itself in do { $env.config.table.mode = none; ls }, because the ls command is not really printing anything, just returning the data. The table command runs after the closure finishes executing and after the do returns, so by the time the implicit table command executes, the environment variable no longer exists. This is not exactly a bug, just something very non intuitive. As a workaround, one can do something like
do { $env.config.table.mode = none; ls | table }
which will convert the ls data to a table string while the environment variable is still set, returning the expected output.
I think the best thing to do in this situation is to document this 'gotcha' in the Pipelines section of the nushell book.
Activity
fdncred commentedon Aug 31, 2023
ya, seems like this should work
We'd accept a PR to fix this.
@zhiburt Seems like you worked on something like this in the past and got it working, but I can't recall exactly what.
dupdrop commentedon Aug 31, 2023
@fdncred, in your example
$env.config.table.modeis basically treated as a single string. According to the docs,$envis not expected here.In my initial example using
with-env, I set it using a dictionary to avoid this. But then there is the actual issue imo, that it didn't apply then, even when it was set.kubouch commentedon Aug 31, 2023
Errors like this are caused by fetching the config during evaluation using
engine_config.get_config()which is not correct because it doesn't take into account the latest environment updates. Instead,nu_engine::env::get_config()should be used. I fixed a similar issue in #9353.kubouch commentedon Aug 31, 2023
If what I wrote above is true, then it should be an easy PR, if someone wants to pick it up, to replace
engine_config.get_config()withnu_engine::env::get_config()wherever possible.JordanllHarper commentedon Sep 3, 2023
I'd like to pick this up
JordanllHarper commentedon Sep 3, 2023
So looking at occurrences in the code, I couldn't find any of
engine_config.get_config()but I see occurrences regardingengine_state.get_config(). Is it these occurrences that need changing?Apologies if I'm misunderstanding, new to this codebase.
kubouch commentedon Sep 3, 2023
@JordanllHarper Cool, go ahead! Yes, I meant
engine_state.get_config().Just quickly scanning, the
tablecommand responsible for printing the data already has theget_config()fix applied, but you might be able to find the cause elsewhere, perhaps https://github.com/nushell/nushell/tree/main/crates/nu-table.JordanllHarper commentedon Sep 15, 2023
Okay been giving this a go and I can't find the issue with this. I think it might be something to do with the do command but as a newcomer to the project I can't be sure. Wondering if I can pass this on to someone else?
4 remaining items
fdncred commentedon Nov 4, 2023
It's yours @cosineblast. Have fun!
kubouch commentedon Nov 4, 2023
There was some work on it and simply replacing the
get_config()calls might incur some performance issues due to cloning the Config. See also #10483.cosineblast commentedon Nov 5, 2023
After some debugging I'm starting to suspect that the display_output hook is just not honoring any $env modifications, I'll confirm it once I get home
cosineblast commentedon Nov 5, 2023
Alright, this is not as simple as it seems. Commands in nushell don't print things to stdout, like in other shells; they're just functions and all they do return is data. The data itself is only printed to stdout when it reaches the repl, so something like.
Won't actually print anything, as the return of the do statement is
(), and thelscommands are just being unused.This behavior of nushell manifests itself in
do { $env.config.table.mode = none; ls }, because thelscommand is not really printing anything, just returning the data. Thetablecommand runs after the closure finishes executing and after the do returns, so by the time the implicittablecommand executes, the environment variable no longer exists. This is not exactly a bug, just something very non intuitive. As a workaround, one can do something likewhich will convert the
lsdata to a table string while the environment variable is still set, returning the expected output.I think the best thing to do in this situation is to document this 'gotcha' in the
Pipelinessection of the nushell book.Would a PR documenting this note be welcome?
amtoine commentedon Nov 5, 2023
@cosineblast
wow, that's very interesting 😮
i could fix it as well with
| table👍i guess the best would be to fix this issue, for sure, but at least having it mentionned somewhere might be great as a quick fix 🤔
cosineblast commentedon Nov 8, 2023
Also, I believe this issue should not be labelled as a 'good first issue' as it involves some semantics of the language (return vs stdout).
kubouch commentedon Nov 8, 2023
True, it has some non-trivial implications that got revealed over time.
amtoine commentedon Nov 8, 2023
the question is still have is: should the following be considered a bug?
kubouch commentedon Nov 8, 2023
It feels counter-intuitive, but IMO it shouldn't. You're sending a value from the
doblock, but the config changes are applied only inside the block.amtoine commentedon Nov 8, 2023
now that i understand what's going on, yeah i can live with that being a feature 😋
the one with
sounds like it should work though 🤔
kubouch commentedon Nov 8, 2023
It shouldn't because you're again trapping the env changes inside the block and returning a value from it, it's the same case as
do.amtoine commentedon Nov 8, 2023
ooh yeah you're right 😮
then we can close this issue, right? 🤔
kubouch commentedon Nov 8, 2023
OK, let's close this. The get_config() or changing the config in general are other topics.