goenv icon indicating copy to clipboard operation
goenv copied to clipboard

Read version from go.mod if .go-version is not available

Open radeksimko opened this issue 5 years ago • 2 comments
trafficstars

Tracking version in .go-version is useful for scenarios where the user of goenv (who has goenv installed locally) is also a maintainer, or simply when the project has .go-version.

It would be great if goenv could also adapt to a scenario where project does not have .go-version for any reason, but the version can be read from go.mod file (go directive specifically):

module my-module

go 1.11

With that in mind, assuming that the version listed in go.mod is installed and there's no .go-version, goenv could behave the same way as if there is .go-version and just switch to the relevant compatible Go version.

It's worth noting that the two are not equivalent since go.mod only tracks minor versions (not patch versions), but I assume that goenv could just pick the latest compatible patch version that is installed?

radeksimko avatar Apr 25 '20 07:04 radeksimko

Good idea, will look into it.

syndbg avatar Jul 21 '20 11:07 syndbg

this would be a really nice feature since some projects don't want to add a .go-version to not duplicate go.mod info

will need changes in:

  • https://github.com/syndbg/goenv/blob/a9349e1db62df5c65227933cde7d9c4435ac9310/libexec/goenv-version-file to make it consider go.mod as a version file
  • https://github.com/syndbg/goenv/blob/a9349e1db62df5c65227933cde7d9c4435ac9310/libexec/goenv-version-file-read to make it read the version
  • https://github.com/syndbg/goenv/blob/a9349e1db62df5c65227933cde7d9c4435ac9310/libexec/goenv-version-name to make it find newest patch for minor version

grosser avatar Apr 01 '21 05:04 grosser

Hey @radeksimko - if you have still need for this, fetch the latest version and try setting GOENV_GOMOD_VERSION_ENABLE=1. It should now pick up the version from go.mod if .go-version is absent.

detro avatar Oct 02 '22 09:10 detro

any chance this can be the default behavior ? ... it seem like a reasonable thing to expect :)

grosser avatar Oct 03 '22 01:10 grosser

... also GOENV_GOMOD_VERSION_ENABLE does not work with minor versions which afaik is what most projects use, it results in

goenv: version '1.18' is not installed (set by go.mod)

grosser avatar Oct 03 '22 01:10 grosser

@grosser I believe that is the desired behaviour.

Project maintainers elected to default GOENV_GOMOD_VERSION_ENABLE to 0 (disabled by default). And the installation of a version of 1.18 remains up to the user.

detro avatar Oct 03 '22 08:10 detro

Hey @syndbg @ChronosMasterOfAllTime can you guys confirm the above understanding I have?

detro avatar Oct 03 '22 08:10 detro

We will need to tweak the behavior of goenv itself. I believe it uses a "0" patch version of none specified. At some point I want to make it behave like tfswitch, where running goenv without any args will try to auto switch and source the installed go version (project or global).

ChronosMasterOfAllTime avatar Oct 03 '22 12:10 ChronosMasterOfAllTime

I confirmed that goenv install 1.18 works, I did not expect that to work. ... and it would be nice if it selected the newest 1.18 (avoids running known bugs)

On Mon, Oct 3, 2022 at 5:43 AM ChronosMasterOfAllTime < @.***> wrote:

We will need to tweak the behavior of goenv itself. I believe it uses a "0" patch version of none specified. At some point I want to make it behave like tfswitch, where running goenv without any args will try to auto switch and source the installed go version (project or global).

— Reply to this email directly, view it on GitHub https://github.com/syndbg/goenv/issues/121#issuecomment-1265385172, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACYZ5PUEDMQFTKBU7FWLTWBLIGRANCNFSM4MQUHT4A . You are receiving this because you were mentioned.Message ID: @.***>

grosser avatar Oct 03 '22 15:10 grosser

I confirmed that goenv install 1.18 works, I did not expect that to work. ... and it would be nice if it selected the newest 1.18 (avoids running known bugs)

On Mon, Oct 3, 2022 at 5:43 AM ChronosMasterOfAllTime < @.***> wrote:

We will need to tweak the behavior of goenv itself. I believe it uses a "0" patch version of none specified. At some point I want to make it behave like tfswitch, where running goenv without any args will try to auto switch and source the installed go version (project or global).

— Reply to this email directly, view it on GitHub https://github.com/syndbg/goenv/issues/121#issuecomment-1265385172, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACYZ5PUEDMQFTKBU7FWLTWBLIGRANCNFSM4MQUHT4A . You are receiving this because you were mentioned.Message ID: @.***>

Yep, I did see that as the default behavior in the code if no patch version is specified. We will have to do some tweaks to change that behavior to install 1.x.latest_x. I wouldn't think this is a major change. Would we want to omit beta and rc from the lookup? I typically would only want stable revisions in this case.

ChronosMasterOfAllTime avatar Oct 03 '22 15:10 ChronosMasterOfAllTime

I'd expect it to pick the latest stable patch, but beta/alpha if nothing else is available.

On Mon, Oct 3, 2022 at 8:53 AM ChronosMasterOfAllTime < @.***> wrote:

I confirmed that goenv install 1.18 works, I did not expect that to work. ... and it would be nice if it selected the newest 1.18 (avoids running known bugs)

On Mon, Oct 3, 2022 at 5:43 AM ChronosMasterOfAllTime < @.***> wrote:

We will need to tweak the behavior of goenv itself. I believe it uses a "0" patch version of none specified. At some point I want to make it behave like tfswitch, where running goenv without any args will try to auto switch and source the installed go version (project or global).

— Reply to this email directly, view it on GitHub #121 (comment) https://github.com/syndbg/goenv/issues/121#issuecomment-1265385172, or unsubscribe

https://github.com/notifications/unsubscribe-auth/AAACYZ5PUEDMQFTKBU7FWLTWBLIGRANCNFSM4MQUHT4A . You are receiving this because you were mentioned.Message ID: @.***>

Yep, I did see that as the default behavior in the code if no patch version is specified. We will have to do some tweaks to change that behavior to install 1.x.latest_x. I wouldn't think this is a major change. Would we want to omit beta and rc from the lookup? I typically would only want stable revisions in this case.

— Reply to this email directly, view it on GitHub https://github.com/syndbg/goenv/issues/121#issuecomment-1265673871, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACYZZMK5DG5DS53RUFMRLWBL6PBANCNFSM4MQUHT4A . You are receiving this because you were mentioned.Message ID: @.***>

grosser avatar Oct 03 '22 15:10 grosser

Need to tweak in the following places:

https://github.com/syndbg/goenv/search?q=Adding+Patch+Version

We had an idea I was discussing @ChronosMasterOfAllTime that would be a larger, more scalable change.

Using the following cURL will yield all the released versions (including beta/rc) in ascending sort order by release date.

curl -s -H "Accept: application/vnd.github+json" https://api.github.com/repos/golang/go/git/refs/tags | grep -oE "go[0-9]\\.[0-9]+\\.?([0-9]+|beta[0-9]+|rc[0-9]+)?" | sed s/go// | awk '!x[$0]++'

Using this, we can auto-generate a list of releases and URLs without having to maintain all the files in https://github.com/syndbg/goenv/tree/master/plugins/go-build/share/go-build

Thoughts? I was kinda bummed go.dev doesnt expose an API as all their pages are SSR'd.

If we want SHA verification, then we will need to do HTML parsing or keep it as-is 🙅 🤮

As for the "short-term" change...you can use the above command and replace the first part of the regex with the derived version from go.mod

For example

if go.mod has 1.19

the command becomes curl -s -H "Accept: application/vnd.github+json" https://api.github.com/repos/golang/go/git/refs/tags | grep -oE "1\\.19\\.?([0-9]+|beta[0-9]+|rc[0-9]+)?" | sed s/go// | awk '!x[$0]++'

we can grab the "latest" version in the list by adding | tail -1 to either command. We want to get to a point where if we specify goenv install latest or goenv install (outside of a project dir) it will assume latest version and install it for us

ghost avatar Oct 03 '22 17:10 ghost

I confirmed that goenv install 1.18 works, I did not expect that to work. ... and it would be nice if it selected the newest 1.18 (avoids running known bugs)

Just to show the output to make it clear what the current (goenv 2.0.0beta11) behaviour is:

❯ goenv install 1.18  
Adding patch version 0 to 1.18
Downloading go1.18.darwin-arm64.tar.gz...
-> https://golang.org/dl/go1.18.darwin-arm64.tar.gz
###################################################################################################################################################### 100.0%###################################################################################################################################################### 100.0%###################################################################################################################################################### 100.0%
Installing Go Darwin arm 1.18.0...
Installed Go Darwin arm 1.18.0 to /Users/docwhat/.local/share/goenv/versions/1.18.0
❯ goenv versions
  1.18.0
* 1.19.2 (set by /Users/docwhat/.local/share/goenv/version)

docwhat avatar Oct 10 '22 15:10 docwhat

Created PR #252 to resolve the major.minor semantic version to install latest patch available to goenv. Cheers! 🎉

ChronosMasterOfAllTime avatar Oct 12 '22 04:10 ChronosMasterOfAllTime

Sorry to revive a dead issue, but it looks like this was never actually completed.

@ChronosMasterOfAllTime the PR you submitted doesn't add support for reading go.mod, as this issue requests.

@syndbg Can we re-open this or can someone help me understand what is the best course? Thanks :)

Edit: Correction, the PR adds support through the install latest sub-command, my apologies for the confusion. However, maybe latest should be the install default then, instead of requiring the user to specify? I can open another issue, but since I've already commented here, I'll leave this for reply in the meantime. Thanks again.

cornfeedhobo avatar Feb 29 '24 12:02 cornfeedhobo

Sorry to revive a dead issue, but it looks like this was never actually completed.

@ChronosMasterOfAllTime the PR you submitted doesn't add support for reading go.mod, as this issue requests.

@syndbg Can we re-open this or can someone help me understand what is the best course? Thanks :)

Edit: Correction, the PR adds support through the install latest sub-command, my apologies for the confusion. However, maybe latest should be the install default then, instead of requiring the user to specify? I can open another issue, but since I've already commented here, I'll leave this for reply in the meantime. Thanks again.

Do you have GOENV_GOMOD_VERSION_ENABLE=1 set on your environment?

The PR I submitted handles taking the latest patch version when only major/minor is submitted to the install command.

ChronosMasterOfAllTime avatar Feb 29 '24 14:02 ChronosMasterOfAllTime

@ChronosMasterOfAllTime I did not have GOENV_GOMOD_VERSION_ENABLE=1 set. I wasn't aware that was needed. Thank you very much for pointing the way!

cornfeedhobo avatar Mar 03 '24 16:03 cornfeedhobo