Skip to content

Commit 73cd0ac

Browse files
authored
Merge pull request #70 from syndbg/gh-30-improve-gopath
[GH-30][GH-50] Improve GOPATH and GOROOT env var management
2 parents 1dcf8e3 + f9a06ed commit 73cd0ac

13 files changed

Lines changed: 635 additions & 74 deletions

CHANGELOG.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ Introducing breaking changes under a feature flag can be ok in some cases where
4747

4848
## Unreleased (master)
4949

50+
## 2.0.0beta6
51+
52+
### Added
53+
54+
* Add management of env variable `GOROOT` that can be disabled with env var `GOENV_DISABLE_GOROOT=1`,
55+
when calling `goenv-sh-rehash` (`goenv rehash` when `eval $(goenv init -)` was previously executed).
56+
It does not attempt to manage when version is `system`.
57+
; Ref: https://github.com/syndbg/goenv/pull/70
58+
* Add management of env variable `GOPATH` that can be disabled with env var `GOENV_DISABLE_GOPATH=1`,
59+
when calling `goenv-sh-rehash` (`goenv rehash` when `eval $(goenv init -)` was previously executed).
60+
It does not attempt to manage when version is `system`.
61+
; Ref: https://github.com/syndbg/goenv/pull/70
62+
* Add configurable managed `GOPATH` prefix for `goenv-sh-rehash`
63+
(`goenv rehash` when `eval $(goenv init -)` was previously executed).
64+
Configured via `GOENV_GOPATH_PREFIX=<your prefix>`.
65+
E.g `GOENV_GOPATH_PREFIX=/tmp`.
66+
; Ref: https://github.com/syndbg/goenv/pull/70
67+
* 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.
68+
Instead it only updates managed `GOPATH` and `GOROOT` env variables.
69+
It does not attempt to manage when version is `system`.
70+
; Ref: https://github.com/syndbg/goenv/pull/70
71+
72+
### Changed
73+
74+
* Changed `goenv`'s bootstrap (`eval $(goenv init -)`) now to call `goenv-sh-rehash --only-manage-paths`.
75+
This means that it'll export and manage `GOROOT` and `GOPATH` env vars.
76+
It does not attempt to manage when version is `system`.
77+
; Ref: https://github.com/syndbg/goenv/pull/70
78+
5079
## 2.0.0beta5
5180

5281
### Added

INSTALL.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ easy to fork and contribute any changes back upstream.
1010

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

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

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

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

30-
**Zsh note**: Modify your `~/.zshenv` file instead of `~/.bash_profile`.
29+
**Zsh note**: Modify your `~/.zshenv` or `~/.zshrc` file instead of `~/.bash_profile`.
3130
**Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
3231

3332
**General warning**: There are some systems where the `BASH_ENV` variable is configured
3433
to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
3534
`eval "$(goenv init -)` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
3635
may observe strange behaviour, such as `goenv` getting into an infinite loop.
3736
See pyenv's issue [#264](https://github.com/yyuu/pyenv/issues/264) for details.
37+
38+
4. **If you want `goenv` to manage `GOPATH` and `GOROOT` (recommended)**,
39+
add `GOPATH` and `GOROOT` to your shell **after `eval "$(goenv init -)"`**.
40+
41+
$ echo 'export PATH="$GOROOT/bin:$PATH"' >> ~/.bash_profile
42+
$ echo 'export PATH="$GOPATH/bin:$PATH"' >> ~/.bash_profile
43+
44+
**Zsh note**: Modify your `~/.zshenv` or `~/.zshrc` file instead of `~/.bash_profile`.
45+
**Ubuntu note**: Modify your `~/.bashrc` file instead of `~/.bash_profile`.
46+
**General warning**: There are some systems where the `BASH_ENV` variable is configured
47+
to point to `.bashrc`. On such systems you should almost certainly put the abovementioned line
48+
`eval "$(goenv init -)` into `.bash_profile`, and **not** into `.bashrc`. Otherwise you
49+
may observe strange behaviour, such as `goenv` getting into an infinite loop.
50+
See pyenv's issue [#264](https://github.com/yyuu/pyenv/issues/264) for details.
51+
3852

39-
4. **Restart your shell so the path changes take effect.**
53+
5. **Restart your shell so the path changes take effect.**
4054
You can now begin using goenv.
4155

4256
$ exec $SHELL
4357

44-
5. **Install Go versions into `$GOENV_ROOT/versions`.**
45-
For example, to download and install Go 1.6.2, run:
58+
6. **Install Go versions into `$GOENV_ROOT/versions`.**
59+
For example, to download and install Go 1.12.0, run:
4660

47-
$ goenv install 1.6.2
61+
$ goenv install 1.12.0
4862

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

65+
An example `.zshrc` that is properly configured may look like
66+
67+
```shell
68+
export GOENV_ROOT="$HOME/.goenv"
69+
export PATH="$GOENV_ROOT/bin:$PATH"
70+
eval "$(goenv init -)"
71+
export PATH="$GOROOT/bin:$PATH"
72+
export PATH="$GOPATH/bin:$PATH"
73+
```
74+
5175
## Homebrew on Mac OS X
5276

5377
You can also install goenv using the [Homebrew](http://brew.sh)

libexec/goenv---version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
set -e
77
[ -n "$GOENV_DEBUG" ] && set -x
88

9-
version="2.0.0beta5"
9+
version="2.0.0beta6"
1010

1111
echo "goenv ${version}"

libexec/goenv-exec

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,39 @@ for script in "${scripts[@]}"; do
3434
done
3535

3636
shift 1
37-
# Go needs a GOROOT without system's Go
38-
if [[ $GOENV_VERSION != "system" ]]; then
39-
export GOROOT="$(goenv-prefix)/"
37+
38+
if [ "${GOENV_VERSION}" != "system" ]; then
39+
case "$shell" in
40+
fish )
41+
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
42+
set -gx GOROOT "$(goenv-prefix)"
43+
fi
44+
45+
if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
46+
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
47+
set -gx GOPATH "${HOME}/go/${GOENV_VERSION}"
48+
else
49+
set -gx GOPATH "${GOENV_GOPATH_PREFIX}/${GOENV_VERSION}"
50+
fi
51+
fi
52+
53+
;;
54+
* )
55+
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
56+
export GOROOT="$(goenv-prefix)"
57+
fi
58+
59+
if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
60+
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
61+
export GOPATH="${HOME}/go/${GOENV_VERSION}"
62+
else
63+
export GOPATH="${GOENV_GOPATH_PREFIX}/${GOENV_VERSION}"
64+
fi
65+
fi
66+
67+
;;
68+
esac
4069
fi
70+
4171
export PATH="${GOENV_BIN_PATH}:${GOROOT}/bin:${PATH}"
4272
exec -a "$GOENV_COMMAND" "$GOENV_COMMAND_PATH" "$@"

libexec/goenv-init

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,8 @@ cat <<EOS
153153
}
154154
EOS
155155
fi
156+
157+
# NOTE: Rehash again, but only to export managed paths
158+
cat <<EOS
159+
goenv rehash --only-manage-paths
160+
EOS

libexec/goenv-sh-rehash

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
2-
# Summary: Calls `goenv-rehash` to rehash shims and also rehashes shell executable if shell is not 'fish'.
3-
# Usage: goenv sh-rehash
2+
# Summary: Calls `goenv-rehash` to rehash shims, manages GO{PATH,ROOT} and rehashes shell executable if shell is not 'fish'.
3+
# Usage: goenv sh-rehash [--only-manage-paths]
44

55
set -e
66
[ -n "$GOENV_DEBUG" ] && set -x
@@ -12,16 +12,46 @@ fi
1212

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

15-
# When goenv shell integration is enabled,
16-
# delegate to goenv-rehash,
17-
# then tell the shell to empty its command lookup cache.
18-
goenv-rehash
19-
20-
case "$shell" in
21-
fish )
22-
# NOTE: No rehash support
23-
;;
24-
* )
25-
echo "hash -r 2>/dev/null || true"
26-
;;
27-
esac
15+
# NOTE: When goenv shell integration is enabled, delegate rehashing of `goenv` shims to goenv-rehash.
16+
# However to speed up `goenv init` and not do rehashing of shims twice,
17+
# allow `only-manage-paths` to skip rehashing of shims.
18+
if [ "$1" != "--only-manage-paths" ]; then
19+
goenv-rehash
20+
fi
21+
22+
currentVersionName=$(goenv-version-name)
23+
24+
if [ "${currentVersionName}" != "system" ]; then
25+
case "$shell" in
26+
fish )
27+
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
28+
echo "set -gx GOROOT \"$(goenv-prefix)\""
29+
fi
30+
31+
if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
32+
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
33+
echo "set -gx GOPATH \"${HOME}/go/${currentVersionName}\""
34+
else
35+
echo "set -gx GOPATH \"${GOENV_GOPATH_PREFIX}/${currentVersionName}\""
36+
fi
37+
fi
38+
39+
# NOTE: No rehash support
40+
;;
41+
* )
42+
if [ "${GOENV_DISABLE_GOROOT}" != "1" ]; then
43+
echo "export GOROOT=\"$(goenv-prefix)\""
44+
fi
45+
46+
if [ "${GOENV_DISABLE_GOPATH}" != "1" ]; then
47+
if [ -z "${GOENV_GOPATH_PREFIX}" ]; then
48+
echo "export GOPATH=\"${HOME}/go/${currentVersionName}\""
49+
else
50+
echo "export GOPATH=\"${GOENV_GOPATH_PREFIX}/${currentVersionName}\""
51+
fi
52+
fi
53+
54+
echo "hash -r 2>/dev/null || true"
55+
;;
56+
esac
57+
fi

test/goenv--version.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
load test_helper
44

5-
expected_version="goenv 2.0.0beta5"
5+
expected_version="goenv 2.0.0beta6"
66

77
@test "default version is 'version' variable" {
88
assert [ ! -e "$GOENV_ROOT" ]

0 commit comments

Comments
 (0)