Skip to content

Nushell doesn't play nice with "current directory" #5050

Closed

Description

@rawsp33d

Describe the bug

I found a problem that is hard to explain, so please bear with me. It seems like nushell doesn't notify other processes about its current directory, or doesn't start in the proper directory, or something.
If I'm in tmux and I open a new split in the current directory, it actually opens the split in the directory that the initial tmux session was created or joined from.
If I just make a split with my terminal, the same thing happens. I have tried in both wezterm and kitty in an effort to narrow it down and make sure this is actually a nushell problem.

How to reproduce

Here is a hopefully simple example with minimal dependencies.

Working:

  1. Configure tmux default-shell to be /usr/bin/bash (even if default-command is nushell)
  2. Create a new tmux session
  3. cd some-folder
  4. Run tmux split-window -v -c "#{pane_current_path}" or tmux new-window -a -c "#{pane_current_path}" or something equivalent
  5. Note that the current folder for the new pane is also some-folder as expected

Not working:

  1. Configure tmux default-shell to be /usr/bin/nu
  2. Create a new tmux session
  3. cd some-folder
  4. Run tmux split-window -v -c "#{pane_current_path}" or tmux new-window -a -c "#{pane_current_path}" or something equivalent
  5. Note that the current folder for the new pane is the original folder, not some-folder

In tmux specifically I just found that I can work around this issue by having default-command be nushell and default-shell be bash, but ironically I'm trying to phase out my usage of tmux and have not yet found a workaround for my terminal, in which the same problem exists.
Despite this, I used tmux for my example because it is more common, terminal-agnostic, and lets me test different shells without changing my login shell.

Expected behavior

I expect terminals and terminal multiplexers to open nushell at a specific path when using common "open new split/pane/tab/window/whatever in current directory" functionality.

Screenshots

No response

Configuration

key value
version 0.60.0
branch makepkg
short_commit 74af31a
commit_hash 74af31a
commit_date 2022-03-22 19:07:11 +00:00
build_os linux-x86_64
rust_version rustc 1.59.0 (Arch Linux rust 1:1.59.0-1)
cargo_version cargo 1.59.0
pkg_version 0.60.0
build_time 2022-03-23 00:39:08 +00:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. kubouch commented on Mar 31, 2022

    @kubouch
    Contributor

    The only time Nushell modifies external environment is when running an external. Otherwise, Nushell's environment variables are just normal values, like normal variables. Current directory is stored just as a plain PWD environment variable. So it might make sense that tmux does not respect cd because all of this information is internal within the Nushell process.

    What we could do is during merge_delta() (when you hit enter in the REPL), we could also set the current directory with std::env::set_current_dir().

  2. added
    category:bug
    on Mar 31, 2022
  3. rawsp33d commented on Mar 31, 2022

    @rawsp33d
    Author

    Another facet of what you said that I found is that nushell does not seem to project what its currently-running command is.

    So for example if you are in tmux and run nvim, tmux still thinks the running process is nu. Same with a terminal, I just tested having wezterm tell me the foreground process name and even when I'm running vim the process is /usr/bin/nu. If I try the same tests in bash or zsh they both say /usr/bin/nvim is the running process.

  4. ryanpcmcquen commented on Apr 1, 2022

    @ryanpcmcquen

    Maybe this is related to my issue?
    #4967

  5. fdncred commented on Apr 1, 2022

    @fdncred
    Contributor

    it appears that naming processes is a linux thing and not so much a Windows thing. Not sure about Mac.
    https://stackoverflow.com/questions/6082189/change-process-name-in-linux - although this link points to a "multi-platform" implementation here.

    But if you just want to set the window title, you can do what I do here https://github.com/nushell/nu_scripts/blob/7f8d633ac35064e7833617b4492b26481f589224/prompt/oh-my.nu#L512-L528

  6. rawsp33d commented on Apr 5, 2022

    @rawsp33d
    Author

    Well I definitely don't know exactly what I'm doing, but I did try setting the name with prctl in two places.
    I tried both the capctl library as well as the much older prctl library.

    # crates/nu-protocol/src/engine/engine_state.rs : line 277
    # in `pub fn merge_delta`
            // FIXME: permanent state changes like this hopefully in time can be removed
            // and be replaced by just passing the cwd in where needed
            std::env::set_current_dir(cwd)?;
    
            capctl::prctl::set_name("foo");
    # crates/nu-cli/src/repl.rs : line 348
    # in `pub fn evaluate_repl`
                    // FIXME: permanent state changes like this hopefully in time can be removed
                    // and be replaced by just passing the cwd in where needed
                    capctl::prctl::set_name("bar");
                    if let Some(cwd) = stack.get_env_var(engine_state, "PWD") {
                        let path = cwd.as_string()?;
                        let _ = std::env::set_current_dir(path);
                        engine_state.env_vars.insert("PWD".into(), cwd);
                    }

    It's hard for me to tell whether I'm not using the libraries correctly, they do not work, I'm trying all of this in the wrong places, or something else entirely.

  7. rawsp33d commented on Apr 5, 2022

    @rawsp33d
    Author

    I don't currently understand the "longer" hack that chromium does nor how to implement it in nushell.
    I'm just trying to do some exploratory R&D because I need nushell to behave like other shells in this way for my workflow.

  8. kubouch commented on Apr 5, 2022

    @kubouch
    Contributor

    Interesting, I would guess it would work by setting it in the merge_delta() (there shouldn't be a need to put it in evaluate_repl() as well). Seems like setting cwd in merge_delta() is already there so there must be something else we can do, but I have no idea.

  9. rawsp33d commented on Apr 6, 2022

    @rawsp33d
    Author

    Okay so I misdiagnosed the second issue as potentially being related to the first. The main problem conveyed in my OP seems to be the same problem as #2649 (closed).

    I can open an issue for the second issue I mentioned in a comment above in the future, ideally after I figure out more about it.

  10. danjenson commented on May 14, 2022

    @danjenson

    I'm finding this problem too, even using tmux attach-session -c <dir> -t <sesh> it is reverting to the original tmux session's CWD; hard to say whether this is a nushell or tmux problem, or a combination of the two. Here is a relevant tmux issue: tmux/tmux#3186

  11. rawsp33d commented on May 15, 2022

    @rawsp33d
    Author

    Yeah I found it only works in tmux if you set your tmux shell to anything other than nu. By which I mean the internal shell setting. Then even though you're using nu for your shell, tmux uses bash behind the scenes. Unfortunately doesn't help my situation because I moved off tmux, but hopefully helps you.

  12. unional commented on Jun 4, 2022

    @unional
    Contributor

    I think this is related to microsoft/vscode#150153

    One problem is $env.PWD is not commonly used by other processes in Windows environment. So it causes some weird cases as it is not automatically updated.

  13. WindSoilder commented on Jun 11, 2022

    @WindSoilder
    Contributor

    @unional Can you please try the latest main build solve your issue?

    I can't reproduce it in vscode.

  14. unional commented on Jun 11, 2022

    @unional
    Contributor

    Yes it is working. Thx!

  15. added this to the v0.64.0 milestone on Jun 11, 2022
  16. rawsp33d commented on Jun 14, 2022

    @rawsp33d
    Author

    As the issue creator I just wanted to weigh in and confirm: the changes work great! Thank you to everybody involved with this.

  17. Southclaws commented on Jul 10, 2023

    @Southclaws
    Contributor

    Old ticket I know but I'm running into this on v0.77.1

    It might be related to the documentation here https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory the post-prompt echo might fix it but I can't find any documentation on what exactly to do for NuShell + Windows Terminal.

    Perhaps this is just a matter of documentation? Would love to get this working for split panes!

  18. fdncred commented on Jul 10, 2023

    @fdncred
    Contributor

    This works with how the link shows already (right click -> duplicate tab), but it doesn't work with split panes. I'm not sure why.

    Update: I take it back. If nushell is launched as the shell versus a subshell of pwsh or cmd or whatever, and you right click the tab and click split pane, the new nushell will start in the same folder as your left pane.

  19. Southclaws commented on Jul 10, 2023

    @Southclaws
    Contributor

    I'm not having any luck with either (both updated to latest version just now)

    Recording.2023-07-10.152008.mp4

    And the starting command for the actual shell is nu itself, not a different shell running nu as a child process.

    image

    Perhaps this is specific to Windows Terminal rather than a linuxy escape-sequence type thing though, is it worth opening a separate issue?

  20. fdncred commented on Jul 10, 2023

    @fdncred
    Contributor

    I think I may have figured it out. I'm running Windows Terminal Preview v1.18.1462.0. I bet you're running the normal Windows Terminal. I'm guessing OSC7 that sets this is not in Windows Terminal and only the preview maybe?

    dup-win

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    category:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions