Remove rules_go version detection logic The logic is conditional on whether rules_go is loaded in version 0.23.0 or later, which is almost three years old at this point. In addition to removing quite a bit of code, getting rid of the version detection also means that we don't have to port it to Bzlmod.
diff --git a/cmd/gazelle/integration_test.go b/cmd/gazelle/integration_test.go index 3cdba9c..f91ac98 100644 --- a/cmd/gazelle/integration_test.go +++ b/cmd/gazelle/integration_test.go
@@ -4756,55 +4756,6 @@ }) } -func TestFindRulesGoVersionWithWORKSPACE(t *testing.T) { - files := []testtools.FileSpec{ - { - Path: "WORKSPACE", - Content: ` -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_go", - sha256 = "7b9bbe3ea1fccb46dcfa6c3f3e29ba7ec740d8733370e21cdc8937467b4a4349", - urls = [ - "https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.22.4/rules_go-v0.22.4.tar.gz", - "https://github.com/bazelbuild/rules_go/releases/download/v0.22.4/rules_go-v0.22.4.tar.gz", - ], -) -`, - }, - { - Path: "foo_illumos.go", - Content: ` -// illumos not supported in rules_go v0.22.4 -package foo -`, - }, - { - Path: "BUILD.bazel", - Content: ` -# gazelle:prefix example.com/foo -`, - }, - } - - dir, cleanup := testtools.CreateFiles(t, files) - defer cleanup() - - if err := runGazelle(dir, []string{"update"}); err != nil { - t.Fatal(err) - } - - testtools.CheckFiles(t, dir, []testtools.FileSpec{ - { - Path: "BUILD.bazel", - Content: ` -# gazelle:prefix example.com/foo -`, - }, - }) -} - func TestPlatformSpecificEmbedsrcs(t *testing.T) { files := []testtools.FileSpec{ {
diff --git a/cmd/generate_repo_config/generate_repo_config.go b/cmd/generate_repo_config/generate_repo_config.go index bf2f16d..ac8aaa6 100644 --- a/cmd/generate_repo_config/generate_repo_config.go +++ b/cmd/generate_repo_config/generate_repo_config.go
@@ -40,8 +40,7 @@ ) const ( - goRepoRuleKind = "go_repository" - httpArchiveRuleKind = "http_archive" + goRepoRuleKind = "go_repository" ) var ( @@ -109,20 +108,16 @@ destFile := rule.EmptyFile(configDest, "") for _, rsrc := range repos { - var rdst *rule.Rule - if rsrc.Kind() == goRepoRuleKind { - rdst = rule.NewRule(goRepoRuleKind, rsrc.Name()) - rdst.SetAttr("importpath", rsrc.AttrString("importpath")) - if namingConvention := rsrc.AttrString("build_naming_convention"); namingConvention != "" { - rdst.SetAttr("build_naming_convention", namingConvention) - } - } else if rsrc.Kind() == httpArchiveRuleKind && rsrc.Name() == "io_bazel_rules_go" { - rdst = rule.NewRule(httpArchiveRuleKind, "io_bazel_rules_go") - rdst.SetAttr("urls", rsrc.AttrStrings("urls")) + if rsrc.Kind() != goRepoRuleKind { + continue } - if rdst != nil { - rdst.Insert(destFile) + + rdst := rule.NewRule(goRepoRuleKind, rsrc.Name()) + rdst.SetAttr("importpath", rsrc.AttrString("importpath")) + if namingConvention := rsrc.AttrString("build_naming_convention"); namingConvention != "" { + rdst.SetAttr("build_naming_convention", namingConvention) } + rdst.Insert(destFile) } buf.WriteString("\n")
diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 4e687f4..ddf5877 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel
@@ -54,7 +54,6 @@ "//internal/gazellebinarytest:all_files", "//internal/generationtest:all_files", "//internal/language:all_files", - "//internal/version:all_files", "//internal/wspace:all_files", ], visibility = ["//visibility:public"],
diff --git a/internal/go_repository_tools_srcs.bzl b/internal/go_repository_tools_srcs.bzl index 77e283d..1d983cb 100644 --- a/internal/go_repository_tools_srcs.bzl +++ b/internal/go_repository_tools_srcs.bzl
@@ -40,8 +40,6 @@ Label("//internal/language/test_loads_from_flag:BUILD.bazel"), Label("//internal/language/test_loads_from_flag:lang.go"), Label("//internal:list_repository_tools_srcs.go"), - Label("//internal/version:BUILD.bazel"), - Label("//internal/version:version.go"), Label("//internal/wspace:BUILD.bazel"), Label("//internal/wspace:finder.go"), Label("//label:BUILD.bazel"),
diff --git a/internal/version/BUILD.bazel b/internal/version/BUILD.bazel deleted file mode 100644 index 2d358d8..0000000 --- a/internal/version/BUILD.bazel +++ /dev/null
@@ -1,31 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "version", - srcs = ["version.go"], - importpath = "github.com/bazelbuild/bazel-gazelle/internal/version", - visibility = ["//:__subpackages__"], -) - -go_test( - name = "version_test", - srcs = ["version_test.go"], - embed = [":version"], -) - -filegroup( - name = "all_files", - testonly = True, - srcs = [ - "BUILD.bazel", - "version.go", - "version_test.go", - ], - visibility = ["//visibility:public"], -) - -alias( - name = "go_default_library", - actual = ":version", - visibility = ["//:__subpackages__"], -)
diff --git a/internal/version/version.go b/internal/version/version.go deleted file mode 100644 index 5c56fca..0000000 --- a/internal/version/version.go +++ /dev/null
@@ -1,72 +0,0 @@ -/* Copyright 2018 The Bazel Authors. All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package version - -import ( - "fmt" - "strconv" - "strings" -) - -// Version is a tuple of non-negative integers that represents the version of -// a software package. -type Version []int - -func (v Version) String() string { - cstrs := make([]string, len(v)) - for i, cn := range v { - cstrs[i] = strconv.Itoa(cn) - } - return strings.Join(cstrs, ".") -} - -// Compare returns an integer comparing two versions lexicographically. -func (x Version) Compare(y Version) int { - n := len(x) - if len(y) < n { - n = len(y) - } - for i := 0; i < n; i++ { - cmp := x[i] - y[i] - if cmp != 0 { - return cmp - } - } - return len(x) - len(y) -} - -// ParseVersion parses a version of the form "12.34.56-abcd". Non-negative -// integer components are separated by dots. An arbitrary suffix may appear -// after '-', which is ignored. -func ParseVersion(vs string) (Version, error) { - i := strings.IndexByte(vs, '-') - if i >= 0 { - vs = vs[:i] - } - cstrs := strings.Split(vs, ".") - v := make(Version, len(cstrs)) - for i, cstr := range cstrs { - cn, err := strconv.Atoi(cstr) - if err != nil { - return nil, fmt.Errorf("could not parse version string: %q is not an integer", cstr) - } - if cn < 0 { - return nil, fmt.Errorf("could not parse version string: %q is negative", cstr) - } - v[i] = cn - } - return v, nil -}
diff --git a/internal/version/version_test.go b/internal/version/version_test.go deleted file mode 100644 index 4c7b8f6..0000000 --- a/internal/version/version_test.go +++ /dev/null
@@ -1,84 +0,0 @@ -/* Copyright 2018 The Bazel Authors. All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package version - -import "testing" - -func TestCompare(t *testing.T) { - for _, tc := range []struct { - x, y Version - want int - }{ - { - x: Version{1}, - y: Version{1}, - want: 0, - }, { - x: Version{1}, - y: Version{2}, - want: -1, - }, { - x: Version{2}, - y: Version{1}, - want: 1, - }, { - x: Version{1}, - y: Version{1, 1}, - want: -1, - }, { - x: Version{1, 1}, - y: Version{1}, - want: 1, - }, - } { - if got := tc.x.Compare(tc.y); got != tc.want { - t.Errorf("Compare(%s, %s): got %v, want %v", tc.x, tc.y, got, tc.want) - } - } -} - -func TestParseVersion(t *testing.T) { - for _, tc := range []struct { - str string - want Version - wantErr bool - }{ - { - str: "", - wantErr: true, - }, { - str: "1", - want: Version{1}, - }, { - str: "-1", - wantErr: true, - }, { - str: "0.1.2", - want: Version{0, 1, 2}, - }, { - str: "0-suffix", - want: Version{0}, - }, - } { - if got, err := ParseVersion(tc.str); tc.wantErr && err == nil { - t.Errorf("ParseVersion(%q): got %s, want error", tc.str, got) - } else if !tc.wantErr && err != nil { - t.Errorf("ParseVersion(%q): got %v, want success", tc.str, err) - } else if got.Compare(tc.want) != 0 { - t.Errorf("ParseVersion(%q): got %s, want %s", tc.str, got, tc.want) - } - } -}
diff --git a/language/go/BUILD.bazel b/language/go/BUILD.bazel index 325eb23..87ddc23 100644 --- a/language/go/BUILD.bazel +++ b/language/go/BUILD.bazel
@@ -40,7 +40,6 @@ deps = [ "//config", "//flag", - "//internal/version", "//label", "//language", "//language/proto",
diff --git a/language/go/config.go b/language/go/config.go index f46a60f..35f5220 100644 --- a/language/go/config.go +++ b/language/go/config.go
@@ -16,7 +16,6 @@ package golang import ( - "errors" "flag" "fmt" "go/build" @@ -25,27 +24,18 @@ "os" "path" "path/filepath" - "regexp" "strconv" "strings" "github.com/bazelbuild/bazel-gazelle/config" gzflag "github.com/bazelbuild/bazel-gazelle/flag" - "github.com/bazelbuild/bazel-gazelle/internal/version" "github.com/bazelbuild/bazel-gazelle/language/proto" - "github.com/bazelbuild/bazel-gazelle/repo" "github.com/bazelbuild/bazel-gazelle/rule" bzl "github.com/bazelbuild/buildtools/build" ) -var minimumRulesGoVersion = version.Version{0, 29, 0} - // goConfig contains configuration values related to Go rules. type goConfig struct { - // rulesGoVersion is the version of io_bazel_rules_go being used. Determined - // by reading go/def.bzl. May be unset if the version can't be read. - rulesGoVersion version.Version - // genericTags is a set of tags that Gazelle considers to be true. Set with // -build_tags or # gazelle:build_tags. Some tags, like gc, are always on. genericTags map[string]bool @@ -463,22 +453,6 @@ c.Exts[goName] = gc if rel == "" { - const message = `Gazelle may not be compatible with this version of rules_go. -Update io_bazel_rules_go to a newer version in your WORKSPACE file.` - var err error - gc.rulesGoVersion, err = findRulesGoVersion(c) - if c.ShouldFix { - // Only check the version when "fix" is run. Generated build files - // frequently work with older version of rules_go, and we don't want to - // nag too much since there's no way to disable this warning. - // Also, don't print a warning if the rules_go repo hasn't been fetched, - // since that's a common issue when Gazelle is run as a separate binary. - if err != nil && err != errRulesGoRepoNotFound && c.ShouldFix { - log.Printf("%v\n%s", err, message) - } else if err == nil && gc.rulesGoVersion.Compare(minimumRulesGoVersion) < 0 { - log.Printf("Found RULES_GO_VERSION %s. Minimum compatible version is %s.\n%s", gc.rulesGoVersion, minimumRulesGoVersion, message) - } - } repoNamingConvention := map[string]namingConvention{} for _, repo := range c.Repos { if repo.Kind() == "go_repository" { @@ -637,56 +611,6 @@ return values } -// findRulesGoVersion attempts to infer the version of io_bazel_rules_go. -// It can read the external directory (if bazel has fetched it), or it can -// read WORKSPACE. Neither method is completely reliable. -func findRulesGoVersion(c *config.Config) (version.Version, error) { - const message = `Gazelle may not be compatible with this version of rules_go. -Update io_bazel_rules_go to a newer version in your WORKSPACE file.` - - var vstr string - if rulesGoPath, err := repo.FindExternalRepo(c.RepoRoot, config.RulesGoRepoName); err == nil { - // Bazel has already fetched io_bazel_rules_go. We can read its version - // from //go:def.bzl. - defBzlPath := filepath.Join(rulesGoPath, "go", "def.bzl") - defBzlContent, err := ioutil.ReadFile(defBzlPath) - if err != nil { - return nil, err - } - versionRe := regexp.MustCompile(`(?m)^RULES_GO_VERSION = ['"]([0-9.]*)['"]`) - match := versionRe.FindSubmatch(defBzlContent) - if match == nil { - return nil, fmt.Errorf("RULES_GO_VERSION not found in @%s//go:def.bzl.\n%s", config.RulesGoRepoName, message) - } - vstr = string(match[1]) - } else { - // Bazel has not fetched io_bazel_rules_go. Maybe we can find it in the - // WORKSPACE file. - re := regexp.MustCompile(`github\.com/bazelbuild/rules_go/releases/download/v([0-9.]+)/`) - RepoLoop: - for _, r := range c.Repos { - if r.Kind() == "http_archive" && r.Name() == "io_bazel_rules_go" { - for _, u := range r.AttrStrings("urls") { - if m := re.FindStringSubmatch(u); m != nil { - vstr = m[1] - break RepoLoop - } - } - } - } - } - - if vstr == "" { - // Couldn't find a version. We return a specific value since this is not - // usually a useful error to report. - return nil, errRulesGoRepoNotFound - } - - return version.ParseVersion(vstr) -} - -var errRulesGoRepoNotFound = errors.New(config.RulesGoRepoName + " external repository not found") - // detectNamingConvention attempts to detect the naming convention in use by // reading build files in subdirectories of the repository root directory. //
diff --git a/language/go/fileinfo.go b/language/go/fileinfo.go index fb431b0..247b34a 100644 --- a/language/go/fileinfo.go +++ b/language/go/fileinfo.go
@@ -31,7 +31,6 @@ "unicode/utf8" "github.com/bazelbuild/bazel-gazelle/config" - "github.com/bazelbuild/bazel-gazelle/internal/version" "github.com/bazelbuild/bazel-gazelle/rule" ) @@ -382,11 +381,11 @@ // // For example, the following string: // -// a b:"c d" 'e''f' "g\"" +// a b:"c d" 'e''f' "g\"" // // Would be parsed as: // -// []string{"a", "b:c d", "ef", `g"`} +// []string{"a", "b:c d", "ef", `g"`} // // Copied from go/build.splitQuoted func splitQuoted(s string) (r []string, err error) { @@ -579,54 +578,6 @@ return tags.eval(checker) && cgoTags.eval(checker) } -// rulesGoSupportsOS returns whether the os tag is recognized by the version of -// rules_go being used. This avoids incompatibility between new versions of -// Gazelle and old versions of rules_go. -func rulesGoSupportsOS(v version.Version, os string) bool { - if len(v) == 0 { - return true - } - if v.Compare(version.Version{0, 23, 0}) < 0 && - (os == "aix" || os == "illumos") { - return false - } - return true -} - -// rulesGoSupportsArch returns whether the arch tag is recognized by the version -// of rules_go being used. This avoids incompatibility between new versions of -// Gazelle and old versions of rules_go. -func rulesGoSupportsArch(v version.Version, arch string) bool { - if len(v) == 0 { - return true - } - if v.Compare(version.Version{0, 23, 0}) < 0 && - arch == "riscv64" { - return false - } - return true -} - -// rulesGoSupportsPlatform returns whether the os and arch tag combination is -// recognized by the version of rules_go being used. This avoids incompatibility -// between new versions of Gazelle and old versions of rules_go. -func rulesGoSupportsPlatform(v version.Version, p rule.Platform) bool { - if len(v) == 0 { - return true - } - if v.Compare(version.Version{0, 23, 0}) < 0 && - (p.OS == "aix" && p.Arch == "ppc64" || - p.OS == "freebsd" && p.Arch == "arm64" || - p.OS == "illumos" && p.Arch == "amd64" || - p.OS == "linux" && p.Arch == "riscv64" || - p.OS == "netbsd" && p.Arch == "arm64" || - p.OS == "openbsd" && p.Arch == "arm64" || - p.OS == "windows" && p.Arch == "arm") { - return false - } - return true -} - // parseGoEmbed parses the text following "//go:embed" to extract the glob patterns. // It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings. // This is based on a similar function in cmd/compile/internal/gc/noder.go;
diff --git a/language/go/package.go b/language/go/package.go index 20502e8..c7e344c 100644 --- a/language/go/package.go +++ b/language/go/package.go
@@ -330,7 +330,6 @@ // performance optimization to avoid evaluating constraints repeatedly. func getPlatformStringsAddFunction(c *config.Config, info fileInfo, cgoTags *cgoTagsAndOpts) func(sb *platformStringsBuilder, ss ...string) { isOSSpecific, isArchSpecific := isOSArchSpecific(info, cgoTags) - v := getGoConfig(c).rulesGoVersion switch { case !isOSSpecific && !isArchSpecific: @@ -345,8 +344,7 @@ case isOSSpecific && !isArchSpecific: var osMatch []string for _, os := range rule.KnownOSs { - if rulesGoSupportsOS(v, os) && - checkConstraints(c, os, "", info.goos, info.goarch, info.tags, cgoTags) { + if checkConstraints(c, os, "", info.goos, info.goarch, info.tags, cgoTags) { osMatch = append(osMatch, os) } } @@ -361,8 +359,7 @@ case !isOSSpecific && isArchSpecific: var archMatch []string for _, arch := range rule.KnownArchs { - if rulesGoSupportsArch(v, arch) && - checkConstraints(c, "", arch, info.goos, info.goarch, info.tags, cgoTags) { + if checkConstraints(c, "", arch, info.goos, info.goarch, info.tags, cgoTags) { archMatch = append(archMatch, arch) } } @@ -377,8 +374,7 @@ default: var platformMatch []rule.Platform for _, platform := range rule.KnownPlatforms { - if rulesGoSupportsPlatform(v, platform) && - checkConstraints(c, platform.OS, platform.Arch, info.goos, info.goarch, info.tags, cgoTags) { + if checkConstraints(c, platform.OS, platform.Arch, info.goos, info.goarch, info.tags, cgoTags) { platformMatch = append(platformMatch, platform) } }
diff --git a/language/go/std_package_list.go b/language/go/std_package_list.go index 034c2b3..83468ab 100644 --- a/language/go/std_package_list.go +++ b/language/go/std_package_list.go
@@ -166,7 +166,6 @@ "cmd/go/internal/tool": true, "cmd/go/internal/trace": true, "cmd/go/internal/vcs": true, - "cmd/go/internal/version": true, "cmd/go/internal/vet": true, "cmd/go/internal/web": true, "cmd/go/internal/work": true,