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

dash does not refresh its hash table when PATH is re-declared #706

Closed
agarzola opened this issue Jan 6, 2022 · 10 comments · Fixed by #707
Closed

dash does not refresh its hash table when PATH is re-declared #706

agarzola opened this issue Jan 6, 2022 · 10 comments · Fixed by #707

Comments

@agarzola
Copy link
Contributor

agarzola commented Jan 6, 2022

The following line recommends re-declaring PATH to ensure the new location of Node.js is loaded in the current session.

n/bin/n

Line 744 in d569e33

printf 'To reset the command location hash either start a new shell, or execute PATH="$PATH"\n'

However, in some environments that might not suffice. I ran into this issue on Platform.sh, which uses dash. Interestingly enough, after installing Node with n auto (our project has an .nvmrc file specifying v14), running which node points to the location where we told n to install Node: /app/.global/bin/node. However, running node --version yielded v12. Simply invoking export PATH="$PATH" did not solve this issue.

As it turns out, dash’s implementation of the hash built-in command does not refresh the hash table where executables are stored for easy access. The fix suggested to us by Platform.sh support was to invoke hash -r instead. This worked: after invoking that, node --version yielded the version number we expect (the one installed via n).

I will submit a PR shortly that updates the line linked above to include a note about how to solve this issue in dash.

agarzola added a commit to agarzola/n that referenced this issue Jan 6, 2022
agarzola added a commit to agarzola/n that referenced this issue Jan 6, 2022
agarzola added a commit to agarzola/n that referenced this issue Jan 6, 2022
@shadowspawn
Copy link
Collaborator

I remember considering hash -r vs PATH=$PATH but can't find any notes I made at the time.

Related issues mentioned in the CHANGELOG: advice to reset command hash when node location changes (#170 #381 #451 #588)

@shadowspawn
Copy link
Collaborator

More documentation:

The Open Group docs for hash say this, which is probably why I went for PATH over hash originally:

The effects of hash -r can also be achieved portably by resetting the value of PATH ; in the simplest form, this can be:

PATH="$PATH"

@agarzola
Copy link
Contributor Author

agarzola commented Jan 7, 2022

That is true of Bash’s implementation of hash. Alas, that appears to not be the case in Dash’s implementation, judging by the experience I detailed above, wherein PATH="$PATH" did not have the desired effect, but hash -r did.

[Edited to add more context.]

@shadowspawn
Copy link
Collaborator

Yes "portably" is not universally. 🙊

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jan 7, 2022

After some experimentation...

bash: PATH=$PATH or hash -r
zsh: PATH=$PATH or hash -r
fish: no action required
csh: rehash
tcsh: rehash
ash: hash -r
dash: hash -r
ksh: hash -r

@agarzola
Copy link
Contributor Author

agarzola commented Jan 7, 2022

Oh, wow. So how about I modify my PR thusly:

To reset the command location hash, start a new shell. If starting a new shell is not an option, here's what you can do:

  • For bash and zsh, execute PATH="$PATH" or hash -r.
  • For csh and tcsh, execute rehash.
  • For ash, dash, and ksh, execute hash -r.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jan 7, 2022

I am disappointed with PATH="$PATH" since it only works in some shells and they both support hash -r anyway!

Moving the command to the front to make it easier to spot, how about:

Note: the node command changed location and the old location may be remembered in your current shell.
         old : /usr/local/bin/node
         new : /root/.n/bin/node
If "node --version" shows the old version then start a new shell, or reset the location hash with:
hash -r  (for bash, zsh, ash, dash, and ksh)
rehash   (for csh and tcsh)

@agarzola
Copy link
Contributor Author

agarzola commented Jan 8, 2022

Agreed, that’s much better. I’ll update my PR with that here shortly!

agarzola added a commit to agarzola/n that referenced this issue Jan 8, 2022
@agarzola
Copy link
Contributor Author

agarzola commented Jan 8, 2022

@shadowspawn All done! PR is ready for review at your leisure.

shadowspawn pushed a commit that referenced this issue Jan 8, 2022
* GH-706 Update location change message

* Remove (seemingly) unnecessary shellcheck exception

* Add missing trailing newline
@shadowspawn shadowspawn added pending release Merged into a branch for a future release, but not released yet and removed pending release Merged into a branch for a future release, but not released yet labels Jan 8, 2022
@shadowspawn
Copy link
Collaborator

Improvements included in n v8.0.2

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 a pull request may close this issue.

2 participants