Skip to content

[Fix] Remove $NVM_DIR trailing slash automatically, fix #1708 - #1734

Merged
ljharb merged 2 commits into
nvm-sh:masterfrom
PeterDaveHello:improve-NVM_DIR-handling
Apr 24, 2018
Merged

ljharb merged 2 commits into
nvm-sh:masterfrom
PeterDaveHello:improve-NVM_DIR-handling

Conversation

@PeterDaveHello

@PeterDaveHello PeterDaveHello commented Feb 18, 2018 •

Copy link
Copy Markdown
Collaborator

This can automatically remove the trailing slash in $NVM_DIR when loading nvm, the trailing space isn't that invalid.

Fixes #1708.

Comment thread nvm.sh Outdated
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" > /dev/null && \pwd)"
export NVM_DIR
else
while echo "${NVM_DIR}" | nvm_grep -q "/$"; do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way overly complex; export NVM_DIR="${NVM_DIR%/}" should be sufficient.

It'd be nice if we also output a warning when this happens so people know they've set an invalid directory path.

Also, can we add some tests for this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test if we could remove the trailing slash with success? I think the trailing slashes would be valid in many places as I replied on the issue, and the complexity overhead is not that high here, remove them as many as possible as we can could be much more flexible here, what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone has multiple trailing slashes in their profile file, or even any, then it's incorrect. I don't just want to silently fix their error, I also want to let them know that they have one.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, fixed now.

@PeterDaveHello
PeterDaveHello force-pushed the improve-NVM_DIR-handling branch from bace013 to 732d380 Compare February 19, 2018 09:29
@PeterDaveHello

Copy link
Copy Markdown
Collaborator Author

Should be good now :)

@ljharb

ljharb commented Feb 21, 2018

Copy link
Copy Markdown
Member

This still doesn't output a warning though when there's a trailing slash.

@PeterDaveHello
PeterDaveHello force-pushed the improve-NVM_DIR-handling branch from 732d380 to ca5c934 Compare February 21, 2018 09:52
@PeterDaveHello

Copy link
Copy Markdown
Collaborator Author

Should be good now :)

Comment thread nvm.sh Outdated
# shellcheck disable=SC1001
NVM_DIR="$(nvm_cd ${NVM_CD_FLAGS} "$(dirname "${NVM_SCRIPT_SOURCE:-$0}")" > /dev/null && \pwd)"
export NVM_DIR
elif echo "${NVM_DIR}" | nvm_grep -q "/$"; then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread nvm.sh Outdated
export NVM_DIR
elif echo "${NVM_DIR}" | nvm_grep -q "/$"; then
export NVM_DIR="${NVM_DIR%/}"
nvm_err "Warning: \$NVM_DIR should not have trailing space"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing slashes, not spaces

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ... my fault

@PeterDaveHello
PeterDaveHello force-pushed the improve-NVM_DIR-handling branch from ca5c934 to 77e2c95 Compare February 25, 2018 08:25

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a test

@PeterDaveHello

PeterDaveHello commented Mar 10, 2018 •

Copy link
Copy Markdown
Collaborator Author

I suppose I'll need to separate it to a function so that I can write its unit test, any idea like what's the name of its function you'd like?

@ljharb

ljharb commented Apr 10, 2018

Copy link
Copy Markdown
Member

@PeterDaveHello hm, not sure - "remove_trailing_slash" perhaps. but, does this need a separate function, or can it be tested by setting NVM_DIR in a test prior to sourcing nvm.sh?

@PeterDaveHello

PeterDaveHello commented Apr 12, 2018 •

Copy link
Copy Markdown
Collaborator Author

The second way will be simpler ;)

@PeterDaveHello
PeterDaveHello force-pushed the improve-NVM_DIR-handling branch from 77e2c95 to b1e3c6a Compare April 12, 2018 06:15
@PeterDaveHello

Copy link
Copy Markdown
Collaborator Author

Updated.

@ljharb ljharb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM pending one question


\. ../../nvm.sh

[ "${NVM_DIR}" = "${NVM_DIR_BASE}" ] && die 'nvm should only remove the last trailing slash in "$NVM_DIR"'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this "die" message seems incorrect; it should remove all trailing slashes in NVM_DIR, no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that was my original idea but rejected here: #1734 (comment), I can revise it back if you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah - so what I intended in that comment was for nvm to fail outright when there was any trailing slashes in NVM_DIR.

The alternative is to remove all of them, and output a warning so they can fix it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I've tested the case that there is only single slash and was removed, so to make sure we'll only remove the last slash, maybe I don't need to revise the tests here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify further: if we go with removal, I think it should remove all trailing slashes, not just one.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rollbacked now ...

@PeterDaveHello
PeterDaveHello force-pushed the improve-NVM_DIR-handling branch from b1e3c6a to 30bc836 Compare April 24, 2018 20:12
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.

2 participants