Skip to content

$env does not apply in some situations where it should #10175

Closed as not planned
Closed as not planned

Description

@dupdrop

Describe the bug

The following snippet is expected to set $env.config.table.mode = none for the scope of the do block:

# either this
do { $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

  1. Run do { $env.config.table.mode = none; ls }
  2. 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.

Screenshots

No response

Configuration

key value
version 0.84.0
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.71.0 (8ede3aae2 2023-07-12)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.71.0 (cfd3bbd8f 2023-06-08)
build_time 2023-08-26 17:04:51 +03:00
build_rust_channel release
allocator standard
features default, sqlite, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    status:needs-triage
    on Aug 31, 2023
  2. fdncred commented on Aug 31, 2023

    @fdncred
    Contributor

    ya, seems like this should work

    with-env [ $env.config.table.mode none ] { print $env.config.table.mode }

    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.

  3. added
    category:bug
    A:table-renderer
    and removed
    status:needs-triage
    on Aug 31, 2023
  4. dupdrop commented on Aug 31, 2023

    @dupdrop
    Author

    @fdncred, in your example $env.config.table.mode is basically treated as a single string. According to the docs, $env is not expected here.

    > $env.config.table.mode = rounded
    > with-env [ config.table.mode none ] { print $env.config.table.mode }
    rounded
    > with-env [ config.table.mode none ] { print $env."config.table.mode" }  # <-- notice the quotes
    none

    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.

  5. kubouch commented on Aug 31, 2023

    @kubouch
    Contributor

    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.

  6. added
    good first issue
    A:configuration
    on Aug 31, 2023
  7. kubouch commented on Aug 31, 2023

    @kubouch
    Contributor

    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.

  8. JordanllHarper commented on Sep 3, 2023

    @JordanllHarper

    I'd like to pick this up

  9. JordanllHarper commented on Sep 3, 2023

    @JordanllHarper

    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.

  10. kubouch commented on Sep 3, 2023

    @kubouch
    Contributor

    @JordanllHarper Cool, go ahead! Yes, I meant engine_state.get_config().

    Just quickly scanning, the table command responsible for printing the data already has the get_config() fix applied, but you might be able to find the cause elsewhere, perhaps https://github.com/nushell/nushell/tree/main/crates/nu-table.

  11. JordanllHarper commented on Sep 15, 2023

    @JordanllHarper

    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?

  12. 4 remaining items

  13. fdncred commented on Nov 4, 2023

    @fdncred
    Contributor

    It's yours @cosineblast. Have fun!

  14. kubouch commented on Nov 4, 2023

    @kubouch
    Contributor

    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.

  15. cosineblast commented on Nov 5, 2023

    @cosineblast
    Contributor

    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

  16. cosineblast commented on Nov 5, 2023

    @cosineblast
    Contributor

    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.

    Would a PR documenting this note be welcome?

  17. amtoine commented on Nov 5, 2023

    @amtoine
    Member

    @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 🤔

  18. cosineblast commented on Nov 8, 2023

    @cosineblast
    Contributor

    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).

  19. removed
    good first issue
    on Nov 8, 2023
  20. kubouch commented on Nov 8, 2023

    @kubouch
    Contributor

    True, it has some non-trivial implications that got revealed over time.

  21. amtoine commented on Nov 8, 2023

    @amtoine
    Member

    the question is still have is: should the following be considered a bug?

    > do { $env.config.table.mode = none; ls }
    #┬──name───┬type┬─size──┬─────modified──────
    0│LICENSE  │file│35.1 KB│2023-10-31 17:14:51
    1│README.md│file│11.8 KB│2023-11-06 12:26:58
    2│documents│dir │ 4.1 KB│2023-10-31 17:09:28
    3│downloads│dir │ 4.1 KB│2023-10-31 18:26:00
    4│media    │dir │ 4.1 KB│2023-11-06 12:24:48
    ─┴─────────┴────┴───────┴───────────────────
  22. kubouch commented on Nov 8, 2023

    @kubouch
    Contributor

    It feels counter-intuitive, but IMO it shouldn't. You're sending a value from the do block, but the config changes are applied only inside the block.

  23. amtoine commented on Nov 8, 2023

    @amtoine
    Member

    now that i understand what's going on, yeah i can live with that being a feature 😋

    the one with

    with-env {config: {table: {mode: none}}} { ls }

    sounds like it should work though 🤔

  24. kubouch commented on Nov 8, 2023

    @kubouch
    Contributor

    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.

  25. amtoine commented on Nov 8, 2023

    @amtoine
    Member

    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.

    ooh yeah you're right 😮

    then we can close this issue, right? 🤔

  26. kubouch commented on Nov 8, 2023

    @kubouch
    Contributor

    OK, let's close this. The get_config() or changing the config in general are other topics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A:configurationA:table-renderercategory:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions