Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

When changing between project directories with different .go-version files, GOROOT and GOPATH remain static until the shell is reloaded. While shims detect the correct version, tools relying on these environment variables see stale values.

Changes

  • libexec/goenv-init: Added shell hook generation when GOENV_AUTOMATICALLY_DETECT_VERSION=1

    • Zsh: Injects function into chpwd_functions array
    • Bash/KSH: Appends function to PROMPT_COMMAND
    • Fish: Adds function with --on-variable PWD trigger
    • Each hook calls eval "$(goenv rehash --only-manage-paths)" to update environment
  • test/goenv-init.bats: Added 9 tests covering hook injection behavior across shells

  • Documentation: Updated ENVIRONMENT_VARIABLES.md and INSTALL.md with usage instructions

Usage

Add before eval "$(goenv init -)":

export GOENV_AUTOMATICALLY_DETECT_VERSION=1

Shell hooks automatically update GOROOT/GOPATH on directory change. Feature is opt-in to avoid performance impact for users who don't need it.

Original prompt

This section details on the original issue you should resolve

<issue_title>Switching directories doesn't change go version</issue_title>
<issue_description>👋🏻

I use https://www.warp.dev/ with a Zsh shell and a https://starship.rs/ prompt (I also use Zoxide for switching directories but the reported issue happens even with standard cd).

I'm finding that when I switch between project directories (where each project has its own .go-version file and are set with different Go versions), that changing directory doesn't trigger the go binary to be the expected version.

Here is my ~/.zshrc configuration:

if [ ! -d "$HOME/.goenv" ]; then
  git clone https://github.com/syndbg/goenv.git ~/.goenv
fi
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"

At the time of reporting this issue, I'm using the latest release:

$ goenv --version
goenv 2.0.6

If I open up a new terminal instance at my home directory ~/ and run which go I see:

/Users/integralist/.goenv/versions/1.20.0/bin/go

This aligns with goenv global.

Now if I cd into a project directory with a .go-version file containing 1.20.2 and re-run go version I'll still see the global Go 1.20.0 being used. If I reload my shell scripts (e.g. source ~/.zshrc) then running both which go and go version will now report the expected 1.20.2 version.

Now if I cd into another project with a .go-version file containing 1.18.5 and re-run go version I'll still see the previous Go version 1.20.2 being used. If I reload my shell scripts (e.g. source ~/.zshrc) then running both which go and go version will now report the expected 1.18.5 version.</issue_description>

<agent_instructions>How would you approach this?</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ChronosMasterOfAllTime @Integralist that isn't a fork, rather a whole new implementation relying on a golang-compiled CLI tool using goreleaser.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix go version not changing on directory switch Add opt-in automatic Go version detection on directory change Nov 20, 2025
Copilot finished work on behalf of ChronosMasterOfAllTime November 20, 2025 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Switching directories doesn't change go version

2 participants