Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work around cases where PATH is set explicitly in .bashrc

  • Loading branch information...
commit 7666933227943daa77b85f4846e8773b653c8fba 1 parent 5f4055a
@isaacs authored
Showing with 55 additions and 21 deletions.
  1. +21 −13 README.md
  2. +34 −8 nave.sh
View
34 README.md
@@ -49,19 +49,6 @@ to where you were before using nave.
* `$NAVE_DIR` Set to the location where you'd like nave to do its
business. Defaults to `~/.nave`.
-## Troubleshooting
-
-If nave doesn't seem to update the version of node that you're using,
-make sure you're not explicitly setting the `PATH` environ in your
-.bashrc or .bash_profile files. Nave works by setting the PATH, so you
-should change any line like this:
-
- export PATH=/blah/blah:/foo/foo
-
-to something like this instead:
-
- export PATH=$PATH:/blah/blah:/foo/foo
-
## Compatibility
Prior to version 0.2, nave would run programs as `node <program>`.
@@ -71,6 +58,27 @@ old style, just prefix your command with `node`.
Nave requires bash. It will probably never work on Windows, or other systems
lack a native Bourne Again Shell. Sorry.
+Nave works out of the box with bash. If you use zsh, sh, ksh, csh, or
+any other sh as your shell, then you should need to add this line or its
+equivalent to your init script:
+
+ export PATH=$NAVE_PATH:$PATH
+
+## Configuration
+
+Nave will source `~/.naverc` on initialization of a new subshell, if it
+exists and is readable.
+
+You may control the place where nave puts things by setting the
+`NAVE_DIR` environment variable. However, note that this must be set
+somewhere *other* than `~/.naverc`, since it needs to be set in the
+*parent* shell where the `nave` command is invoked.
+
+By default, nave puts its stuff in `~/.nave/`. If this directory does
+not exist and cannot be created, then it will attempt to use the location
+of the nave.sh bash script itself. If it cannot write to this location,
+then it will exit with an error.
+
## Credits
nave borrows concepts, inspiration, and code from Tim Caswell's "nvm" and Kris
View
42 nave.sh
@@ -60,6 +60,26 @@ main () {
NAVE_DIR="$(dirname -- "$SELF_PATH")"
fi
+ # set up the naverc init file.
+ if ! [ -f "$NAVE_DIR/naverc" ]; then
+ cat > "$NAVE_DIR/naverc" <<RC
+[ "\$NAVE_DEBUG" != "" ] && set -x || true
+if [ "\$NAVE_LOGIN" != "" ]; then
+ [ -f ~/.bash_profile ] && . ~/.bash_profile || true
+ [ -f ~/.bash_login ] && . ~/.bash_login || true
+ [ -f ~/.profile ] && . ~/.profile || true
+fi
+[ -f ~/.bashrc ] && . ~/.bashrc || true

The Ubuntu default .profile already executes .bashrc if it exists (at least on my 10.04.3->10.04.4 system):

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

This meant that mine was being executed twice and I had to fix a minor failure with it not being idempotent.

I don't know if .bashrc is supposed to be idempotent and thus OK to execute multiple times, but it seems at least inefficient to do so...

@isaacs Owner
isaacs added a note

Ah, this is slightly off from the bash mechanisms anyway. It should only do .bashrc if it's not a login shell.

@isaacs Owner
isaacs added a note

Fixed in 0.2.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+export PATH=\$NAVEPATH:\$PATH
+[ -f ~/.naverc ] && . ~/.naverc || true
+RC
+ fi
+
+ # couldn't write file
+ if ! [ -f "$NAVE_DIR/naverc" ]; then
+ fail "Nave dir $NAVE_DIR is not writable."
+ fi
+
export NAVE_DIR
export NAVE_SRC="$NAVE_DIR/src"
export NAVE_ROOT="$NAVE_DIR/installed"
@@ -348,23 +368,27 @@ nave_use () {
if [ $# -gt 1 ]; then
shift
hash -r
- PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" \
+ NAVELVL=$lvl NAVE="$version" \
+ NAVEPATH="$bin" \
NAVEVERSION="$version" \
NAVENAME="$version" \
npm_config_binroot="$bin" npm_config_root="$lib" \
npm_config_manroot="$man" \
NODE_PATH="$lib" \
- "$SHELL" -c "$(enquote_all "$@")"
+ NAVE_LOGIN="" \
+ "$SHELL" -c "$(enquote_all "$@")" --rcfile "$NAVE_DIR/naverc"
hash -r
else
hash -r
- PATH="$bin:$PATH" NAVELVL=$lvl NAVE="$version" \
+ NAVELVL=$lvl NAVE="$version" \
+ NAVEPATH="$bin" \
NAVEVERSION="$version" \
NAVENAME="$version" \
npm_config_binroot="$bin" npm_config_root="$lib" \
npm_config_manroot="$man" \
NODE_PATH="$lib" \
- "$SHELL"
+ NAVE_LOGIN="1" \
+ "$SHELL" --rcfile "$NAVE_DIR/naverc"
hash -r
fi
return $?
@@ -403,21 +427,23 @@ nave_named () {
local lvl=$[ ${NAVELVL-0} + 1 ]
# get the version
if [ $# -gt 0 ]; then
- PATH="$bin:$PATH" \
+ NAVEPATH="$bin" \
NAVELVL=$lvl \
NAVE="$version-$name" \
NAVEVERSION="$version" \
NAVENAME="$name" \
NODE_PATH="$lib" \
- "$SHELL" -c "$(enquote_all "$@")"
+ NAVE_LOGIN="" \
+ "$SHELL" -c "$(enquote_all "$@")" --rcfile "$NAVE_DIR/naverc"
else
- PATH="$bin:$PATH" \
+ NAVEPATH="$bin" \
NAVELVL=$lvl \
NAVE="$version-$name" \
NAVEVERSION="$version" \
NAVENAME="$name" \
NODE_PATH="$lib" \
- "$SHELL"
+ NAVE_LOGIN="1" \
+ "$SHELL" --rcfile "$NAVE_DIR/naverc"
fi
return $?
}
Please sign in to comment.
Something went wrong with that request. Please try again.