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 3 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/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def emit_compilepkg(
args.add("-cover_format", go.cover_format)
args.add_all(cover, before_each = "-cover")
args.add_all(archives, before_each = "-arc", map_each = _archive)
args.add("-label_name", go.label.name)
if importpath:
args.add("-importpath", importpath)
sluongng marked this conversation as resolved.
Show resolved Hide resolved
if importmap:
Expand Down
28 changes: 20 additions & 8 deletions go/tools/builders/compilepkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func compilePkg(args []string) error {
goenv := envFlags(fs)
var unfilteredSrcs, coverSrcs, embedSrcs, embedLookupDirs, embedRoots multiFlag
var deps archiveMultiFlag
var importPath, packagePath, nogoPath, packageListPath, coverMode string
var labelName, importPath, packagePath, nogoPath, packageListPath, coverMode string
var outPath, outFactsPath, cgoExportHPath string
var testFilter string
var gcFlags, asmFlags, cppFlags, cFlags, cxxFlags, objcFlags, objcxxFlags, ldFlags quoteMultiFlag
Expand All @@ -63,6 +63,7 @@ func compilePkg(args []string) error {
fs.Var(&objcFlags, "objcflags", "Objective-C compiler flags")
fs.Var(&objcxxFlags, "objcxxflags", "Objective-C++ compiler flags")
fs.Var(&ldFlags, "ldflags", "C linker flags")
fs.StringVar(&labelName, "label_name", "", "The label's name")
fs.StringVar(&nogoPath, "nogo", "", "The nogo binary. If unset, nogo will not be run.")
fs.StringVar(&packageListPath, "package_list", "", "The file containing the list of standard library packages")
fs.StringVar(&coverMode, "cover_mode", "", "The coverage mode to use. Empty if coverage instrumentation should not be added.")
Expand Down Expand Up @@ -123,6 +124,7 @@ func compilePkg(args []string) error {

return compileArchive(
goenv,
labelName,
importPath,
packagePath,
srcs,
Expand Down Expand Up @@ -152,6 +154,7 @@ func compilePkg(args []string) error {

func compileArchive(
goenv *env,
labelName string,
importPath string,
packagePath string,
srcs archiveSrcs,
Expand All @@ -176,8 +179,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 +191,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), labelName)
err := os.Mkdir(emptyDir, 0700)
if err != nil {
return 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 +428,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) {}