Skip to content

Commit

Permalink
Infer importpath if not set explicitely
Browse files Browse the repository at this point in the history
  • Loading branch information
mering committed Sep 21, 2023
1 parent 7309aba commit 84d9478
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
12 changes: 7 additions & 5 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -357,17 +357,19 @@ def _check_importpaths(ctx):
if ":" in p:
fail("import path '%s' contains invalid character :" % p)

def _infer_importpath(ctx):
def _infer_importpath(ctx, attr = None):
DEFAULT_LIB = "go_default_library"
VENDOR_PREFIX = "/vendor/"
if not attr:
attr = ctx.attr

# Check if paths were explicitly set, either in this rule or in an
# embedded rule.
attr_importpath = getattr(ctx.attr, "importpath", "")
attr_importmap = getattr(ctx.attr, "importmap", "")
attr_importpath = getattr(attr, "importpath", "")
attr_importmap = getattr(attr, "importmap", "")
embed_importpath = ""
embed_importmap = ""
for embed in getattr(ctx.attr, "embed", []):
for embed in getattr(attr, "embed", []):
if GoLibrary not in embed:
continue
lib = embed[GoLibrary]
Expand Down Expand Up @@ -504,7 +506,7 @@ def go_context(ctx, attr = None):
toolchain.sdk.tools)

_check_importpaths(ctx)
importpath, importmap, pathtype = _infer_importpath(ctx)
importpath, importmap, pathtype = _infer_importpath(ctx, attr)
importpath_aliases = tuple(getattr(attr, "importpath_aliases", ()))

return struct(
Expand Down
3 changes: 0 additions & 3 deletions go/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ load(
load(
"//go/private:providers.bzl",
"GoLibrary",
"INFERRED_PATH",
)
load(
"//go/private/rules:transition.bzl",
Expand All @@ -39,8 +38,6 @@ load(
def _go_library_impl(ctx):
"""Implements the go_library() rule."""
go = go_context(ctx)
if go.pathtype == INFERRED_PATH:
fail("importpath must be specified in this library or one of its embedded libraries")
library = go.new_library(go)
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
archive = go.archive(go, source)
Expand Down
16 changes: 6 additions & 10 deletions proto/def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ load(
"//go/private:go_toolchain.bzl",
"GO_TOOLCHAIN",
)
load(
"//go/private:providers.bzl",
"INFERRED_PATH",
)
load(
"//go/private/rules:transition.bzl",
"non_go_tool_transition",
Expand All @@ -46,7 +42,7 @@ load(

GoProtoImports = provider()

def get_imports(attr):
def get_imports(attr, importpath):
proto_deps = []

# ctx.attr.proto is a one-element array since there is a Starlark transition attached to it.
Expand All @@ -60,7 +56,7 @@ def get_imports(attr):
direct = dict()
for dep in proto_deps:
for src in dep[ProtoInfo].check_deps_sources.to_list():
direct["{}={}".format(proto_path(src, dep[ProtoInfo]), attr.importpath)] = True
direct["{}={}".format(proto_path(src, dep[ProtoInfo]), importpath)] = True

deps = getattr(attr, "deps", []) + getattr(attr, "embed", [])
transitive = [
Expand All @@ -71,7 +67,8 @@ def get_imports(attr):
return depset(direct = direct.keys(), transitive = transitive)

def _go_proto_aspect_impl(_target, ctx):
imports = get_imports(ctx.rule.attr)
go = go_context(ctx, ctx.rule.attr)
imports = get_imports(ctx.rule.attr, go.importpath)
return [GoProtoImports(imports = imports)]

_go_proto_aspect = aspect(
Expand All @@ -80,6 +77,7 @@ _go_proto_aspect = aspect(
"deps",
"embed",
],
toolchains = [GO_TOOLCHAIN],
)

def _proto_library_to_source(_go, attr, source, merge):
Expand All @@ -93,8 +91,6 @@ def _proto_library_to_source(_go, attr, source, merge):

def _go_proto_library_impl(ctx):
go = go_context(ctx)
if go.pathtype == INFERRED_PATH:
fail("importpath must be specified in this library or one of its embedded libraries")
if ctx.attr.compiler:
#TODO: print("DEPRECATED: compiler attribute on {}, use compilers instead".format(ctx.label))
compilers = [ctx.attr.compiler]
Expand Down Expand Up @@ -124,7 +120,7 @@ def _go_proto_library_impl(ctx):
go,
compiler = compiler,
protos = [d[ProtoInfo] for d in proto_deps],
imports = get_imports(ctx.attr),
imports = get_imports(ctx.attr, go.importpath),
importpath = go.importpath,
))
library = go.new_library(
Expand Down

0 comments on commit 84d9478

Please sign in to comment.