Skip to content

Normalize paths in nvm_tree_contains_path before comparing #2045

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

Closed

Conversation

gridbugs
Copy link

It's possible that tree and node_path both refer to the same
directory, but are lexically different. This converts both paths
into a canonical form so that any lexical-only differences are
removed.

This solves a problem where setting NVM_DIR to /home/user//.nvm
instead of /home/user/.nvm results in the infamous:
nvm is not compatible with the npm config "prefix" option

This adds a test that the above case no longer results in an error. An
existing test stopped failing as expected as a result, as the test set
the npm prefix to a local-directory-relative path, which resolved to
a directory inside the NVM_DIR when running the tests from within the
NVM_DIR. This test is updated to use an absolute path.

@gridbugs
Copy link
Author

This will not work on macos because macos doesn't come with realpath (it's part of coreutils which can be installed with brew). I'm opening this PR to get people's thoughts.

@ljharb
Copy link
Member

ljharb commented May 11, 2019

The lack of realpath in POSIX (and thus on Macs) is why this hasn't been added before, which is why #617 remains a problem. I'd like a solution, but there has to be a fallback for when realpath is not available, or else it won't be viable.

@ljharb
Copy link
Member

ljharb commented Jan 8, 2020

@stevebob turns out cd -P is in POSIX, so that might be a solution here? (#2143)

@GolanTrevize10
Copy link

What about a function like this

realpath() { OURPWD=$PWD cd "$(dirname "$1")" LINK=$(readlink "$(basename "$1")") while [ "$LINK" ]; do cd "$(dirname "$LINK")" LINK=$(readlink "$(basename "$1")") done REALPATH="$PWD/$(basename "$1")" cd "$OURPWD" echo "$REALPATH" }

@ljharb
Copy link
Member

ljharb commented Jan 11, 2020

Is readlink in posix?

@GolanTrevize10
Copy link

the readlink function yes, the readlink command no

@GolanTrevize10
Copy link

As @ljharb suggested this would be POSIX
cd -P "$NVM_DIR" && pwd

@mvastola
Copy link

What about replacing readlink with node -e "console.log(require('fs').realpathSync(process.env.HOME));"?

At the time nvm_tree_contains_path is called, the PATH should be updated to contain node..

@ljharb
Copy link
Member

ljharb commented Jun 13, 2020

@mvastola not every node version has fs.realPathSync.

It's possible that `tree` and `node_path` both refer to the same
directory, but are lexically different. This converts both paths
into a canonical form so that any lexical-only differences are
removed.

This solves a problem where setting `NVM_DIR` to /home/user//.nvm
instead of /home/user/.nvm results in the infamous:
nvm is not compatible with the npm config "prefix" option

This adds a test that the above case no longer results in an error. An
existing test stopped failing as expected as a result, as the test set
the npm prefix to a local-directory-relative path, which resolved to
a directory inside the `NVM_DIR` when running the tests from within the
`NVM_DIR`. This test is updated to use an absolute path.
@ljharb ljharb force-pushed the normalize-paths-before-comparing branch from f6757b1 to e77cead Compare March 29, 2021 16:28
@ljharb ljharb marked this pull request as draft March 29, 2021 16:28
@gridbugs
Copy link
Author

The problem this PR solves no longer occurs (just tested with v0.38). Closing.

@gridbugs gridbugs closed this Mar 30, 2021
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.

None yet

4 participants