Skip to content

Commit

Permalink
Fixup importpath
Browse files Browse the repository at this point in the history
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 bazel-contrib#721
  • Loading branch information
ianthehat committed Jan 3, 2018
1 parent 8533245 commit 2f8bcdc
Show file tree
Hide file tree
Showing 46 changed files with 81 additions and 23 deletions.
5 changes: 1 addition & 4 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_prefix", "go_path", "go_vet_test")
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_path", "go_vet_test")
load("@io_bazel_rules_go//go/private:tools/lines_sorted_test.bzl", "lines_sorted_test")
load("@io_bazel_rules_go//go/private:rules/info.bzl", "go_info")
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_google_protobuf")
Expand All @@ -16,9 +16,6 @@ go_context_data(

# gazelle:prefix github.com/bazelbuild/rules_go

# TODO(jayconrod): remove when nothing depends on this.
go_prefix("github.com/bazelbuild/rules_go")

go_google_protobuf()

lines_sorted_test(
Expand Down
1 change: 1 addition & 0 deletions examples/bindata/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bindata(

go_library(
name = "go_default_library",
importpath = "github.com/bazelbuild/rules_go/examples/bindata",
srcs = [":data"],
)

Expand Down
2 changes: 2 additions & 0 deletions examples/cgo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"use_exported.c",
"use_exported.h",
],
importpath = "github.com/bazelbuild/rules_go/examples/cgo",
cdeps = ["//examples/cgo/cc_dependency:version"],
cgo = True,
clinkopts = ["-lm"],
Expand All @@ -31,6 +32,7 @@ go_library(
cgo = True,
clinkopts = ["-lm"],
visibility = ["//visibility:private"],
importpath = "github.com/bazelbuild/rules_go/examples/cgo/sub",
)

go_test(
Expand Down
1 change: 1 addition & 0 deletions examples/cgo/skip_go_library/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ go_library(
"types.go",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/examples/cgo/skip_go_library",
)
1 change: 1 addition & 0 deletions examples/lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"lib.go",
"sub.s",
],
importpath = "github.com/bazelbuild/rules_go/examples/lib",
deps = ["//examples/lib/deep:go_default_library"],
)

Expand Down
1 change: 1 addition & 0 deletions examples/lib/deep/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ go_library(
"doc.go",
"thought.go",
],
importpath = "github.com/bazelbuild/rules_go/examples/lib/deep",
)
2 changes: 0 additions & 2 deletions examples/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ go_test(
name = "proto_test",
size = "small",
srcs = ["proto_test.go"],
importpath = "github.com/bazelbuild/rules_go/examples/proto",
pure = "off",
deps = [
"//examples/proto/embed:go_default_library",
Expand All @@ -27,7 +26,6 @@ go_test(
name = "proto_pure_test",
size = "small",
srcs = ["proto_test.go"],
importpath = "github.com/bazelbuild/rules_go/examples/proto",
pure = "on",
deps = [
"//examples/proto/embed:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions examples/stamped_bin/stamp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["stamp.go"],
importpath = "github.com/bazelbuild/rules_go/examples/stamped_bin/stamp",
visibility = ["//visibility:public"],
x_defs = {
"XdefBuildTimestamp": "{BUILD_TIMESTAMP}",
Expand Down
1 change: 1 addition & 0 deletions examples/vendor/github.com/user/vendored/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 19 additions & 3 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,30 @@ def _infer_importpath(ctx):
VENDOR_PREFIX = "/vendor/"
# Check if import path was explicitly set
path = getattr(ctx.attr, "importpath", "")
# are we in forced infer mode?
if path == "~auto~":
path = ""
if path != "":
return path, EXPLICIT_PATH
# See if we can collect importpath from embeded libraries
# This is the path that fixes tests as well
for embed in getattr(ctx.attr, "embed", []):
if GoLibrary in embed:
if embed[GoLibrary].pathtype == EXPLICIT_PATH:
return embed[GoLibrary].importpath, EXPLICIT_PATH
if GoLibrary not in embed:
continue
if embed[GoLibrary].pathtype == EXPLICIT_PATH:
return embed[GoLibrary].importpath, EXPLICIT_PATH
# If we are a test, and we have a dep in the same package, presume
# we should be named the same with an _test suffix
if ctx.label.name.endswith("_test~library~"):
for dep in getattr(ctx.attr, "deps", []):
if GoLibrary not in dep:
continue
lib = dep[GoLibrary]
if lib.label.workspace_root != ctx.label.workspace_root:
continue
if lib.label.package != ctx.label.package:
continue
return lib.importpath + "_test", INFERRED_PATH
# TODO: stop using the prefix
prefix = getattr(ctx.attr, "_go_prefix", None)
path = prefix.go_prefix if prefix else ""
Expand Down
20 changes: 14 additions & 6 deletions go/private/rules/wrappers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ load("@io_bazel_rules_go//go/private:rules/library.bzl", "go_library")
load("@io_bazel_rules_go//go/private:rules/test.bzl", "go_test")
load("@io_bazel_rules_go//go/private:rules/cgo.bzl", "setup_cgo_library")

def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], clinkopts=[], library=None, **kwargs):
def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], clinkopts=[], importpath="", library=None, **kwargs):
"""See go/core.rst#go_library for full documentation."""
if library:
#TODO: print("\nDEPRECATED: {}//{}:{} : the library attribute is deprecated. Please migrate to embed.".format(native.repository_name(), native.package_name(), name))
Expand All @@ -37,16 +37,19 @@ def go_library_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], c
name = name,
srcs = srcs,
embed = embed,
importpath = importpath,
**kwargs
)

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

if cgo:
cgo_embed = setup_cgo_library(
Expand All @@ -65,13 +68,18 @@ def go_binary_macro(name, srcs=None, embed=[], cgo=False, cdeps=[], copts=[], cl
**kwargs
)

def go_test_macro(name, srcs=None, deps=None, importpath="", library=None, embed=[], gc_goopts=[], cgo=False, cdeps=[], copts=[], clinkopts=[], x_defs={}, **kwargs):
#TODO(#1207): Remove importpath
def go_test_macro(name, srcs=None, deps=None, importpath=None, library=None, embed=[], gc_goopts=[], cgo=False, cdeps=[], copts=[], clinkopts=[], x_defs={}, **kwargs):
"""See go/core.rst#go_test for full documentation."""
if library:
#TODO: print("\nDEPRECATED: {}//{}:{} : the library attribute is deprecated. Please migrate to embed.".format(native.repository_name(), native.package_name(), name))
embed = embed + [library]
#TODO: if importpath:
# print("\nDEPRECATED: {}//{}:{} : the importpath attribute on go_test is deprecated.".format(native.repository_name(), native.package_name(), name))
if not importpath:
importpath = "~auto~"
#TODO: Turn on the deprecation warning when gazelle stops adding these
#elif native.repository_name() == "@":
# print("\nDEPRECATED: //{}:{} : the importpath attribute on go_test is deprecated.".format(native.package_name(), name))


library_name = name + "~library~"
go_library_macro(
Expand Down
1 change: 1 addition & 0 deletions go/tools/bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["bazel.go"],
importpath = "github.com/bazelbuild/rules_go/go/tools/bazel",
visibility = ["//visibility:public"],
)

Expand Down
1 change: 1 addition & 0 deletions go/tools/fetch_repo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_binary(
go_library(
name = "go_default_library",
srcs = ["main.go"],
importpath = "github.com/bazelbuild/rules_go/go/tools/fetch_repo",
visibility = ["//visibility:private"],
deps = ["@org_golang_x_tools//go/vcs:go_default_library"],
)
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
"directives.go",
"platform.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/config",
visibility = ["//visibility:public"],
deps = ["@com_github_bazelbuild_buildtools//build:go_default_library"],
)
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"main.go",
"print.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/gazelle",
deps = [
"//go/tools/gazelle/config:go_default_library",
"//go/tools/gazelle/merger:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/merger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"fix.go",
"merger.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/merger",
visibility = ["//visibility:public"],
deps = [
"@com_github_bazelbuild_buildtools//build:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"package.go",
"walk.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/packages",
visibility = ["//visibility:public"],
deps = [
"//go/tools/gazelle/config:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/resolve/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"resolve_vendored.go",
"std_package_list.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/resolve",
visibility = ["//visibility:public"],
deps = [
"@com_github_bazelbuild_buildtools//build:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
"generator.go",
"sort_labels.go",
],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/rules",
visibility = ["//visibility:public"],
deps = [
"//go/tools/gazelle/config:go_default_library",
Expand Down
1 change: 1 addition & 0 deletions go/tools/gazelle/wspace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["finder.go"],
importpath = "github.com/bazelbuild/rules_go/go/tools/gazelle/wspace",
visibility = ["//visibility:public"],
)

Expand Down
1 change: 1 addition & 0 deletions tests/asm_include/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
":darwin_amd64": LIB_AMD64_SRCS,
"//conditions:default": LIB_OTHER_SRCS,
}),
importpath = "github.com/bazelbuild/rules_go/tests/asm_include",
)

go_test(
Expand Down
1 change: 1 addition & 0 deletions tests/build_constraints/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ go_library(
":c_srcs_group",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/build_constraints",
)

filegroup(
Expand Down
1 change: 1 addition & 0 deletions tests/cgo_filtered/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ go_library(
name = "go_default_library",
srcs = ["pure.go"],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_filtered",
)
1 change: 1 addition & 0 deletions tests/cgo_library_root_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_library(
"foo.c",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_library_root_dir",
tags = ["manual"],
)

Expand Down
1 change: 1 addition & 0 deletions tests/cgo_multi_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ go_library(
"foo/foo.go",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_multi_dir",
)
1 change: 0 additions & 1 deletion tests/cgo_opts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ go_test(
name = "go_default_test",
srcs = ["cgo_opts_test.go"],
embed = [":go_default_library"],
importpath = "github.com/bazelbuild/rules_go/tests/cgo_opts",
)
1 change: 1 addition & 0 deletions tests/cgo_pthread_flag/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = ["cgo_pthread_flag.go"],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_pthread_flag",
)

go_test(
Expand Down
1 change: 1 addition & 0 deletions tests/cgo_pure/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ go_library(
"pure.go",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_pure",
)

go_test(
Expand Down
1 change: 1 addition & 0 deletions tests/cgo_select/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
],
}),
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_select",
)

cc_library(
Expand Down
1 change: 1 addition & 0 deletions tests/cgo_sys_hdr/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"sub/foo.h",
],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_sys_hdr",
)

go_test(
Expand Down
1 change: 1 addition & 0 deletions tests/cgo_trans_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = ["cgo_lib.go"],
cgo = True,
importpath = "github.com/bazelbuild/rules_go/tests/cgo_trans_deps",
deps = ["//tests/cgo_trans_deps/dep:go_default_library"],
)

Expand Down
1 change: 1 addition & 0 deletions tests/cgo_trans_deps/dep/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["dep.go"],
importpath = "github.com/bazelbuild/rules_go/tests/cgo_trans_deps/dep",
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions tests/coverage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_test(
go_library(
name = "go_default_library",
srcs = ["lib.go"],
importpath = "github.com/bazelbuild/rules_go/tests/coverage",
)

bazel_test(
Expand All @@ -37,6 +38,7 @@ go_binary(
go_library(
name = "bin_lib",
srcs = ["bin_lib.go"],
importpath = "github.com/bazelbuild/rules_go/tests/coverage",
)

bazel_test(
Expand Down
2 changes: 0 additions & 2 deletions tests/empty_package/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ go_test(
name = "empty_package_cgo",
size = "small",
srcs = ["empty_package_test.go"],
importpath = "github.com/bazelbuild/rules_go/tests/empty_package_test",
pure = "off",
x_defs = {
"Expect": "2",
Expand All @@ -36,7 +35,6 @@ go_test(
name = "empty_package_pure",
size = "small",
srcs = ["empty_package_test.go"],
importpath = "github.com/bazelbuild/rules_go/tests/empty_package_test",
pure = "on",
x_defs = {
"Expect": "1",
Expand Down
4 changes: 4 additions & 0 deletions tests/gc_opts_unsafe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ go_library(
srcs = ["unsafe.go"],
gc_goopts = ["-u"],
tags = ["manual"],
importpath = "github.com/bazelbuild/rules_go/tests/gc_opts_unsafe",
)

go_library(
name = "unsafe_library_lib",
embed = [":unsafe_srcs_lib"],
tags = ["manual"],
importpath = "github.com/bazelbuild/rules_go/tests/gc_opts_unsafe",
)

go_binary(
Expand Down Expand Up @@ -55,12 +57,14 @@ go_library(
cgo = True,
gc_goopts = ["-u"],
tags = ["manual"],
importpath = "github.com/bazelbuild/rules_go/tests/gc_opts_unsafe",
)

go_library(
name = "unsafe_cgo_client_lib",
embed = [":unsafe_cgo_lib"],
tags = ["manual"],
importpath = "github.com/bazelbuild/rules_go/tests/gc_opts_unsafe",
)

go_binary(
Expand Down
Loading

0 comments on commit 2f8bcdc

Please sign in to comment.