Skip to content

Commit

Permalink
Fix go_repository configuration
Browse files Browse the repository at this point in the history
* go_repository now supports a build_config attribute. By default,
  this points to "@//:WORKSPACE", but it may reference another file
  that changes less frequently or None. This is used to set the
  -repo_config option when invoking Gazelle.
* A global default may be set via "go_repository_default_config" in
  gazelle_dependencies.
* go_repository now reads files with "cat" instead of ctx.read.

Fixes bazel-contrib#555
  • Loading branch information
jayconrod committed Jun 23, 2019
1 parent 9baa0ab commit 14b9763
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 43 deletions.
1 change: 1 addition & 0 deletions cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"//cmd/autogazelle:all_files",
"//cmd/fetch_repo:all_files",
"//cmd/gazelle:all_files",
Expand Down
4 changes: 3 additions & 1 deletion deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ load(
# Re-export go_repository . Users should get it from this file.
go_repository = _go_repository

def gazelle_dependencies(go_sdk = ""):
def gazelle_dependencies(
go_sdk = "",
go_repository_default_config = "@//:WORKSPACE"):
if go_sdk:
go_repository_cache(
name = "bazel_gazelle_go_repository_cache",
Expand Down
3 changes: 3 additions & 0 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports_files(
[
"gazelle.bash.in",
"list_repository_tools_srcs.go",
"repository_rules_test_errors.patch",
],
visibility = ["//visibility:public"],
)
Expand All @@ -28,6 +29,8 @@ filegroup(
"gazelle.bash.in",
"gazelle_binary.bzl",
"go_repository.bzl",
"go_repository_cache.bzl",
"go_repository_tools.bzl",
"list_repository_tools_srcs.go",
"overlay_repository.bzl",
"repository_rules_test_errors.patch",
Expand Down
33 changes: 15 additions & 18 deletions internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def _go_repository_impl(ctx):
# go_repository rules to be invalidated when they change. Gazelle's cache
# should NOT be invalidated, so we shouldn't need to download these again.
# TODO(#549): vcs repositories are not cached and still need to be fetched.
workspace_label = Label("@//:WORKSPACE")
workspace_path = ctx.path(workspace_label)
for label in _find_macro_file_labels(ctx, workspace_label):
ctx.path(label)
config_path = None
if ctx.attr.build_config:
config_path = ctx.path(ctx.attr.build_config)
for label in _find_macro_file_labels(ctx, ctx.attr.build_config):
ctx.path(label)

# Download the repository or module.
fetch_repo_args = None
Expand Down Expand Up @@ -137,9 +138,9 @@ def _go_repository_impl(ctx):
"fix",
"-repo_root",
ctx.path(""),
"-repo_config",
str(workspace_path),
]
if config_path:
cmd.extend(["-repo_config", str(config_path)])
if ctx.attr.version:
cmd.append("-go_experimental_module_mode")
if ctx.attr.build_file_name:
Expand Down Expand Up @@ -225,6 +226,7 @@ go_repository = repository_rule(
],
),
"build_extra_args": attr.string_list(),
"build_config": attr.label(default = "@//:WORKSPACE"),

# Patches to apply after running gazelle.
"patches": attr.label_list(),
Expand Down Expand Up @@ -267,18 +269,13 @@ def _find_macro_file_labels(ctx, label):
seen = {}
files = []

if "read" in dir(ctx):
# TODO(jayconrod): not supported in Bazel 0.23.0. Use directly when
# minimum version of Bazel supports this.
content = ctx.read(label)
result = ctx.execute(["cat", str(ctx.path(label))])
if result.return_code == 0:
content = result.stdout
else:
result = ctx.execute(["cat", str(ctx.path(label))])
if result.return_code == 0:
content = result.stdout
else:
# TODO(jayconrod): "type" might work on Windows, but I think
# it's a shell builtin, and I'm not sure if ctx.execute will work.
content = ""
# TODO(jayconrod): "type" might work on Windows, but I think
# it's a shell builtin, and I'm not sure if ctx.execute will work.
content = ""

lines = content.split("\n")
for line in lines:
Expand All @@ -300,7 +297,7 @@ def _find_macro_file_labels(ctx, label):
if i < 0:
continue
line = line[:i].lstrip()
macro_label = Label("@//:" + line)
macro_label = Label("@" + label.workspace_name + "//:" + line)
if macro_label not in seen:
seen[macro_label] = None
files.append(macro_label)
Expand Down
30 changes: 8 additions & 22 deletions internal/go_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,11 @@ import (
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

const mainWorkspace = `
-- WORKSPACE --
local_repository(
name = "io_bazel_rules_go",
path = "../io_bazel_rules_go",
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains(go_version = "host")
local_repository(
name = "bazel_gazelle",
path = "../bazel_gazelle",
)
var testArgs = bazel_testing.Args{
Main: `
-- BUILD.bazel --
`,
WorkspaceSuffix: `
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
gazelle_dependencies()
Expand All @@ -57,12 +44,11 @@ go_repository(
version = "v0.8.1",
sum ="h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
)
-- BUILD.bazel --
`
`,
}

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{Main: mainWorkspace})
bazel_testing.TestMain(m, testArgs)
}

func TestBuild(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/language/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
filegroup(
name = "all_files",
testonly = True,
srcs = ["//internal/language/test_filegroup:all_files"],
srcs = [
"BUILD.bazel",
"//internal/language/test_filegroup:all_files",
],
visibility = ["//visibility:public"],
)
2 changes: 1 addition & 1 deletion internal/language/test_filegroup/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (*testFilegroupLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *r
var kinds = map[string]rule.KindInfo{
"filegroup": {
NonEmptyAttrs: map[string]bool{"srcs": true, "deps": true},
MergeableAttrs: map[string]bool{"deps": true},
MergeableAttrs: map[string]bool{"srcs": true},
},
}

Expand Down
15 changes: 15 additions & 0 deletions repository.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ returned by ``go env GOPATH``.
| mode, Gazelle will run if there is no build file in the repository root |
| directory. |
+--------------------------------+----------------------+-------------------------------------------------+
| :param:`build_config` | :type:`label` | :value:`@//:WORKSPACE` |
+--------------------------------+----------------------+-------------------------------------------------+
| A file that Gazelle should read to learn about external repositories before |
| generating build files. This is useful for dependency resolution. For example, |
| a ``go_repository`` rule in this file establishes a mapping between a |
| repository name like ``golang.org/x/tools`` and a workspace name like |
| ``org_golang_x_tools``. Workspace directives like |
| ``# gazelle:repository_macro`` are recognized. |
| |
| By default, Gazelle reads the WORKSPACE file in the main workspace. This |
| means that ``go_repository`` rules are re-evaluated when WORKSPACE changes. |
| Their content should still be fetched from a local cache, but build files |
| will be regenerated. If this is not desirable, ``build_config`` may be set |
| to a less frequently updated file or ``None`` to disable this functionality. |
+--------------------------------+----------------------+-------------------------------------------------+
| :param:`build_file_name` | :type:`string` | :value:`BUILD.bazel,BUILD` |
+--------------------------------+----------------------+-------------------------------------------------+
| Comma-separated list of names Gazelle will consider to be build files. |
Expand Down

0 comments on commit 14b9763

Please sign in to comment.