Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore compatibility with Go 1.18 #1833

Merged
merged 2 commits into from
Jun 21, 2024
Merged
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
8 changes: 7 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_gazelle_is_bazel_module//:defs.bzl", "GAZELLE_IS_BAZEL_MODULE")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_rules_go//go:def.bzl", "nogo")
load("@io_bazel_rules_go//go:def.bzl", "go_cross_binary", "nogo")
load("//:def.bzl", "gazelle", "gazelle_binary")

# gazelle:prefix github.com/bazelbuild/bazel-gazelle
Expand Down Expand Up @@ -38,6 +38,12 @@ gazelle_binary(
],
)

go_cross_binary(
name = "gazelle_local_go1.18",
sdk_version = "1.18",
target = ":gazelle_local",
)

nogo(
name = "nogo",
vet = True,
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependenc
bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True, repo_name = "io_bazel_stardoc")

go_sdk_dev = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk", dev_dependency = True)
go_sdk_dev.download(version = "1.22.4")

# Used by compatibility tests, keep as low as possible.
go_sdk_dev.download(version = "1.18.10")

# Known to exist since it is instantiated by rules_go itself.
use_repo(
Expand Down
9 changes: 7 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

Expand All @@ -43,9 +43,14 @@ go_register_toolchains(
version = "1.22.0",
)

go_download_sdk(
name = "go_compat_sdk",
version = "1.18.10",
)

load("//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()
gazelle_dependencies(go_sdk = "go_sdk")

# For API doc generation
# This is a dev dependency, users should not need to install it
Expand Down
11 changes: 10 additions & 1 deletion cmd/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library", "go_test")

go_library(
name = "fetch_repo_lib",
srcs = [
"clean.go",
"copy_tree.go",
"errorscompat.go",
"fetch_repo.go",
"go_mod_download.go",
"module.go",
Expand All @@ -25,6 +26,13 @@ go_binary(
visibility = ["//visibility:public"],
)

# Verify that fetch_repo builds with Go 1.18.
go_cross_binary(
name = "fetch_repo_go1.18",
sdk_version = "1.18",
target = ":fetch_repo",
)

go_test(
name = "fetch_repo_test",
srcs = ["fetch_repo_test.go"],
Expand All @@ -39,6 +47,7 @@ filegroup(
"BUILD.bazel",
"clean.go",
"copy_tree.go",
"errorscompat.go",
"fetch_repo.go",
"fetch_repo_test.go",
"go_mod_download.go",
Expand Down
61 changes: 61 additions & 0 deletions cmd/fetch_repo/errorscompat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Taken from
// https://github.com/golang/go/blob/20b79fd5775c39061d949569743912ad5e58b0e7/src/errors/join.go
// TODO: Remove when Go 1.20 is the minimum supported version.

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

// Join returns an error that wraps the given errors.
// Any nil error values are discarded.
// Join returns nil if every value in errs is nil.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// A non-nil error returned by Join implements the Unwrap() []error method.
func Join(errs ...error) error {
n := 0
for _, err := range errs {
if err != nil {
n++
}
}
if n == 0 {
return nil
}
e := &joinError{
errs: make([]error, 0, n),
}
for _, err := range errs {
if err != nil {
e.errs = append(e.errs, err)
}
}
return e
}

type joinError struct {
errs []error
}

func (e *joinError) Error() string {
// Since Join returns nil if every value in errs is nil,
// e.errs cannot be empty.
if len(e.errs) == 1 {
return e.errs[0].Error()
}

b := []byte(e.errs[0].Error())
for _, err := range e.errs[1:] {
b = append(b, '\n')
b = append(b, err.Error()...)
}
return string(b)
}

func (e *joinError) Unwrap() []error {
return e.errs
}
2 changes: 1 addition & 1 deletion cmd/fetch_repo/go_mod_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func runGoModDownload(dl *GoModDownloadResult, dest string, importpath string, v
return fmt.Errorf("go mod download output format: `%s %s`: parsing JSON: %q error: %w", cmd.Path, strings.Join(cmd.Args, " "), buf.String(), err)
}
if dl.Error != "" {
return errors.Join(errors.New(dl.Error), dlErr)
return Join(errors.New(dl.Error), dlErr)
}
if dlErr != nil {
return dlErr
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/bazelbuild/bazel-gazelle

go 1.21.4
go 1.21

require (
github.com/bazelbuild/buildtools v0.0.0-20240313121412-66c605173954
Expand Down
1 change: 1 addition & 0 deletions internal/go_repository_tools_srcs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GO_REPOSITORY_TOOLS_SRCS = [
Label("//cmd/fetch_repo:BUILD.bazel"),
Label("//cmd/fetch_repo:clean.go"),
Label("//cmd/fetch_repo:copy_tree.go"),
Label("//cmd/fetch_repo:errorscompat.go"),
Label("//cmd/fetch_repo:fetch_repo.go"),
Label("//cmd/fetch_repo:go_mod_download.go"),
Label("//cmd/fetch_repo:module.go"),
Expand Down