Skip to content

Commit

Permalink
Merge branch 'bazelbuild:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lkassar-stripe committed Jul 9, 2024
2 parents bf69b4b + 852fdcf commit bfa2c87
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 27 deletions.
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
3 changes: 3 additions & 0 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ def _go_deps_impl(module_ctx):
continue
go_repository_args = {
"name": module.repo_name,
# Compared to the name attribute, the content of this attribute does not go through repo
# mapping.
"internal_only_do_not_use_apparent_name": module.repo_name,
"importpath": path,
"build_directives": _get_directives(path, gazelle_overrides, gazelle_default_attributes),
"build_file_generation": _get_build_file_generation(path, gazelle_overrides, gazelle_default_attributes),
Expand Down
14 changes: 7 additions & 7 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,13 @@ def _go_repository_impl(ctx):
# ctx.attr.name is the canonical name of this repository, which contains a '~' if and only
# if this repository is generated by a module extension rather than an invocation in
# WORKSPACE.
is_module_extension_repo = "~" in ctx.attr.name
is_module_extension_repo = ctx.attr.internal_only_do_not_use_apparent_name
if is_module_extension_repo:
# TODO: In Bazel 6.3.0 and earlier, there is no way to obtain a label referencing a repo
# generated by an extension from within that extension. We thus have to manually
# construct such a label pointing to the sibling `_go_repository_config` repo created by
# the `go_deps` extension. All extension-generated repos have names of the form
# `<prefix>~<name set by the extension>`.
extension_repo_prefix = ctx.attr.name.rpartition("~")[0] + "~"
# This repository is generated by the 'go_deps' extension. Since as of Bazel 7.2.0 there
# is no API that constructs a label referencing a sibling repo from within a repo rule,
# we rely on the assumption that the apparent name of the extension-generated repos is
# the last component of their canonical names.
extension_repo_prefix = ctx.attr.name[:-len(ctx.attr.internal_only_do_not_use_apparent_name)]
repo_config = ctx.path(Label("@@" + extension_repo_prefix + "bazel_gazelle_go_repository_config//:WORKSPACE"))
else:
repo_config = ctx.path(ctx.attr.build_config)
Expand Down Expand Up @@ -596,6 +595,7 @@ go_repository = repository_rule(
unexpected behavior for the given rule.
""",
),
"internal_only_do_not_use_apparent_name": attr.string(doc = "Internal usage only"),
},
)
"""See repository.md#go-repository for full documentation."""
15 changes: 6 additions & 9 deletions internal/go_repository_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ def _go_repository_config_impl(ctx):
for f in result.stdout.splitlines():
f = f.lstrip()
if len(f) > 0:
macro_label_str = "@" + ctx.attr.config.workspace_name + "//:" + f
if "~" in ctx.attr.config.workspace_name:
# The workspace name is a Bzlmod canonical repository
# name that we don't have visibility into directly.
# Instead, use a canonical label literal (starting with
# "@@") to bypass visibility checks.
macro_label_str = "@" + macro_label_str
macro_label = Label(macro_label_str)
ctx.path(macro_label)
# Reuse the repo prefix of the stringified label to use a
# canonical label literal on Bazel 6 and higher.
config_label = str(ctx.attr.config)
macro_label_prefix = config_label[:config_label.find("//")]
macro_label_str = macro_label_prefix + "//:" + f
ctx.path(Label(macro_label_str))

else:
ctx.file(
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
8 changes: 5 additions & 3 deletions repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ git_repository(
<pre>
go_repository(<a href="#go_repository-name">name</a>, <a href="#go_repository-auth_patterns">auth_patterns</a>, <a href="#go_repository-build_config">build_config</a>, <a href="#go_repository-build_directives">build_directives</a>, <a href="#go_repository-build_external">build_external</a>, <a href="#go_repository-build_extra_args">build_extra_args</a>,
<a href="#go_repository-build_file_generation">build_file_generation</a>, <a href="#go_repository-build_file_name">build_file_name</a>, <a href="#go_repository-build_file_proto_mode">build_file_proto_mode</a>, <a href="#go_repository-build_naming_convention">build_naming_convention</a>,
<a href="#go_repository-build_tags">build_tags</a>, <a href="#go_repository-canonical_id">canonical_id</a>, <a href="#go_repository-commit">commit</a>, <a href="#go_repository-debug_mode">debug_mode</a>, <a href="#go_repository-importpath">importpath</a>, <a href="#go_repository-local_path">local_path</a>, <a href="#go_repository-patch_args">patch_args</a>,
<a href="#go_repository-patch_cmds">patch_cmds</a>, <a href="#go_repository-patch_tool">patch_tool</a>, <a href="#go_repository-patches">patches</a>, <a href="#go_repository-remote">remote</a>, <a href="#go_repository-replace">replace</a>, <a href="#go_repository-repo_mapping">repo_mapping</a>, <a href="#go_repository-sha256">sha256</a>, <a href="#go_repository-strip_prefix">strip_prefix</a>,
<a href="#go_repository-sum">sum</a>, <a href="#go_repository-tag">tag</a>, <a href="#go_repository-type">type</a>, <a href="#go_repository-urls">urls</a>, <a href="#go_repository-vcs">vcs</a>, <a href="#go_repository-version">version</a>)
<a href="#go_repository-build_tags">build_tags</a>, <a href="#go_repository-canonical_id">canonical_id</a>, <a href="#go_repository-commit">commit</a>, <a href="#go_repository-debug_mode">debug_mode</a>, <a href="#go_repository-importpath">importpath</a>,
<a href="#go_repository-internal_only_do_not_use_apparent_name">internal_only_do_not_use_apparent_name</a>, <a href="#go_repository-local_path">local_path</a>, <a href="#go_repository-patch_args">patch_args</a>, <a href="#go_repository-patch_cmds">patch_cmds</a>, <a href="#go_repository-patch_tool">patch_tool</a>,
<a href="#go_repository-patches">patches</a>, <a href="#go_repository-remote">remote</a>, <a href="#go_repository-replace">replace</a>, <a href="#go_repository-repo_mapping">repo_mapping</a>, <a href="#go_repository-sha256">sha256</a>, <a href="#go_repository-strip_prefix">strip_prefix</a>, <a href="#go_repository-sum">sum</a>, <a href="#go_repository-tag">tag</a>, <a href="#go_repository-type">type</a>, <a href="#go_repository-urls">urls</a>, <a href="#go_repository-vcs">vcs</a>,
<a href="#go_repository-version">version</a>)
</pre>

`go_repository` downloads a Go project and generates build files with Gazelle
Expand Down Expand Up @@ -187,6 +188,7 @@ go_repository(
| <a id="go_repository-commit"></a>commit | If the repository is downloaded using a version control tool, this is the commit or revision to check out. With git, this would be a sha1 commit id. `commit` and `tag` may not both be set. | String | optional | `""` |
| <a id="go_repository-debug_mode"></a>debug_mode | Enables logging of fetch_repo and Gazelle output during succcesful runs. Gazelle can be noisy so this defaults to `False`. However, setting to `True` can be useful for debugging build failures and unexpected behavior for the given rule. | Boolean | optional | `False` |
| <a id="go_repository-importpath"></a>importpath | The Go import path that matches the root directory of this repository.<br><br>In module mode (when `version` is set), this must be the module path. If neither `urls` nor `remote` is specified, `go_repository` will automatically find the true path of the module, applying import path redirection.<br><br>If build files are generated for this repository, libraries will have their `importpath` attributes prefixed with this `importpath` string. | String | required | |
| <a id="go_repository-internal_only_do_not_use_apparent_name"></a>internal_only_do_not_use_apparent_name | Internal usage only | String | optional | `""` |
| <a id="go_repository-local_path"></a>local_path | If specified, `go_repository` will load the module from this local directory | String | optional | `""` |
| <a id="go_repository-patch_args"></a>patch_args | Arguments passed to the patch tool when applying patches. | List of strings | optional | `["-p0"]` |
| <a id="go_repository-patch_cmds"></a>patch_cmds | Commands to run in the repository after patches are applied. | List of strings | optional | `[]` |
Expand Down
8 changes: 6 additions & 2 deletions walk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ func (c *Configurer) loadBazelIgnore(repoRoot string, wc *walkConfig) error {
log.Printf("the .bazelignore exclusion pattern must not be a glob %s", ignore)
continue
}
// Ensure we remove trailing slashes or the exclude matching won't work correctly
wc.excludes = append(wc.excludes, strings.TrimSuffix(ignore, "/"))

// Clean the path to remove any extra '.', './' etc otherwise
// the exclude matching won't work correctly.
ignore = path.Clean(ignore)

wc.excludes = append(wc.excludes, ignore)
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions walk/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ foo/*
# Random comment followed by a line
a.file
# Paths can have a ./ prefix
./b.file
././blah/../ugly/c.file
`,
},
{Path: ".dot"}, // not ignored
Expand All @@ -228,6 +232,8 @@ a.file
{Path: "dir2/a/b"}, // ignored by .bazelignore 'dir2/a/b'
{Path: "dir3/g/h"}, // ignored by .bazelignore 'dir3/'
{Path: "a.file"}, // ignored by .bazelignore 'a.file'
{Path: "b.file"}, // ignored by .bazelignore './b.file'
{Path: "ugly/c.file"}, // ignored by .bazelignore '././blah/../ugly/c.file'
})
defer cleanup()

Expand Down

0 comments on commit bfa2c87

Please sign in to comment.