From e1c7cf2c93fc2a5b9029011901d51e026c1f77f0 Mon Sep 17 00:00:00 2001 From: dror nir Date: Fri, 22 Nov 2024 16:35:14 +0200 Subject: [PATCH 1/2] workaround for using tools with no ge directive in their go.mod According to https://go.dev/ref/mod#go-mod-file-go, prior to Go 1.21, the go directive was option. This seems to not be supported in bingo. This commit only changes the fallback value used when a go.mod file does not contain the version. This semantically means that any go version should be able to compile this. I encountered the issue while trying to use bingo to install github.com/betacraft/easytags Their go.mod is completely empty, which causes a panic on if semver.MustParse(targetModParsed.GoVersion()).GreaterThan(runnable.GoVersion()) { `get.go:723` --- pkg/mod/mod.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/mod/mod.go b/pkg/mod/mod.go index 8b8d60a..5071968 100644 --- a/pkg/mod/mod.go +++ b/pkg/mod/mod.go @@ -126,7 +126,7 @@ func (mf *File) AddComment(comment string) error { } func (mf *File) GoVersion() string { if mf.m.Go == nil { - return "" + return "1.0" } return mf.m.Go.Version } From 56fffe2f814ef68a96187c009d01e8ba8b39ed9a Mon Sep 17 00:00:00 2001 From: dror nir Date: Mon, 9 Dec 2024 16:26:51 +0200 Subject: [PATCH 2/2] fix test and add doc comment --- pkg/mod/mod.go | 6 ++++++ pkg/mod/mod_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/mod/mod.go b/pkg/mod/mod.go index 5071968..636018b 100644 --- a/pkg/mod/mod.go +++ b/pkg/mod/mod.go @@ -124,6 +124,12 @@ func (mf *File) AddComment(comment string) error { return mf.flush() } + +// GoVersion returns a semver string containing the value of of the go directive. +// For example, it will return "1.2.3" if the go.mod file contains the line "go 1.2.3". +// If no go directive is found, it returns "1.0" because: +// 1. "1.0" is a valid semver string, so it's always safe to parse this value using semver.MustParse(). +// 2. The semantics of the absence of a go directive in a go.mod file means all versions of Go should be able to compile it. func (mf *File) GoVersion() string { if mf.m.Go == nil { return "1.0" diff --git a/pkg/mod/mod_test.go b/pkg/mod/mod_test.go index 4b15994..13e7bc5 100644 --- a/pkg/mod/mod_test.go +++ b/pkg/mod/mod_test.go @@ -43,7 +43,7 @@ func TestFile(t *testing.T) { testutil.Equals(t, "", p) testutil.Equals(t, "", comment) testutil.Equals(t, []string(nil), mf.Comments()) - testutil.Equals(t, "", mf.GoVersion()) + testutil.Equals(t, "1.0", mf.GoVersion()) testutil.Equals(t, 0, len(mf.RequireDirectives())) testutil.Equals(t, 0, len(mf.ReplaceDirectives())) testutil.Equals(t, 0, len(mf.ExcludeDirectives()))