Skip to content

Commit

Permalink
Merge pull request #70 from syndbg/gh-30-improve-gopath
Browse files Browse the repository at this point in the history
[GH-30][GH-50] Improve GOPATH and GOROOT env var management
  • Loading branch information
syndbg authored Mar 10, 2019
2 parents 1dcf8e3 + f9a06ed commit 73cd0ac
Show file tree
Hide file tree
Showing 13 changed files with 635 additions and 74 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ Introducing breaking changes under a feature flag can be ok in some cases where

## Unreleased (master)

## 2.0.0beta6

### Added

* Add management of env variable `GOROOT` that can be disabled with env var `GOENV_DISABLE_GOROOT=1`,
when calling `goenv-sh-rehash` (`goenv rehash` when `eval $(goenv init -)` was previously executed).
It does not attempt to manage when version is `system`.
; Ref: https://github.com/syndbg/goenv/pull/70
* Add management of env variable `GOPATH` that can be disabled with env var `GOENV_DISABLE_GOPATH=1`,
when calling `goenv-sh-rehash` (`goenv rehash` when `eval $(goenv init -)` was previously executed).
It does not attempt to manage when version is `system`.
; Ref: https://github.com/syndbg/goenv/pull/70
* Add configurable managed `GOPATH` prefix for `goenv-sh-rehash`
(`goenv rehash` when `eval $(goenv init -)` was previously executed).
Configured via `GOENV_GOPATH_PREFIX=<your prefix>`.
E.g `GOENV_GOPATH_PREFIX=/tmp`.
; Ref: https://github.com/syndbg/goenv/pull/70
* Add `--only-manage-paths` option to `goenv-sh-rehash` (`goenv rehash` when `eval $(goenv init -)` was previously executed) to skip calling `goenv-rehash` and update shims.
Instead it only updates managed `GOPATH` and `GOROOT` env variables.
It does not attempt to manage when version is `system`.
; Ref: https://github.com/syndbg/goenv/pull/70

### Changed

* Changed `goenv`'s bootstrap (`eval $(goenv init -)`) now to call `goenv-sh-rehash --only-manage-paths`.
This means that it'll export and manage `GOROOT` and `GOPATH` env vars.
It does not attempt to manage when version is `system`.
; Ref: https://github.com/syndbg/goenv/pull/70

## 2.0.0beta5

### Added
Expand Down
38 changes: 31 additions & 7 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ easy to fork and contribute any changes back upstream.

$ git clone https://github.com/syndbg/goenv.git ~/.goenv


2. **Define environment variable `GOENV_ROOT`** to point to the path where
goenv repo is cloned and add `$GOENV_ROOT/bin` to your `$PATH` for access
to the `goenv` command-line utility.
Expand All @@ -21,33 +20,58 @@ easy to fork and contribute any changes back upstream.
**Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
**Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.

3. **Add `goenv init` to your shell** to enable shims and autocompletion.
3. **Add `goenv init` to your shell** to enable shims, management of `GOPATH` and `GOROOT` and auto-completion.
Please make sure `eval "$(goenv init -)"` is placed toward the end of the shell
configuration file since it manipulates `PATH` during the initialization.

$ echo 'eval "$(goenv init -)"' >> ~/.bash_profile

**Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
**Zsh note**: Modify your `~/.zshenv` or `~/.zshrc` file instead of `~/.bash_profile`.
**Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.

**General warning**: There are some systems where the `BASH_ENV` variable is configured
to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
`eval "$(goenv init -)` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
may observe strange behaviour, such as `goenv` getting into an infinite loop.
See pyenv's issue [#264](https://github.com/yyuu/pyenv/issues/264) for details.

4. **If you want `goenv` to manage `GOPATH` and `GOROOT` (recommended)**,
add `GOPATH` and `GOROOT` to your shell **after `eval "$(goenv init -)"`**.

$ echo 'export PATH="$GOROOT/bin:$PATH"' >> ~/.bash_profile
$ echo 'export PATH="$GOPATH/bin:$PATH"' >> ~/.bash_profile
**Zsh note**: Modify your `~/.zshenv` or `~/.zshrc` file instead of `~/.bash_profile`.
**Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
**General warning**: There are some systems where the `BASH_ENV` variable is configured
to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
`eval "$(goenv init -)` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
may observe strange behaviour, such as `goenv` getting into an infinite loop.
See pyenv's issue [#264](https://github.com/yyuu/pyenv/issues/264) for details.


4. **Restart your shell so the path changes take effect.**
5. **Restart your shell so the path changes take effect.**
You can now begin using goenv.

$ exec $SHELL

5. **Install Go versions into `$GOENV_ROOT/versions`.**
For example, to download and install Go 1.6.2, run:
6. **Install Go versions into `$GOENV_ROOT/versions`.**
For example, to download and install Go 1.12.0, run:

$ goenv install 1.6.2
$ goenv install 1.12.0

**NOTE:** It downloads and places the prebuilt Go binaries provided by Google.

An example `.zshrc` that is properly configured may look like

```shell
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$GOPATH/bin:$PATH"
```

## Homebrew on Mac OS X

You can also install goenv using the [Homebrew](http://brew.sh)
Expand Down
2 changes: 1 addition & 1 deletion libexec/goenv---version
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
set -e
[ -n "$GOENV_DEBUG" ] && set -x

version="2.0.0beta5"
version="2.0.0beta6"

echo "goenv ${version}"
36 changes: 33 additions & 3 deletions libexec/goenv-exec
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,39 @@ for script in "${scripts[@]}"; do
done

shift 1
# Go needs a GOROOT without system's Go
if [[ $GOENV_VERSION != "system" ]]; then
export GOROOT="$(goenv-prefix)/"

if [ "${GOENV_VERSION}" != "system" ]; then
case "$shell" in
fish )
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
set -gx GOROOT "$(goenv-prefix)"
fi

if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
set -gx GOPATH "${HOME}/go/${GOENV_VERSION}"
else
set -gx GOPATH "${GOENV_GOPATH_PREFIX}/${GOENV_VERSION}"
fi
fi

;;
* )
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
export GOROOT="$(goenv-prefix)"
fi

if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
export GOPATH="${HOME}/go/${GOENV_VERSION}"
else
export GOPATH="${GOENV_GOPATH_PREFIX}/${GOENV_VERSION}"
fi
fi

;;
esac
fi

export PATH="${GOENV_BIN_PATH}:${GOROOT}/bin:${PATH}"
exec -a "$GOENV_COMMAND" "$GOENV_COMMAND_PATH" "$@"
5 changes: 5 additions & 0 deletions libexec/goenv-init
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,8 @@ cat <<EOS
}
EOS
fi

# NOTE: Rehash again, but only to export managed paths
cat <<EOS
goenv rehash --only-manage-paths
EOS
60 changes: 45 additions & 15 deletions libexec/goenv-sh-rehash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# Summary: Calls `goenv-rehash` to rehash shims and also rehashes shell executable if shell is not 'fish'.
# Usage: goenv sh-rehash
# Summary: Calls `goenv-rehash` to rehash shims, manages GO{PATH,ROOT} and rehashes shell executable if shell is not 'fish'.
# Usage: goenv sh-rehash [--only-manage-paths]

set -e
[ -n "$GOENV_DEBUG" ] && set -x
Expand All @@ -12,16 +12,46 @@ fi

shell="$(basename "${GOENV_SHELL:-$SHELL}")"

# When goenv shell integration is enabled,
# delegate to goenv-rehash,
# then tell the shell to empty its command lookup cache.
goenv-rehash

case "$shell" in
fish )
# NOTE: No rehash support
;;
* )
echo "hash -r 2>/dev/null || true"
;;
esac
# NOTE: When goenv shell integration is enabled, delegate rehashing of `goenv` shims to goenv-rehash.
# However to speed up `goenv init` and not do rehashing of shims twice,
# allow `only-manage-paths` to skip rehashing of shims.
if [ "$1" != "--only-manage-paths" ]; then
goenv-rehash
fi

currentVersionName=$(goenv-version-name)

if [ "${currentVersionName}" != "system" ]; then
case "$shell" in
fish )
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
echo "set -gx GOROOT \"$(goenv-prefix)\""
fi

if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
echo "set -gx GOPATH \"${HOME}/go/${currentVersionName}\""
else
echo "set -gx GOPATH \"${GOENV_GOPATH_PREFIX}/${currentVersionName}\""
fi
fi

# NOTE: No rehash support
;;
* )
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
echo "export GOROOT=\"$(goenv-prefix)\""
fi

if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
echo "export GOPATH=\"${HOME}/go/${currentVersionName}\""
else
echo "export GOPATH=\"${GOENV_GOPATH_PREFIX}/${currentVersionName}\""
fi
fi

echo "hash -r 2>/dev/null || true"
;;
esac
fi
2 changes: 1 addition & 1 deletion test/goenv--version.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

load test_helper

expected_version="goenv 2.0.0beta5"
expected_version="goenv 2.0.0beta6"

@test "default version is 'version' variable" {
assert [ ! -e "$GOENV_ROOT" ]
Expand Down
Loading

0 comments on commit 73cd0ac

Please sign in to comment.