Open jdx opened 8 months ago
It's unclear to me if preexec actually runs in non-interactive sessions or not, I'll need to experiment. If not, chpwd could work for zsh.
Unfortunately most people use bash for scripts so I'm not sure what solution could be used there.
Possible the biggest benefit here would be not being able to run "scripts" but just for zshrc to be able to use tools. Of course zshrc is a "script" but it's a special kind since it prepares the interactive session and I think users would expect it to work.
I ran into this tonight, there is a slight difference to the way zsh and bash hooks seem to work currently with rtx.
2023.12.40 linux-x64 (460e547 2023-12-28)
zsh has much better support for chpwd, but bash is clunky.
Here's a good example when using bash:
Let's say we have a project at ~/foo/bar
:
.rtx.toml
[tools]
python = {version='3.12', virtualenv='.venv'}
export RTX_DEBUG=1
export RTX_LOG_LEVEL=trace
In zsh (5.9 for me, but should work with whatever), this works correctly:
.venv
etc is created.cd ~/foo/bar
bar
directory, then echo "hello".In theory, this should first setup the virtualenv then echo hello
.
cd ~/foo && rm -rf bar/.venv && cd bar && echo "hello"
Output (correct):
[DEBUG] ARGS: rtx hook-env -s zsh
[DEBUG] Config {
Config Files: [
"~/.config/rtx/config.toml",
],
Installed Plugins: [
"poetry",
],
}
[DEBUG] Toolset:
[DEBUG] ARGS: rtx hook-env -s zsh
[DEBUG] Config {
Config Files: [
"~/foo/bar/.rtx.toml",
"~/.config/rtx/config.toml",
],
Installed Plugins: [
"poetry",
],
}
[DEBUG] Toolset: python@3.11
[INFO] rtx setting up virtualenv at: /home/josh/foo/bar/.venv
[DEBUG] $ ~/.local/share/rtx/installs/python/3.11/bin/python -m venv /home/josh/foo/bar/.venv
hello
[DEBUG] ARGS: rtx hook-env -s zsh
[DEBUG] Config {
Config Files: [
"~/foo/bar/.rtx.toml",
"~/.config/rtx/config.toml",
],
Installed Plugins: [
"poetry",
],
}
hello
[DEBUG] ARGS: rtx hook-env -s bash
[DEBUG] Config {
Config Files: [
"~/foo/bar/.rtx.toml",
"~/.config/rtx/config.toml",
],
Installed Plugins: [
"poetry",
],
}
[DEBUG] Toolset: python@3.11
[INFO] rtx setting up virtualenv at: /home/josh/foo/bar/.venv
[DEBUG] $ ~/.local/share/rtx/installs/python/3.11/bin/python -m venv /home/josh/foo/bar/.venv
first, thanks for the writeup, it's helpful to get these kind of user stories. I wonder if chpwd only gets fired on a new prompt display? I would expect that line to work like you did. I'll have to do some experimentation here
https://github.com/jdx/rtx/blob/main/src/shell/zsh.rs#L49-L56
I'll have a play with it, Bash isn't my primary shell but agreed, looks like it only fires on prompt as bash doesn't have that hook.
IMHO, it's outside of the scope of rtx to fix if it's a bash issue, the current way is fine for most usecases.
I'll have more of a dig though.
It might be possible to use
rtx activate
in scripts if I used preexec instead of precmd in zsh. It looks like this package provides preexec for bash too: https://github.com/rcaloras/bash-preexecI think I might be able to get rid of the "not_found" handler with this too. rtx could perform the bin check before the command is executed.
Fish probably has a similar hook too