Skip to content

Commit a783ea5

Browse files
authored
Fixup importpath (#1209)
* Fixup importpath Infer test import path correctly from deps Remove importpath from all go_binary and go_test rules Add importpath to all go_library rules Delete go_prefix occurences Progress on #721 * Review fixes
1 parent 8533245 commit a783ea5

File tree

47 files changed

+88
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+88
-24
lines changed

BUILD.bazel

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_prefix", "go_path", "go_vet_test")
1+
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_path", "go_vet_test")
22
load("@io_bazel_rules_go//go/private:tools/lines_sorted_test.bzl", "lines_sorted_test")
33
load("@io_bazel_rules_go//go/private:rules/info.bzl", "go_info")
44
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_google_protobuf")
@@ -16,9 +16,6 @@ go_context_data(
1616

1717
# gazelle:prefix github.com/bazelbuild/rules_go
1818

19-
# TODO(jayconrod): remove when nothing depends on this.
20-
go_prefix("github.com/bazelbuild/rules_go")
21-
2219
go_google_protobuf()
2320

2421
lines_sorted_test(

examples/bindata/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bindata(
99

1010
go_library(
1111
name = "go_default_library",
12+
importpath = "github.com/bazelbuild/rules_go/examples/bindata",
1213
srcs = [":data"],
1314
)
1415

examples/cgo/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"use_exported.c",
1515
"use_exported.h",
1616
],
17+
importpath = "github.com/bazelbuild/rules_go/examples/cgo",
1718
cdeps = ["//examples/cgo/cc_dependency:version"],
1819
cgo = True,
1920
clinkopts = ["-lm"],
@@ -31,6 +32,7 @@ go_library(
3132
cgo = True,
3233
clinkopts = ["-lm"],
3334
visibility = ["//visibility:private"],
35+
importpath = "github.com/bazelbuild/rules_go/examples/cgo/sub",
3436
)
3537

3638
go_test(

examples/cgo/skip_go_library/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ go_library(
99
"types.go",
1010
],
1111
cgo = True,
12+
importpath = "github.com/bazelbuild/rules_go/examples/cgo/skip_go_library",
1213
)

examples/lib/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"lib.go",
1313
"sub.s",
1414
],
15+
importpath = "github.com/bazelbuild/rules_go/examples/lib",
1516
deps = ["//examples/lib/deep:go_default_library"],
1617
)
1718

examples/lib/deep/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ go_library(
88
"doc.go",
99
"thought.go",
1010
],
11+
importpath = "github.com/bazelbuild/rules_go/examples/lib/deep",
1112
)

examples/proto/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ go_test(
1515
name = "proto_test",
1616
size = "small",
1717
srcs = ["proto_test.go"],
18-
importpath = "github.com/bazelbuild/rules_go/examples/proto",
1918
pure = "off",
2019
deps = [
2120
"//examples/proto/embed:go_default_library",
@@ -27,7 +26,6 @@ go_test(
2726
name = "proto_pure_test",
2827
size = "small",
2928
srcs = ["proto_test.go"],
30-
importpath = "github.com/bazelbuild/rules_go/examples/proto",
3129
pure = "on",
3230
deps = [
3331
"//examples/proto/embed:go_default_library",

examples/stamped_bin/stamp/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
33
go_library(
44
name = "go_default_library",
55
srcs = ["stamp.go"],
6+
importpath = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp",
67
visibility = ["//visibility:public"],
78
x_defs = {
89
"XdefBuildTimestamp": "{BUILD_TIMESTAMP}",

examples/vendor/github.com/user/vendored/BUILD.bazel

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/private/common.bzl

+3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ c_exts = [
5353
go_filetype = FileType(go_exts + asm_exts)
5454
cc_hdr_filetype = FileType(hdr_exts)
5555

56+
auto_importpath = "~auto~"
57+
test_library_suffix = "~library~"
58+
5659
# Extensions of files we can build with the Go compiler or with cc_library.
5760
# This is a subset of the extensions recognized by go/build.
5861
cgo_filetype = FileType(go_exts + asm_exts + c_exts)

go/private/context.bzl

+21-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ load("@io_bazel_rules_go//go/private:common.bzl",
3030
"structs",
3131
"goos_to_extension",
3232
"as_iterable",
33+
"auto_importpath",
34+
"test_library_suffix",
3335
)
3436

3537
GoContext = provider()
@@ -121,14 +123,30 @@ def _infer_importpath(ctx):
121123
VENDOR_PREFIX = "/vendor/"
122124
# Check if import path was explicitly set
123125
path = getattr(ctx.attr, "importpath", "")
126+
# are we in forced infer mode?
127+
if path == auto_importpath:
128+
path = ""
124129
if path != "":
125130
return path, EXPLICIT_PATH
126131
# See if we can collect importpath from embeded libraries
127132
# This is the path that fixes tests as well
128133
for embed in getattr(ctx.attr, "embed", []):
129-
if GoLibrary in embed:
130-
if embed[GoLibrary].pathtype == EXPLICIT_PATH:
131-
return embed[GoLibrary].importpath, EXPLICIT_PATH
134+
if GoLibrary not in embed:
135+
continue
136+
if embed[GoLibrary].pathtype == EXPLICIT_PATH:
137+
return embed[GoLibrary].importpath, EXPLICIT_PATH
138+
# If we are a test, and we have a dep in the same package, presume
139+
# we should be named the same with an _test suffix
140+
if ctx.label.name.endswith("_test" + test_library_suffix):
141+
for dep in getattr(ctx.attr, "deps", []):
142+
if GoLibrary not in dep:
143+
continue
144+
lib = dep[GoLibrary]
145+
if lib.label.workspace_root != ctx.label.workspace_root:
146+
continue
147+
if lib.label.package != ctx.label.package:
148+
continue
149+
return lib.importpath + "_test", INFERRED_PATH
132150
# TODO: stop using the prefix
133151
prefix = getattr(ctx.attr, "_go_prefix", None)
134152
path = prefix.go_prefix if prefix else ""

go/private/rules/wrappers.bzl

+16-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ load("@io_bazel_rules_go//go/private:rules/binary.bzl", "go_binary")
1616
load("@io_bazel_rules_go//go/private:rules/library.bzl", "go_library")
1717
load("@io_bazel_rules_go//go/private:rules/test.bzl", "go_test")
1818
load("@io_bazel_rules_go//go/private:rules/cgo.bzl", "setup_cgo_library")
19+
load("@io_bazel_rules_go//go/private:common.bzl", "auto_importpath", "test_library_suffix")
1920

20-
def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], clinkopts=[], library=None, **kwargs):
21+
def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], clinkopts=[], importpath="", library=None, **kwargs):
2122
"""See go/core.rst#go_library for full documentation."""
2223
if library:
2324
#TODO: print("\nDEPRECATED: {}//{}:{} : the library attribute is deprecated. Please migrate to embed.".format(native.repository_name(), native.package_name(), name))
@@ -37,16 +38,19 @@ def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], c
3738
name = name,
3839
srcs = srcs,
3940
embed = embed,
41+
importpath = importpath,
4042
**kwargs
4143
)
4244

45+
#TODO(#1207): Remove importpath
4346
def go_binary_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], clinkopts=[], library=None, importpath="", **kwargs):
4447
"""See go/core.rst#go_binary for full documentation."""
4548
if library:
4649
#TODO: print("\nDEPRECATED: {}//{}:{} : the library attribute is deprecated. Please migrate to embed.".format(native.repository_name(), native.package_name(), name))
4750
embed = embed + [library]
48-
#if importpath:
49-
# print("\nDEPRECATED: {}//{}:{} : the importpath attribute on go_binary is deprecated.".format(native.repository_name(), native.package_name(), name))
51+
#TODO: Turn on the deprecation warning when gazelle stops adding these
52+
#if importpath and native.repository_name() == "@":
53+
# print("\nDEPRECATED: //{}:{} : the importpath attribute on go_binary is deprecated.".format(native.package_name(), name))
5054

5155
if cgo:
5256
cgo_embed = setup_cgo_library(
@@ -65,15 +69,20 @@ def go_binary_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], cl
6569
**kwargs
6670
)
6771

68-
def go_test_macro(name, srcs=None, deps=None, importpath="", library=None, embed=[], gc_goopts=[], cgo=False, cdeps=[], copts=[], clinkopts=[], x_defs={}, **kwargs):
72+
#TODO(#1207): Remove importpath
73+
def go_test_macro(name, srcs=None, deps=None, importpath=None, library=None, embed=[], gc_goopts=[], cgo=False, cdeps=[], copts=[], clinkopts=[], x_defs={}, **kwargs):
6974
"""See go/core.rst#go_test for full documentation."""
7075
if library:
7176
#TODO: print("\nDEPRECATED: {}//{}:{} : the library attribute is deprecated. Please migrate to embed.".format(native.repository_name(), native.package_name(), name))
7277
embed = embed + [library]
73-
#TODO: if importpath:
74-
# print("\nDEPRECATED: {}//{}:{} : the importpath attribute on go_test is deprecated.".format(native.repository_name(), native.package_name(), name))
78+
if not importpath:
79+
importpath = auto_importpath
80+
#TODO: Turn on the deprecation warning when gazelle stops adding these
81+
#elif native.repository_name() == "@":
82+
# print("\nDEPRECATED: //{}:{} : the importpath attribute on go_test is deprecated.".format(native.package_name(), name))
83+
7584

76-
library_name = name + "~library~"
85+
library_name = name + test_library_suffix
7786
go_library_macro(
7887
name = library_name,
7988
visibility = ["//visibility:private"],

go/tools/bazel/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
33
go_library(
44
name = "go_default_library",
55
srcs = ["bazel.go"],
6+
importpath = "github.com/bazelbuild/rules_go/go/tools/bazel",
67
visibility = ["//visibility:public"],
78
)
89

go/tools/fetch_repo/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_binary(
99
go_library(
1010
name = "go_default_library",
1111
srcs = ["main.go"],
12+
importpath = "github.com/bazelbuild/rules_go/go/tools/fetch_repo",
1213
visibility = ["//visibility:private"],
1314
deps = ["@org_golang_x_tools//go/vcs:go_default_library"],
1415
)

go/tools/gazelle/config/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
"directives.go",
99
"platform.go",
1010
],
11+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/config",
1112
visibility = ["//visibility:public"],
1213
deps = ["@com_github_bazelbuild_buildtools//build:go_default_library"],
1314
)

go/tools/gazelle/gazelle/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"main.go",
1010
"print.go",
1111
],
12+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/gazelle",
1213
deps = [
1314
"//go/tools/gazelle/config:go_default_library",
1415
"//go/tools/gazelle/merger:go_default_library",

go/tools/gazelle/merger/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
"fix.go",
77
"merger.go",
88
],
9+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/merger",
910
visibility = ["//visibility:public"],
1011
deps = [
1112
"@com_github_bazelbuild_buildtools//build:go_default_library",

go/tools/gazelle/packages/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
"package.go",
1111
"walk.go",
1212
],
13+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/packages",
1314
visibility = ["//visibility:public"],
1415
deps = [
1516
"//go/tools/gazelle/config:go_default_library",

go/tools/gazelle/resolve/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
"resolve_vendored.go",
1212
"std_package_list.go",
1313
],
14+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/resolve",
1415
visibility = ["//visibility:public"],
1516
deps = [
1617
"@com_github_bazelbuild_buildtools//build:go_default_library",

go/tools/gazelle/rules/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
"generator.go",
99
"sort_labels.go",
1010
],
11+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/rules",
1112
visibility = ["//visibility:public"],
1213
deps = [
1314
"//go/tools/gazelle/config:go_default_library",

go/tools/gazelle/wspace/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
33
go_library(
44
name = "go_default_library",
55
srcs = ["finder.go"],
6+
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/wspace",
67
visibility = ["//visibility:public"],
78
)
89

tests/asm_include/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_library(
2525
":darwin_amd64": LIB_AMD64_SRCS,
2626
"//conditions:default": LIB_OTHER_SRCS,
2727
}),
28+
importpath = "github.com/bazelbuild/rules_go/tests/asm_include",
2829
)
2930

3031
go_test(

tests/build_constraints/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ go_library(
2727
":c_srcs_group",
2828
],
2929
cgo = True,
30+
importpath = "github.com/bazelbuild/rules_go/tests/build_constraints",
3031
)
3132

3233
filegroup(

tests/cgo_filtered/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ go_library(
1111
name = "go_default_library",
1212
srcs = ["pure.go"],
1313
cgo = True,
14+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_filtered",
1415
)

tests/cgo_library_root_dir/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
"foo.c",
1919
],
2020
cgo = True,
21+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_library_root_dir",
2122
tags = ["manual"],
2223
)
2324

tests/cgo_multi_dir/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ go_library(
1818
"foo/foo.go",
1919
],
2020
cgo = True,
21+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_multi_dir",
2122
)

tests/cgo_opts/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ go_test(
1313
name = "go_default_test",
1414
srcs = ["cgo_opts_test.go"],
1515
embed = [":go_default_library"],
16-
importpath = "github.com/bazelbuild/rules_go/tests/cgo_opts",
1716
)

tests/cgo_pthread_flag/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_library(
44
name = "go_default_library",
55
srcs = ["cgo_pthread_flag.go"],
66
cgo = True,
7+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_pthread_flag",
78
)
89

910
go_test(

tests/cgo_pure/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"pure.go",
1010
],
1111
cgo = True,
12+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_pure",
1213
)
1314

1415
go_test(

tests/cgo_select/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_library(
2525
],
2626
}),
2727
cgo = True,
28+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_select",
2829
)
2930

3031
cc_library(

tests/cgo_sys_hdr/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
"sub/foo.h",
88
],
99
cgo = True,
10+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_sys_hdr",
1011
)
1112

1213
go_test(

tests/cgo_trans_deps/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_library(
44
name = "go_default_library",
55
srcs = ["cgo_lib.go"],
66
cgo = True,
7+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_trans_deps",
78
deps = ["//tests/cgo_trans_deps/dep:go_default_library"],
89
)
910

tests/cgo_trans_deps/dep/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
33
go_library(
44
name = "go_default_library",
55
srcs = ["dep.go"],
6+
importpath = "github.com/bazelbuild/rules_go/tests/cgo_trans_deps/dep",
67
visibility = ["//visibility:public"],
78
)

tests/coverage/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_test(
1414
go_library(
1515
name = "go_default_library",
1616
srcs = ["lib.go"],
17+
importpath = "github.com/bazelbuild/rules_go/tests/coverage",
1718
)
1819

1920
bazel_test(
@@ -37,6 +38,7 @@ go_binary(
3738
go_library(
3839
name = "bin_lib",
3940
srcs = ["bin_lib.go"],
41+
importpath = "github.com/bazelbuild/rules_go/tests/coverage",
4042
)
4143

4244
bazel_test(

tests/empty_package/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ go_test(
2424
name = "empty_package_cgo",
2525
size = "small",
2626
srcs = ["empty_package_test.go"],
27-
importpath = "github.com/bazelbuild/rules_go/tests/empty_package_test",
2827
pure = "off",
2928
x_defs = {
3029
"Expect": "2",
@@ -36,7 +35,6 @@ go_test(
3635
name = "empty_package_pure",
3736
size = "small",
3837
srcs = ["empty_package_test.go"],
39-
importpath = "github.com/bazelbuild/rules_go/tests/empty_package_test",
4038
pure = "on",
4139
x_defs = {
4240
"Expect": "1",

0 commit comments

Comments
 (0)