Skip to content

Commit

Permalink
Ensure go_binary.embed libraries' importpath is main (bazel-contrib#2135
Browse files Browse the repository at this point in the history
)

Go 1.13 is stricter on the package path passed to go tool compile -p.
This means that go_library passed in go_binary.embed fails to declare a
main.main function, as they now retain their original import path. Fix
that by adding a is_main boolean to the GoLibrary provider, which will
tell wether a library is supposed to be a main package; that is,
compiled from within a go_binary.

Fixes bazel-contrib#2133
  • Loading branch information
steeve authored and Jay Conrod committed Aug 23, 2019
1 parent 81382dd commit 492f98e
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 4 deletions.
7 changes: 5 additions & 2 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ load(
"@io_bazel_rules_go//go/private:providers.bzl",
"GoArchive",
"GoArchiveData",
"effective_importpath_pkgpath",
"get_archive",
)

Expand Down Expand Up @@ -66,11 +67,13 @@ def emit_archive(go, source = None):
if split.asm:
asmhdr = go.declare_file(go, "go_asm.h")

importpath, _ = effective_importpath_pkgpath(source.library)
importmap = "main" if source.library.is_main else source.library.importmap
if len(split.asm) == 0 and not source.cgo_archives:
go.compile(
go,
sources = split.go,
importpath = source.library.importmap,
importpath = importmap,
archives = direct,
out_lib = out_lib,
out_export = out_export,
Expand All @@ -87,7 +90,7 @@ def emit_archive(go, source = None):
go.compile(
go,
sources = split.go + split.asm + split.headers,
importpath = source.library.importmap,
importpath = importmap,
archives = direct,
out_lib = partial_lib,
out_export = out_export,
Expand Down
3 changes: 2 additions & 1 deletion go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _tool_args(go):
args.set_param_file_format("multiline")
return args

def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, **kwargs):
def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, is_main = False, **kwargs):
if not importpath:
importpath = go.importpath
importmap = go.importmap
Expand All @@ -160,6 +160,7 @@ def _new_library(go, name = None, importpath = None, resolver = None, importable
pathtype = pathtype,
resolve = resolver,
testfilter = testfilter,
is_main = is_main,
**kwargs
)

Expand Down
2 changes: 1 addition & 1 deletion go/private/rules/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _go_binary_impl(ctx):
"""go_binary_impl emits actions for compiling and linking a go executable."""
go = go_context(ctx)

library = go.new_library(go, importable = False)
library = go.new_library(go, importable = False, is_main = True)
source = go.library_to_source(go, ctx.attr, library, ctx.coverage_instrumented())
name = ctx.attr.basename
if not name:
Expand Down
1 change: 1 addition & 0 deletions go/private/rules/nogo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def _nogo_impl(ctx):
importpath = "nogomain",
importmap = "nogomain",
pathtype = EXPORT_PATH,
is_main = True,
resolve = None,
)

Expand Down
1 change: 1 addition & 0 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def _go_test_impl(ctx):
importpath = "testmain",
importmap = "testmain",
pathtype = INFERRED_PATH,
is_main = True,
resolve = None,
)
test_deps = external_archive.direct + [external_archive]
Expand Down
6 changes: 6 additions & 0 deletions go/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ an input to the `library_to_source`_ helper method, which produces GoSource_.
| A function called by `library_to_source`_ that can be used to resolve this |
| library to a mode-specific GoSource_. |
+--------------------------------+-----------------------------------------------------------------+
| :param:`is_main` | :type:`bool` |
+--------------------------------+-----------------------------------------------------------------+
| Indicates whether the library should be compiled as a `main` package. |
| `main` packages may have arbitrary `importpath` and `importmap` values, |
| but the compiler and linker must see them as `main`. |
+--------------------------------+-----------------------------------------------------------------+

GoSource
~~~~~~~~
Expand Down

0 comments on commit 492f98e

Please sign in to comment.