Skip to content

Commit d1da1bb

Browse files
meringlinzhp
andauthored
Infer importpath if not set explicitly (#3705)
* Infer importpath if not set explicitely * Add test with explicit and implicit importpath --------- Co-authored-by: Zhongpeng Lin <[email protected]>
1 parent 69d0fc8 commit d1da1bb

File tree

7 files changed

+85
-18
lines changed

7 files changed

+85
-18
lines changed

go/private/context.bzl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,17 @@ def _check_importpaths(ctx):
357357
if ":" in p:
358358
fail("import path '%s' contains invalid character :" % p)
359359

360-
def _infer_importpath(ctx):
360+
def _infer_importpath(ctx, attr):
361361
DEFAULT_LIB = "go_default_library"
362362
VENDOR_PREFIX = "/vendor/"
363363

364364
# Check if paths were explicitly set, either in this rule or in an
365365
# embedded rule.
366-
attr_importpath = getattr(ctx.attr, "importpath", "")
367-
attr_importmap = getattr(ctx.attr, "importmap", "")
366+
attr_importpath = getattr(attr, "importpath", "")
367+
attr_importmap = getattr(attr, "importmap", "")
368368
embed_importpath = ""
369369
embed_importmap = ""
370-
for embed in getattr(ctx.attr, "embed", []):
370+
for embed in getattr(attr, "embed", []):
371371
if GoLibrary not in embed:
372372
continue
373373
lib = embed[GoLibrary]
@@ -504,7 +504,7 @@ def go_context(ctx, attr = None):
504504
toolchain.sdk.tools)
505505

506506
_check_importpaths(ctx)
507-
importpath, importmap, pathtype = _infer_importpath(ctx)
507+
importpath, importmap, pathtype = _infer_importpath(ctx, attr)
508508
importpath_aliases = tuple(getattr(attr, "importpath_aliases", ()))
509509

510510
return struct(

go/private/rules/library.bzl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ load(
2929
load(
3030
"//go/private:providers.bzl",
3131
"GoLibrary",
32-
"INFERRED_PATH",
3332
)
3433
load(
3534
"//go/private/rules:transition.bzl",
@@ -39,8 +38,6 @@ load(
3938
def _go_library_impl(ctx):
4039
"""Implements the go_library() rule."""
4140
go = go_context(ctx)
42-
if go.pathtype == INFERRED_PATH:
43-
fail("importpath must be specified in this library or one of its embedded libraries")
4441
library = go.new_library(go)
4542
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
4643
archive = go.archive(go, source)

proto/def.bzl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ load(
3131
"//go/private:go_toolchain.bzl",
3232
"GO_TOOLCHAIN",
3333
)
34-
load(
35-
"//go/private:providers.bzl",
36-
"INFERRED_PATH",
37-
)
3834
load(
3935
"//go/private/rules:transition.bzl",
4036
"non_go_tool_transition",
@@ -46,7 +42,7 @@ load(
4642

4743
GoProtoImports = provider()
4844

49-
def get_imports(attr):
45+
def get_imports(attr, importpath):
5046
proto_deps = []
5147

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

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

7369
def _go_proto_aspect_impl(_target, ctx):
74-
imports = get_imports(ctx.rule.attr)
70+
go = go_context(ctx, ctx.rule.attr)
71+
imports = get_imports(ctx.rule.attr, go.importpath)
7572
return [GoProtoImports(imports = imports)]
7673

7774
_go_proto_aspect = aspect(
@@ -80,6 +77,7 @@ _go_proto_aspect = aspect(
8077
"deps",
8178
"embed",
8279
],
80+
toolchains = [GO_TOOLCHAIN],
8381
)
8482

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

9492
def _go_proto_library_impl(ctx):
9593
go = go_context(ctx)
96-
if go.pathtype == INFERRED_PATH:
97-
fail("importpath must be specified in this library or one of its embedded libraries")
9894
if ctx.attr.compiler:
9995
#TODO: print("DEPRECATED: compiler attribute on {}, use compilers instead".format(ctx.label))
10096
compilers = [ctx.attr.compiler]
@@ -124,7 +120,7 @@ def _go_proto_library_impl(ctx):
124120
go,
125121
compiler = compiler,
126122
protos = [d[ProtoInfo] for d in proto_deps],
127-
imports = get_imports(ctx.attr),
123+
imports = get_imports(ctx.attr, go.importpath),
128124
importpath = go.importpath,
129125
))
130126
library = go.new_library(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
3+
load("@rules_proto//proto:defs.bzl", "proto_library")
4+
5+
# Common rules
6+
proto_library(
7+
name = "foo_proto",
8+
srcs = ["foo.proto"],
9+
)
10+
11+
go_proto_library(
12+
name = "foo_go_proto",
13+
importpath = "path/to/foo_go",
14+
proto = ":foo_proto",
15+
)
16+
17+
proto_library(
18+
name = "bar_proto",
19+
srcs = ["bar.proto"],
20+
deps = [":foo_proto"],
21+
)
22+
23+
go_proto_library(
24+
name = "bar_go_proto",
25+
proto = ":bar_proto",
26+
deps = [":foo_go_proto"],
27+
)
28+
29+
go_test(
30+
name = "importpath_test",
31+
srcs = ["importpath_test.go"],
32+
deps = [
33+
":bar_go_proto",
34+
":foo_go_proto",
35+
],
36+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
package tests.core.go_proto_library_importpath.bar;
4+
5+
import "tests/core/go_proto_library_importpath/foo.proto";
6+
7+
message Bar {
8+
foo.Foo value = 1;
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package tests.core.go_proto_library_importpath.foo;
4+
5+
message Foo {
6+
int64 value = 1;
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package importpath_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
bar_proto "tests/core/go_proto_library_importpath/bar_go_proto"
8+
foo_proto "path/to/foo_go"
9+
)
10+
11+
func Test(t *testing.T) {
12+
bar := &bar_proto.Bar{}
13+
bar.Value = &foo_proto.Foo{}
14+
bar.Value.Value = 5
15+
16+
var expected int64 = 5
17+
if bar.Value.Value != expected {
18+
t.Errorf(fmt.Sprintf("Not equal: \n"+
19+
"expected: %s\n"+
20+
"actual : %s", expected, bar.Value.Value))
21+
}
22+
}

0 commit comments

Comments
 (0)