lukechilds / zsh-nvm

Zsh plugin for installing, updating and loading nvm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding custom commands that trigger lazy loading nvm/node

joerideg opened this issue · comments

Hi,
First of all, thanks for your work on this plugin, its awesome.

I have the situation where my vim setup includes a plugin (coc and subplugins) that requires node to be available. Now if I enable NVM_LAZY_LOAD and start vim it would complain that node is not available. If I run 'node' before I start vim, node is of course available and there is no problem.

Would it be possible to add an option to configure additional commands that trigger the lazy load function? Maybe adding them to the global_binaries?

That sounds like it might work, can you try it?

My zsh script knowledge is limited but I can certainly learn & try :) I will see if I can provide a MR somewhere soon

Sorry about that closing, accidentally hit ctrl+enter

Hey guys,
I'm having trouble getting this variable to work. I am having the issue with nvim (coc - mentioned above with vim) and VSCode and IntelliJ IDEs with the SonarLint plugin.

nvim is not that big of a deal, but SonarLint is. I'd really like to keep lazyloading NVM and also have SonarLint working.

This is a snippet from my ~/.zshrc file

export NVM_LAZY_LOAD=true
export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim', 'nvim', '/snap/bin/nvim', 'code', '/snap/bin/code', 'webstorm', 'phpstorm')

Upon starting VScode and opening say a .js file I see this message:

[Info  - 19:35:39.991] Using storage for server '<default>' (last update 8/16/20, 7:13 PM)
[Info  - 19:35:40.539] Analyzing file 'file:///home/gnwork/Projects/paperchase/vsf/src/themes/paperchase/mixins/SearchResult.ts'...
[Info  - 19:35:40.672] Index files
[Info  - 19:35:40.678] 1 file indexed
[Info  - 19:35:42.703] Using TypeScript at: '/snap/code/40/usr/share/code/resources/app/extensions/node_modules'
[Error - 19:35:42.709] Error when running: 'node -v'. Is Node.js available during analysis?
[Error - 19:35:42.710] org.sonarsource.nodejs.NodeCommandException: Error when running: 'node -v'. Is Node.js available during analysis?
	at org.sonarsource.nodejs.NodeCommand.start(NodeCommand.java:83)
	at org.sonarsource.nodejs.NodeCommandBuilderImpl.getVersion(NodeCommandBuilderImpl.java:196)
	at org.sonarsource.nodejs.NodeCommandBuilderImpl.checkNodeCompatibility(NodeCommandBuilderImpl.java:169)
	at org.sonarsource.nodejs.NodeCommandBuilderImpl.build(NodeCommandBuilderImpl.java:144)
	at org.sonar.plugins.javascript.eslint.EslintBridgeServerImpl.initNodeCommand(EslintBridgeServerImpl.java:148)
	at org.sonar.plugins.javascript.eslint.EslintBridgeServerImpl.startServer(EslintBridgeServerImpl.java:106)
	at org.sonar.plugins.javascript.eslint.EslintBridgeServerImpl.startServerLazily(EslintBridgeServerImpl.java:169)
	at org.sonar.plugins.javascript.eslint.AbstractEslintSensor.execute(AbstractEslintSensor.java:106)
	at org.sonar.plugins.javascript.eslint.JavaScriptEslintBasedSensor.execute(JavaScriptEslintBasedSensor.java:49)
	at org.sonarsource.sonarlint.core.analyzer.sensor.SensorsExecutor.executeSensor(SensorsExecutor.java:80)
	at org.sonarsource.sonarlint.core.analyzer.sensor.SensorsExecutor.execute(SensorsExecutor.java:71)
	at org.sonarsource.sonarlint.core.container.analysis.AnalysisContainer.doAfterStart(AnalysisContainer.java:134)
	at org.sonarsource.sonarlint.core.container.ComponentContainer.startComponents(ComponentContainer.java:126)
	at org.sonarsource.sonarlint.core.container.ComponentContainer.execute(ComponentContainer.java:111)
	at org.sonarsource.sonarlint.core.container.storage.StorageAnalyzer.analyze(StorageAnalyzer.java:75)
	at org.sonarsource.sonarlint.core.container.storage.StorageContainerHandler.analyze(StorageContainerHandler.java:84)
	at org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl.lambda$analyze$0(ConnectedSonarLintEngineImpl.java:152)
	at org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl.withReadLock(ConnectedSonarLintEngineImpl.java:349)
	at org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl.withReadLock(ConnectedSonarLintEngineImpl.java:339)
	at org.sonarsource.sonarlint.core.ConnectedSonarLintEngineImpl.analyze(ConnectedSonarLintEngineImpl.java:149)
	at org.sonarsource.sonarlint.ls.AnalysisManager.lambda$analyzeConnected$7(AnalysisManager.java:337)
	at org.sonarsource.sonarlint.ls.AnalysisManager.analyzeWithTiming(AnalysisManager.java:353)
	at org.sonarsource.sonarlint.ls.AnalysisManager.analyzeConnected(AnalysisManager.java:337)
	at org.sonarsource.sonarlint.ls.AnalysisManager.analyze(AnalysisManager.java:243)
	at org.sonarsource.sonarlint.ls.AnalysisManager.lambda$analyzeAsync$0(AnalysisManager.java:205)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: Cannot run program "node": error=2, No such file or directory
	at java.base/java.lang.ProcessBuilder.start(Unknown Source)
	at java.base/java.lang.ProcessBuilder.start(Unknown Source)
	at org.sonarsource.nodejs.NodeCommand$ProcessWrapperImpl.start(NodeCommand.java:175)
	at org.sonarsource.nodejs.NodeCommand.start(NodeCommand.java:79)
	... 27 more
Caused by: java.io.IOException: error=2, No such file or directory
	at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
	at java.base/java.lang.ProcessImpl.<init>(Unknown Source)
	at java.base/java.lang.ProcessImpl.start(Unknown Source)
	... 31 more

[Info  - 19:35:42.718] Found 0 issue(s)

After that, after opening new files I get a bunch of false positives

[Info  - 19:35:55.482] Analyzing file 'file:///home/gnwork/Projects/unicorn/vsf/src/themes/unicorn/mixins/OurStores.ts'...
[Info  - 19:35:55.493] Index files
[Info  - 19:35:55.493] 1 file indexed
[Info  - 19:35:55.553] Found 0 issue(s)

--
If I change the NVM_LAZY_LOAD variable to true then, node is loaded wherever it's required, everything works and no error messages about not being able to find node are shown.

I know that I can simply disable lazyload and have this working, but I really appreciate the lower opening times of ZSH with lazyload. During the day I open dozens of terminals and get pretty irritated by long opening times.

If you need any kind of information, please ask and I'll be happy to provide.

Hello again,

I have managed to get this to work.

TLDR
Update /etc/zsh/zshenv and add this line

export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim' 'nvim' 'code' 'java')

If anyone's interested here's my process for figuring this out:

I mainly focused on getting this to work with VSCode.
export line at the time looked like this

export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim', 'nvim', '/snap/bin/nvim', 'code', '/snap/bin/code', 'webstorm', 'phpstorm')

nvim did not want to load node at all. coc kept nagging me about how node is not present. That lead me to believe that this variable NVM_LAZY_LOAD_EXTRA_COMMANDS is not working at all. I went here, looked at the PR and the thread and looked at the variable a bit more closely. I saw that only spaces were used for delimiting string literals inside the parentheses, and not commas as I wrote. After amending the parentheses content I tried to launch nvim again. coc once more showed the error message.
export line at the time looked like this

export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim' 'nvim' '/snap/bin/nvim' 'code' '/snap/bin/code' 'webstorm' 'phpstorm')

I thought to myself, how I am actually importing this zsh plugin? Do I really have the new code that enables this variable?
I'm using antibody for managing plugins. Inside the ~/.cache/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-lukechilds-SLASH-zsh-nvm I executed git log at saw an old commit hash 9f7e144. I was immediately relieved. After downloading the new version, nvim successfully loaded coc.

But, I was not done yet. SonarLint inside VSCode still didn't find node. I messed around with the variable a bit, set it to ('code' 'java') and SonarLint again didn't work. Inside the vscode settings I found that SonarLint was using its own JRE found at ~/.vscode/extensions/sonarsource.sonarlint_managed-jre/jre/jdk-11.0.8+10-jre/bin/java. I put the whole path into variable string hoping it would work, but it didn't.

After that, I went and installed JRE 11 on my Linux Mint 20, changed the JRE path inside vscode, changed it in the variable and tried again. Didn't work. 'java', '/usr/bin/java', '/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java', nothing.
The variable now looked like this

export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim' 'nvim'  'code' 'java')

Then I thought, wait a minute, I'm editing a ~/.zshrc file. When I start vscode from the system I doubt the same file will be used for reading and setting env variables. Here I found the load order of all files required for bash and zsh.

I tried updating this file ~/.zshenv since it's the second file in the load order and doesn't require sudo for editing and after that logging out and back in again. SonarLint still wouldn't start.

My last option was to update the env file /etc/zsh/zshenv. I added the line, logged out, logged back in, fired up vscode and lo and behold, SonarLint worked 🎉

I do have a different question for you guys now.
It is possible to add the variable to both /etc/zsh/zshenv and ~/.zshrc and have the values merged? Since I mostly start nvim from the terminal, I could add it to the variable inside ~/.zshrc. For software such as nvim, that would save me from editing the protected /etc/zsh/zshenv file logging out and back in.