pucelle / vscode-run-on-save

Run configured shell commands when a file is saved in vscode, and output configured messages on status bar.
https://marketplace.visualstudio.com/items?itemName=pucelle.run-on-save
MIT License
51 stars 14 forks source link

Backend shell + macos homebrew not working as expected #19

Closed rodber closed 3 years ago

rodber commented 3 years ago

I've spotted an issue affecting the backend shell when using homebrew. The problem is that the backed shell doesn't behave as the integrated terminal.

This happens when using zsh from homebrew and launching VS Code before homebrew wires the shell. This is the trickiest part of this bug, if you wait ~30s after login in this will work as expected and you won't be able to reproduce the bug.

I doubt that this error is caused by the extension, in any case here's my findings.

POC

Configuration:

            "command": "echo $PATH && which zsh",
            "runIn": "backend",

Trigger a save, the results are:

Running "echo $PATH && which zsh"
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
/bin/zsh

If you switch to this configuration:

            "command": "echo $PATH && which zsh",
            "runIn": "terminal",

You will get this:

❯ echo $PATH && which zsh
/opt/homebrew/bin/php:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
/opt/homebrew/bin/zsh

Workarounds

Homebrew

I related this to homebrew because If you login, then right away open a terminal and run which some_binary_provided_by_homebrew it will take some time to respond. If you launch VS Code while homebrew is being loaded it will cause "runIn": "backend" to don't behave as expected.

pucelle commented 3 years ago

I just guess, this issue may be caused by the plugin making the terminal CWD (current working directory) to be current vscode working directory.

I accept this feature from one of my issues.

pucelle commented 3 years ago

Already released v1.4.4, try it.

rodber commented 3 years ago

Using workingDirectoryAsCWD true

            "workingDirectoryAsCWD": true,
            "command": "echo ${cwd}",

Result (I get the same result using true or false):

Running "echo /"
/

Now the only way to run my command is to:

"command": "cd ${workspaceFolder} && vendor/bin/ecs check ${file} --fix",

Otherwise I get:

/bin/sh: vendor/bin/ecs: No such file or directory

And by the way, the shell is still not ready and I still need to restart.

pucelle commented 3 years ago

It seems your problem doesn't have any relationship with the CWD... I changed the default value of workingDirectoryAsCWD to true now. Still need some research on this problem.

sfeldkamp commented 2 years ago

@pucelle

I have some additional information about this problem. The issue for me was that my PATH didn't include executables installed by by Homebrew. I think this is because when node.child_process uses a non-interactive shell. That means that .zprofile and .zshrc are not processed.

I was able to work around this by using the full path to the shell command I want to run.

"command": "/usr/local/bin/{command} --args ..."

https://apple.stackexchange.com/questions/388622/zsh-zprofile-zshrc-zlogin-what-goes-where

https://www.geeksforgeeks.org/shell-scripting-interactive-and-non-interactive-shell/#:~:text=It%20is%20highly%20recommended%20to%20use%20the%20full%20path%20for%20a%20command%20in%20non%2Dinteractive%20shells.