arrterian / nix-env-selector

Allows switch environment for Visual Studio Code using Nix Package Manager.
MIT License
221 stars 28 forks source link

[BUG] Extension don't add package to PATH variable, when some active tabs need any package from shell.nix #66

Open Wittano opened 2 years ago

Wittano commented 2 years ago

Weird situation. First time run extension, I selected my configured shell.nix file, extension loaded packages, everything works. Second time, extension didn't load my packages and LSP throws exception, because package didn't add to PATH. But when I close every tabs, that use any package from shell.nix, extension will load packages, add to PATH and everything back to normal.

I found this bug for GoLang project. You can generate simple golang project and add to your shell.nix LSP for golang.

AleksanderGondek commented 2 years ago

Thank you for taking time to report this, I thought I had been going crazy.

rectcircle commented 1 year ago

I also encountered the same problem.

I read the source code of this extension. I think this is an underlying flaw of this extension, related to the VSCode extension activation order.

In order to start fast enough, VSCode does not provide an API to define the order of extension activation.

Therefore, in some cases, other extensions will start before this extension completes env/set-current-env.

https://github.com/arrterian/nix-env-selector/blob/62f01e632dd1e5ba4ac72d23ccf18b30175dabe0/src/main/main.cljs#L21

Taking the Go extension as an example, if I don't open the go source file, it will work fine, the extension host output is:

2023-03-04 17:50:41.355 [info] ExtensionService#_doActivateExtension arrterian.nix-env-selector, startup: false, activationEvent: '*'
2023-03-04 17:50:41.355 [info] ExtensionService#_doActivateExtension mutantdino.resourcemonitor, startup: false, activationEvent: '*'
2023-03-04 17:50:41.356 [info] ExtensionService#_doActivateExtension vscode.extension-editing, startup: false, activationEvent: 'onLanguage:markdown'
2023-03-04 17:50:41.356 [info] ExtensionService#_doActivateExtension vscode.markdown-language-features, startup: false, activationEvent: 'onLanguage:markdown'
2023-03-04 17:50:41.356 [info] ExtensionService#_doActivateExtension DavidAnson.vscode-markdownlint, startup: false, activationEvent: 'onLanguage:markdown'
2023-03-04 17:50:42.638 [info] ExtensionService#_doActivateExtension vscode.git, startup: false, activationEvent: '*'
2023-03-04 17:50:42.638 [info] ExtensionService#_doActivateExtension vscode.github, startup: false, activationEvent: '*'
2023-03-04 17:50:42.816 [info] ExtensionService#_doActivateExtension vscode.markdown-math, startup: false, activationEvent: 'api', root cause: vscode.markdown-language-features
2023-03-04 17:50:42.972 [info] ExtensionService#_doActivateExtension golang.go, startup: true, activationEvent: 'workspaceContains:*.go,*/*.go,*/*/*.go'

If I open the Go source file, the Go extension will report an error, and the extension host output is:

2023-03-04 17:49:58.989 [info] ExtensionService#_doActivateExtension arrterian.nix-env-selector, startup: false, activationEvent: '*'
2023-03-04 17:49:58.989 [info] ExtensionService#_doActivateExtension mutantdino.resourcemonitor, startup: false, activationEvent: '*'
2023-03-04 17:49:58.989 [info] ExtensionService#_doActivateExtension golang.go, startup: false, activationEvent: 'onLanguage:go'

Therefore, I think the implementation of this extension can only completely solve this problem by modifying the user's shell profile to inject environment variables such as PATH. For example ~/.bashrc

if [ $VSCODE_RESOLVING_ENVIRONMENT ]; then
  # source ....
fi
rectcircle commented 1 year ago

https://github.com/microsoft/vscode/issues/152806

opeik commented 1 year ago

I don't think anything can be done without modifying VSCode. Trying to fix it on the extension side ends up being hacky and buggy.