Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compilepkg: fix race when run without sandbox #3145

Merged
merged 5 commits into from
Jun 4, 2022
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
2 changes: 2 additions & 0 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def emit_compilepkg(
args.add_all(archives, before_each = "-arc", map_each = _archive)
if importpath:
args.add("-importpath", importpath)
else:
args.add("-importpath", go.label.name)
if importmap:
args.add("-p", importmap)
args.add("-package_list", go.package_list)
Expand Down
23 changes: 16 additions & 7 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ func compileArchive(
outPath string,
outXPath string,
cgoExportHPath string,
coverFormat string) error {

coverFormat string,
) error {
sluongng marked this conversation as resolved.
Show resolved Hide resolved
workDir, cleanup, err := goenv.workDir()
if err != nil {
return err
Expand All @@ -188,19 +188,28 @@ func compileArchive(
// We need to run the compiler to create a valid archive, even if there's
// nothing in it. GoPack will complain if we try to add assembly or cgo
// objects.
//
// _empty.go needs to be in a deterministic location (not tmpdir) in order
// to ensure deterministic output
emptyPath := filepath.Join(filepath.Dir(outPath), "_empty.go")
// to ensure deterministic output. The location also needs to be unique
// otherwise platforms without sandbox support may race to create/remove
// the file during parallel compilation.
emptyDir := filepath.Join(filepath.Dir(outPath), sanitizePathForIdentifier(importPath))
sluongng marked this conversation as resolved.
Show resolved Hide resolved
if err := os.Mkdir(emptyDir, 0700); err != nil {
return fmt.Errorf("could not create directory for _empty.go: %v", err)
}
defer os.RemoveAll(emptyDir)

emptyPath := filepath.Join(emptyDir, "_empty.go")
if err := os.WriteFile(emptyPath, []byte("package empty\n"), 0666); err != nil {
return err
}

srcs.goSrcs = append(srcs.goSrcs, fileInfo{
filename: emptyPath,
ext: goExt,
matched: true,
pkg: "empty",
})
defer os.Remove(emptyPath)
}
packageName := srcs.goSrcs[0].pkg
var goSrcs, cgoSrcs []string
Expand Down Expand Up @@ -416,8 +425,8 @@ func compileArchive(
// Compile the .s files.
if len(srcs.sSrcs) > 0 {
includeSet := map[string]struct{}{
filepath.Join(os.Getenv("GOROOT"), "pkg", "include"): struct{}{},
workDir: struct{}{},
filepath.Join(os.Getenv("GOROOT"), "pkg", "include"): {},
workDir: {},
sluongng marked this conversation as resolved.
Show resolved Hide resolved
}
for _, hdr := range srcs.hSrcs {
includeSet[filepath.Dir(hdr.filename)] = struct{}{}
Expand Down
13 changes: 13 additions & 0 deletions tests/core/go_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ go_library(
deps = [":indirect_import_lib"],
)

[
go_test(
name = "same_package_{}_test".format(i),
srcs = ["same_package_test.go"],
)
for i in range(1, 80)
]

test_suite(
name = "same_package_test",
tests = ["same_package_{}_test".format(i) for i in range(1, 80)],
)

go_bazel_test(
name = "testmain_without_exit_test",
srcs = ["testmain_without_exit_test.go"],
Expand Down
7 changes: 7 additions & 0 deletions tests/core/go_test/same_package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package same_package

import (
"testing"
)

func OkTest(t *testing.T) {}