Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go/private/actions/archive.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def emit_archive(go, source = None):
label = source.library.label,
importpath = source.library.importpath,
importmap = source.library.importmap,
importpath_aliases = source.library.importpath_aliases,
pathtype = source.library.pathtype,
file = out_lib,
export_file = out_export,
Expand Down
4 changes: 3 additions & 1 deletion go/private/actions/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ load(
)

def _archive(v):
importpaths = [v.data.importpath]
importpaths.extend(v.data.importpath_aliases)
return "{}={}={}={}".format(
v.data.importpath,
":".join(importpaths),
v.data.importmap,
v.data.file.path,
v.data.export_file.path if v.data.export_file else "",
Expand Down
4 changes: 3 additions & 1 deletion go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ load(
)

def _archive(v):
importpaths = [v.data.importpath]
importpaths.extend(v.data.importpath_aliases)
return "{}={}={}={}".format(
v.data.importpath,
":".join(importpaths),
v.data.importmap,
v.data.file.path,
v.data.export_file.path if v.data.export_file else "",
Expand Down
19 changes: 19 additions & 0 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def _new_library(go, name = None, importpath = None, resolver = None, importable
label = go._ctx.label,
importpath = importpath,
importmap = importmap,
importpath_aliases = go.importpath_aliases,
pathtype = pathtype,
resolve = resolver,
testfilter = testfilter,
Expand Down Expand Up @@ -294,6 +295,20 @@ def _check_binary_dep(go, dep, edge):
edge = edge,
))

def _check_importpaths(ctx):
paths = []
p = getattr(ctx.attr, "importpath", "")
if p:
paths.append(p)
p = getattr(ctx.attr, "importmap", "")
if p:
paths.append(p)
paths.extend(getattr(ctx.attr, "importpath_aliases", ()))

for p in paths:
if ":" in p:
fail("import path '%s' contains invalid character :" % p)

def _infer_importpath(ctx):
DEFAULT_LIB = "go_default_library"
VENDOR_PREFIX = "/vendor/"
Expand Down Expand Up @@ -380,7 +395,10 @@ def go_context(ctx, attr = None):
toolchain.sdk.libs +
toolchain.sdk.tools)

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

return GoContext(
# Fields
toolchain = toolchain,
Expand All @@ -399,6 +417,7 @@ def go_context(ctx, attr = None):
package_list = toolchain.sdk.package_list,
importpath = importpath,
importmap = importmap,
importpath_aliases = importpath_aliases,
pathtype = pathtype,
cgo_tools = context_data.cgo_tools,
nogo = nogo,
Expand Down
1 change: 1 addition & 0 deletions go/private/rules/library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ go_library = go_rule(
"deps": attr.label_list(providers = [GoLibrary]),
"importpath": attr.string(),
"importmap": attr.string(),
"importpath_aliases": attr.string_list(), # experimental, undocumented
"embed": attr.label_list(providers = [GoLibrary]),
"gc_goopts": attr.string_list(),
"x_defs": attr.string_dict(),
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 @@ -64,6 +64,7 @@ def _nogo_impl(ctx):
label = go._ctx.label,
importpath = "nogomain",
importmap = "nogomain",
importpath_aliases = (),
pathtype = EXPORT_PATH,
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 @@ -133,6 +133,7 @@ def _go_test_impl(ctx):
label = go._ctx.label,
importpath = "testmain",
importmap = "testmain",
importpath_aliases = (),
pathtype = INFERRED_PATH,
resolve = None,
)
Expand Down
88 changes: 2 additions & 86 deletions go/tools/builders/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -109,13 +108,13 @@ func compile(args []string) error {

// Check that the filtered sources don't import anything outside of
// the standard library and the direct dependencies.
_, stdImports, err := checkDirectDeps(goFiles, archives, *packageList)
imports, err := checkImports(goFiles, archives, *packageList)
if err != nil {
return err
}

// Build an importcfg file for the compiler.
importcfgName, err := buildImportcfgFileForCompile(archives, stdImports, goenv.installSuffix, filepath.Dir(*output))
importcfgName, err := buildImportcfgFileForCompile(imports, goenv.installSuffix, filepath.Dir(*output))
if err != nil {
return err
}
Expand Down Expand Up @@ -163,9 +162,6 @@ func compile(args []string) error {
var nogoargs []string
nogoargs = append(nogoargs, "-p", *packagePath)
nogoargs = append(nogoargs, "-importcfg", importcfgName)
for _, imp := range stdImports {
nogoargs = append(nogoargs, "-stdimport", imp)
}
for _, arc := range archives {
if arc.xFile != "" {
nogoargs = append(nogoargs, "-fact", fmt.Sprintf("%s=%s", arc.importPath, arc.xFile))
Expand Down Expand Up @@ -197,83 +193,3 @@ func compile(args []string) error {
}
return nil
}

func checkDirectDeps(files []fileInfo, archives []archive, packageList string) (depImports, stdImports []string, err error) {
packagesTxt, err := ioutil.ReadFile(packageList)
if err != nil {
log.Fatal(err)
}
stdlibSet := map[string]bool{}
for _, line := range strings.Split(string(packagesTxt), "\n") {
line = strings.TrimSpace(line)
if line != "" {
stdlibSet[line] = true
}
}

depSet := map[string]bool{}
depList := make([]string, len(archives))
for i, arc := range archives {
depSet[arc.importPath] = true
depList[i] = arc.importPath
}

importSet := map[string]bool{}

derr := depsError{known: depList}
for _, f := range files {
for _, path := range f.imports {
if path == "C" || isRelative(path) || importSet[path] {
// TODO(#1645): Support local (relative) import paths. We don't emit
// errors for them here, but they will probably break something else.
continue
}
if stdlibSet[path] {
stdImports = append(stdImports, path)
continue
}
if depSet[path] {
depImports = append(depImports, path)
continue
}
derr.missing = append(derr.missing, missingDep{f.filename, path})
}
}
if len(derr.missing) > 0 {
return nil, nil, derr
}
return depImports, stdImports, nil
}

type depsError struct {
missing []missingDep
known []string
}

type missingDep struct {
filename, imp string
}

var _ error = depsError{}

func (e depsError) Error() string {
buf := bytes.NewBuffer(nil)
fmt.Fprintf(buf, "missing strict dependencies:\n")
for _, dep := range e.missing {
fmt.Fprintf(buf, "\t%s: import of %q\n", dep.filename, dep.imp)
}
if len(e.known) == 0 {
fmt.Fprintln(buf, "No dependencies were provided.")
} else {
fmt.Fprintln(buf, "Known dependencies are:")
for _, imp := range e.known {
fmt.Fprintf(buf, "\t%s\n", imp)
}
}
fmt.Fprint(buf, "Check that imports in Go sources match importpath attributes in deps.")
return buf.String()
}

func isRelative(path string) bool {
return strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../")
}
35 changes: 19 additions & 16 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,33 @@ func compileArchive(

// Check that the filtered sources don't import anything outside of
// the standard library and the direct dependencies.
_, stdImports, err := checkDirectDeps(srcs.goSrcs, deps, packageListPath)
imports, err := checkImports(srcs.goSrcs, deps, packageListPath)
if err != nil {
return err
}
if cgoEnabled && len(cgoSrcs) != 0 {
// cgo generated code imports some extra packages.
cgoStdImports := map[string]struct{}{
"runtime/cgo": struct{}{},
"syscall": struct{}{},
"unsafe": struct{}{},
}
for _, imp := range stdImports {
delete(cgoStdImports, imp)
imports["runtime/cgo"] = nil
imports["syscall"] = nil
imports["unsafe"] = nil
}
if coverMode != "" {
const coverdataPath = "github.com/bazelbuild/rules_go/go/tools/coverdata"
var coverdata *archive
for i := range deps {
if deps[i].importPath == coverdataPath {
coverdata = &deps[i]
break
}
}
for imp := range cgoStdImports {
stdImports = append(stdImports, imp)
if coverdata == nil {
return errors.New("coverage requested but coverdata dependency not provided")
}
imports[coverdataPath] = coverdata
}

// Build an importcfg file for the compiler.
importcfgPath, err := buildImportcfgFileForCompile(deps, stdImports, goenv.installSuffix, filepath.Dir(outPath))
importcfgPath, err := buildImportcfgFileForCompile(imports, goenv.installSuffix, filepath.Dir(outPath))
if err != nil {
return err
}
Expand All @@ -298,7 +304,7 @@ func compileArchive(
ctx, cancel := context.WithCancel(context.Background())
nogoChan = make(chan error)
go func() {
nogoChan <- runNogo(ctx, nogoPath, goSrcs, deps, stdImports, packagePath, importcfgPath, outFactsPath)
nogoChan <- runNogo(ctx, nogoPath, goSrcs, deps, packagePath, importcfgPath, outFactsPath)
}()
defer func() {
if nogoChan != nil {
Expand Down Expand Up @@ -402,13 +408,10 @@ func compileGo(goenv *env, srcs []string, packagePath, importcfgPath, asmHdrPath
return goenv.runCommand(args)
}

func runNogo(ctx context.Context, nogoPath string, srcs []string, deps []archive, stdImports []string, packagePath, importcfgPath, outFactsPath string) error {
func runNogo(ctx context.Context, nogoPath string, srcs []string, deps []archive, packagePath, importcfgPath, outFactsPath string) error {
args := []string{nogoPath}
args = append(args, "-p", packagePath)
args = append(args, "-importcfg", importcfgPath)
for _, imp := range stdImports {
args = append(args, "-stdimport", imp)
}
for _, dep := range deps {
if dep.xFile != "" {
args = append(args, "-fact", fmt.Sprintf("%s=%s", dep.importPath, dep.xFile))
Expand Down
Loading