Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] 'nvm exec' no longer prints error about '-q' being an invalid option #2800

Merged
merged 1 commit into from
May 6, 2023

Conversation

spikegrobstein
Copy link
Contributor

@spikegrobstein spikegrobstein commented Apr 28, 2022

the nvm.sh file assigns and exports an NVM_CD_FLAGS variable if it
was sourced from a zsh shell. the fact that it's exported means that
it'll be assigned in all child processes, including the nvm-exec
script, which uses bash as the interpreter.

Bash's cd command doesn't have a -q flag, so if the NVM_CD_FLAGS
is assigned -q, the script will error out and incorrectly claim that
the node version isn't installed.

this also manifests itself in the nvm exec command.

Example:

$ nvm exec 16.14.0 npm --version
Running node v16.14.0 (npm v8.3.1)
/Users/<ME>/.nvm/nvm.sh: line 28: cd: -q: invalid option
cd: usage: cd [-L|[-P [-e]] [-@]] [dir]
both the tree and the node path are required
N/A: version "v16.14.0 -> N/A" is not yet installed.

You need to run "nvm install v16.14.0" to install it before using it.

To address this, we unset the NVM_CD_FLAGS at the start of the
nvm-exec script, before loading nvm.sh.

@spikegrobstein spikegrobstein changed the title [Fix] 'nvm exec' no longer prints error about '-q' being an invalid o… [Fix] 'nvm exec' no longer prints error about '-q' being an invalid option Apr 28, 2022
@ljharb
Copy link
Member

ljharb commented Apr 28, 2022

This was added in 00a8b36 to fix #868. This change seems like it regresses that?

@spikegrobstein
Copy link
Contributor Author

this will not regress it because the variable is still being assigned a value; it's just not being exported.

When it's exported, it means that it's going to be assigned to all child processes that are called from this point on. so when the nvm-exec shell script is called, which is using the bash interpretter, the variable will already be assigned the -q variable even though it's not being run in zsh.

@ljharb
Copy link
Member

ljharb commented Apr 28, 2022

Is an alternative for nvm-exec to always unset NVM_CD_FLAGS?

@spikegrobstein
Copy link
Contributor Author

that was the fix that I was originally going to propose but it doesn't address the root cause of the bug.

The codebase only references the NVM_CD_FLAGS in 4 places and in none of them, does the variable need to be exported, so by not exporting it, it potentially addresses other (not yet found) cases where the leaking of this variable could affect other operations.

if explicitly unset'ing it in nvm-exec is more desirable, that's a fine compromise since it still addresses this bug.

@Joecliff8 Joecliff8 linked an issue May 15, 2022 that may be closed by this pull request
@ljharb
Copy link
Member

ljharb commented Dec 23, 2022

@spikegrobstein any interest in updating to that approach?

@Xcalibur53 Xcalibur53 linked an issue Apr 21, 2023 that may be closed by this pull request
Xcalibur53

This comment was marked as spam.

@spikegrobstein
Copy link
Contributor Author

I totally didn't notice this reply and I totally forgot about this. I'll get this updated later today when I have time and push the requested change.

…sers

the `nvm.sh` file assigns and exports an `NVM_CD_FLAGS` variable if it
was sourced from a zsh shell. the fact that it's exported means that
it'll be assigned in all child processes, including the `nvm-exec`
script, which uses bash as the interpreter.

Bash's `cd` command doesn't have a `-q` flag, so if the `NVM_CD_FLAGS`
is assigned `-q`, the script will error out and incorrectly claim that
the node version isn't installed.

this also manifests itself in the `nvm exec` command.

Example:

```console
$ nvm exec 16.14.0 npm --version
Running node v16.14.0 (npm v8.3.1)
/Users/<ME>/.nvm/nvm.sh: line 28: cd: -q: invalid option
cd: usage: cd [-L|[-P [-e]] [-@]] [dir]
both the tree and the node path are required
N/A: version "v16.14.0 -> N/A" is not yet installed.

You need to run "nvm install v16.14.0" to install it before using it.
```

To address this, we unset the `NVM_CD_FLAGS` at the start of the
`nvm-exec` script, before loading `nvm.sh`.
@spikegrobstein
Copy link
Contributor Author

spikegrobstein commented Apr 22, 2023

I replaced my previous commit with a new fix/explanation and rebased against master. Also updated the PR body to accurate explain the change (matching the commit).

Let me know how this is.

@ljharb ljharb merged commit 70aa611 into nvm-sh:master May 6, 2023
85 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[spam] [spam]
3 participants