diff --git a/go/private/actions/archive.bzl b/go/private/actions/archive.bzl index 9b96e17aa6..63621ee140 100644 --- a/go/private/actions/archive.bzl +++ b/go/private/actions/archive.bzl @@ -29,6 +29,7 @@ load( "@io_bazel_rules_go//go/private:providers.bzl", "GoArchive", "GoArchiveData", + "effective_importpath_pkgpath", "get_archive", ) @@ -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, @@ -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, diff --git a/go/private/context.bzl b/go/private/context.bzl index 611b1257d1..00de56b192 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -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 @@ -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 ) diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl index 521f8498d1..33e2a63b65 100644 --- a/go/private/rules/binary.bzl +++ b/go/private/rules/binary.bzl @@ -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: diff --git a/go/private/rules/nogo.bzl b/go/private/rules/nogo.bzl index 1db18c13a7..7d3be8452a 100644 --- a/go/private/rules/nogo.bzl +++ b/go/private/rules/nogo.bzl @@ -65,6 +65,7 @@ def _nogo_impl(ctx): importpath = "nogomain", importmap = "nogomain", pathtype = EXPORT_PATH, + is_main = True, resolve = None, ) diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index ecf133e958..9bfbe8304a 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -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] diff --git a/go/providers.rst b/go/providers.rst index 3ce43b7977..cd0ff96900 100644 --- a/go/providers.rst +++ b/go/providers.rst @@ -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 ~~~~~~~~