Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ENVIRONMENT_VARIABLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ name | default | description
`GOENV_AUTO_INSTALL_FLAGS` | | (Note: only works if `GOENV_AUTO_INSTALL` is set to 1) Appends flags to the auto install command (see `goenv install --help` for all available flags)
`GOENV_RC_FILE` | `$HOME/.goenvrc` | If `GOENV_RC_FILE` is set, it will be modified accordingly.
`GOENV_PATH_ORDER` | | If `GOENV_PATH_ORDER` is set to `front`, `$GOENV_ROOT/shims` will be prepended to the existing `PATH`.Set `GOENV_PATH_ORDER` to a configuration file named by `GOENV_RC_FILE`(e.g. `~/.goenvrc`), for example `GOENV_PATH_ORDER=front` in `~/.goenvrc`.
`GOENV_AUTOMATICALLY_DETECT_VERSION` | `0` | If set to `1`, goenv will automatically update `GOROOT` and `GOPATH` when you change directories. This enables automatic version switching based on local `.go-version` files without needing to reload your shell configuration. **Note:** This feature adds a hook that runs on every directory change, which may have a slight performance impact.
16 changes: 16 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
```

7. **(Optional) Enable automatic version detection on directory change.**
By default, when you `cd` into a directory with a `.go-version` file, the shims will use the correct Go version,
but environment variables like `GOROOT` and `GOPATH` won't update until you reload your shell.

To enable automatic updating of `GOROOT` and `GOPATH` when changing directories, set the following environment variable:

```shell
export GOENV_AUTOMATICALLY_DETECT_VERSION=1
```

Add this line **before** the `eval "$(goenv init -)"` line in your shell configuration file.

**Note:** This feature adds a hook that runs on every directory change (or before each prompt in bash/ksh),
which may have a slight performance impact. If you don't need `GOROOT` and `GOPATH` to update automatically,
you can leave this disabled and rely on the shims, which always work correctly.

## via ZPlug plugin manager for Zsh

Expand Down
44 changes: 44 additions & 0 deletions libexec/goenv-init
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,47 @@ if [ -z "$no_rehash" ]; then
goenv rehash --only-manage-paths
EOS
fi

# Add automatic version detection on directory change (opt-in via GOENV_AUTOMATICALLY_DETECT_VERSION)
if [ -n "${GOENV_AUTOMATICALLY_DETECT_VERSION}" ] && [ "${GOENV_AUTOMATICALLY_DETECT_VERSION}" != "0" ]; then
case "$shell" in
fish)
cat <<'EOS'
function __goenv_auto_detect_version --on-variable PWD
if test -n "$GOENV_AUTOMATICALLY_DETECT_VERSION"; and test "$GOENV_AUTOMATICALLY_DETECT_VERSION" != "0"
goenv rehash --only-manage-paths > /dev/null 2>&1
end
end
EOS
;;
zsh)
cat <<'EOS'
__goenv_auto_detect_version() {
if [ -n "${GOENV_AUTOMATICALLY_DETECT_VERSION}" ] && [ "${GOENV_AUTOMATICALLY_DETECT_VERSION}" != "0" ]; then
eval "$(goenv rehash --only-manage-paths)"
fi
}
if [[ ! "${chpwd_functions[*]}" =~ "__goenv_auto_detect_version" ]]; then
chpwd_functions+=(__goenv_auto_detect_version)
fi
EOS
;;
*)
# bash, ksh, and other POSIX shells use PROMPT_COMMAND
cat <<'EOS'
__goenv_auto_detect_version() {
if [ -n "${GOENV_AUTOMATICALLY_DETECT_VERSION}" ] && [ "${GOENV_AUTOMATICALLY_DETECT_VERSION}" != "0" ]; then
eval "$(goenv rehash --only-manage-paths)"
fi
}
if [[ ! "${PROMPT_COMMAND}" =~ "__goenv_auto_detect_version" ]]; then
if [ -n "${PROMPT_COMMAND}" ]; then
PROMPT_COMMAND="${PROMPT_COMMAND};__goenv_auto_detect_version"
else
PROMPT_COMMAND="__goenv_auto_detect_version"
fi
fi
EOS
;;
esac
fi
66 changes: 66 additions & 0 deletions test/goenv-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,69 @@ OUT

assert_success
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is not set for bash" {
run goenv-init - bash

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 0 for bash" {
GOENV_AUTOMATICALLY_DETECT_VERSION=0 run goenv-init - bash

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "includes automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 1 for bash" {
GOENV_AUTOMATICALLY_DETECT_VERSION=1 run goenv-init - bash

assert_success
assert [ -n "$(echo "$output" | grep "__goenv_auto_detect_version()")" ]
assert [ -n "$(echo "$output" | grep "PROMPT_COMMAND=")" ]
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is not set for zsh" {
run goenv-init - zsh

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 0 for zsh" {
GOENV_AUTOMATICALLY_DETECT_VERSION=0 run goenv-init - zsh

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "includes automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 1 for zsh" {
GOENV_AUTOMATICALLY_DETECT_VERSION=1 run goenv-init - zsh

assert_success
assert [ -n "$(echo "$output" | grep "__goenv_auto_detect_version()")" ]
assert [ -n "$(echo "$output" | grep "chpwd_functions")" ]
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is not set for fish" {
run goenv-init - fish

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "does not include automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 0 for fish" {
GOENV_AUTOMATICALLY_DETECT_VERSION=0 run goenv-init - fish

assert_success
assert [ -z "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
}

@test "includes automatic version detection hook when GOENV_AUTOMATICALLY_DETECT_VERSION is 1 for fish" {
GOENV_AUTOMATICALLY_DETECT_VERSION=1 run goenv-init - fish

assert_success
assert [ -n "$(echo "$output" | grep "__goenv_auto_detect_version")" ]
assert [ -n "$(echo "$output" | grep "on-variable PWD")" ]
}
Loading