How is cwd determined? #4869
Replies: 7 comments 9 replies
-
The CWD comes from the process attached to the tty device that has the
highest PID number. Your LSP server process should be calling setsid()
so that it is not associated with the original tty. Indeed neovim should
in general be calling setsid() after forking when execing processes it
does not expect to output to/input from the tty.
|
Beta Was this translation helpful? Give feedback.
-
I found a workaround by controlling kitty with this command in neovim
Following configuration is needed in kitty.conf
|
Beta Was this translation helpful? Give feedback.
-
stdin/stdout have nothing to do with the tty. A language server's
stdin/stdout will be connected to a pipe from which the process that
launches it reads/writes data to control it. They are not connected to
the tty, otherwise you would see output from the lsp in your terminal.
When you launch a daemon (background) process in unix you should detach
it from its environment, including the controlling tty. An editor
running in a terminal needs exclusive control over the tty, otherwise
other processes can change tty state behind its back, leading to
corruption. Therefore, a well designed editor should call setsid before
execing binaries that it does not expect to write to the *TTY*.
Read man 7 credentials and man 2 setsid and google how to write a unix
daemon process for more information.
kitty used to use the heuristic that the *first* process associated with
the tty was used to determine the process to take the cwd from. That was
not well defined, thought it worked around this bug in neovim. Now it
has switched to using the processes with the highest PID, which is more
likely to be the actual foreground process in situations such as running
a script like:
```
#!/bin/bash
cd /something
vim
```
Here both bash and vim will be associated with the tty, but the user
will expect the cwd to come from vim not bash. Using the highest PID
achieves that.
|
Beta Was this translation helpful? Give feedback.
-
See feea699 So you can use launch --cwd=oldest |
Beta Was this translation helpful? Give feedback.
-
For Homebrew users who want to use this now, brew tap homebrew/cask-versions
brew uninstall kitty
brew install kitty-nightly |
Beta Was this translation helpful? Give feedback.
-
On Sat, May 07, 2022 at 06:13:30PM -0700, Chris Lasher wrote:
I filed an issue with Neovim for this:
neovim/neovim#18475
Thank you again for your help explaining the matter. Hopefully I paraphrased correctly.
Good, it should be a straightforward change in neovim.
|
Beta Was this translation helpful? Give feedback.
-
neovim itself should not run with setsid() it should run background processes it
starts with setsid(). And when it is exiting, it should clean up
background processes that it does not want to outlive it. Language
servers have no business being connected to a terminal, they are not
displaying text to the user.
|
Beta Was this translation helpful? Give feedback.
-
I'll open an issue if it seems like it's a problem with kitty, but there are a lot of moving pieces with the problem I'm having, so I just wanted to get an answer to this question first.
Basically, when I have Neovim running and there is a language server running via the built-in LSP client, opening a new window in kitty results in the new shell starting in the directory where the LSP server is located.
pwd
in Neovim still shows the correct working directory, as does the tab title when the Neovim window is selected.I'm kinda baffled, so I figured finding out how cwd is determined by kitty is the place to start to track down what the heck is going on.
Beta Was this translation helpful? Give feedback.
All reactions