From: Cherry Mui Date: Tue, 22 Mar 2022 21:08:56 +0000 (-0400) Subject: cmd/compile: default to -p=main for main package X-Git-Tag: go1.19beta1~952 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=20ba3f5de52bf8665d35c86111312274f6a4d1be;p=gostls13.git cmd/compile: default to -p=main for main package With the previous CL the compiler emits an unlinkable object if the -p flag is not specified. It is actually okay (and convenient) to omit the -p flag for (just) the main package. This CL makes it so. Change-Id: I978d54d14c45b3bb9ed7471e40a2c47f269b56f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/394834 Trust: Cherry Mui Run-TryBot: Cherry Mui TryBot-Result: Gopher Robot Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/gc/main.go b/src/cmd/compile/internal/gc/main.go index 5a9a889894..1f547130ad 100644 --- a/src/cmd/compile/internal/gc/main.go +++ b/src/cmd/compile/internal/gc/main.go @@ -188,6 +188,9 @@ func Main(archInit func(*ssagen.ArchInfo)) { // Parse and typecheck input. noder.LoadPackage(flag.Args()) + if base.Ctxt.Pkgpath == obj.UnlinkablePkg && types.LocalPkg.Name == "main" { + base.Ctxt.Pkgpath = "main" + } dwarfgen.RecordPackageName() diff --git a/src/cmd/link/link_test.go b/src/cmd/link/link_test.go index 2eefc5c8e7..8df31d7fd4 100644 --- a/src/cmd/link/link_test.go +++ b/src/cmd/link/link_test.go @@ -1067,19 +1067,30 @@ func TestUnlinkableObj(t *testing.T) { tmpdir := t.TempDir() - src := filepath.Join(tmpdir, "x.go") - obj := filepath.Join(tmpdir, "x.o") + xSrc := filepath.Join(tmpdir, "x.go") + pSrc := filepath.Join(tmpdir, "p.go") + xObj := filepath.Join(tmpdir, "x.o") + pObj := filepath.Join(tmpdir, "p.o") exe := filepath.Join(tmpdir, "x.exe") - err := ioutil.WriteFile(src, []byte("package main\nfunc main() {}\n"), 0666) + err := ioutil.WriteFile(xSrc, []byte("package main\nimport _ \"p\"\nfunc main() {}\n"), 0666) if err != nil { t.Fatalf("failed to write source file: %v", err) } - cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", obj, src) // without -p + err = ioutil.WriteFile(pSrc, []byte("package p\n"), 0666) + if err != nil { + t.Fatalf("failed to write source file: %v", err) + } + cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", pObj, pSrc) // without -p out, err := cmd.CombinedOutput() if err != nil { - t.Fatalf("compile failed: %v. output:\n%s", err, out) + t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) } - cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-o", exe, obj) + cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", tmpdir, "-p=main", "-o", xObj, xSrc) + out, err = cmd.CombinedOutput() + if err != nil { + t.Fatalf("compile x.go failed: %v. output:\n%s", err, out) + } + cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-L", tmpdir, "-o", exe, xObj) out, err = cmd.CombinedOutput() if err == nil { t.Fatalf("link did not fail") @@ -1087,4 +1098,21 @@ func TestUnlinkableObj(t *testing.T) { if !bytes.Contains(out, []byte("unlinkable object")) { t.Errorf("did not see expected error message. out:\n%s", out) } + + // It is okay to omit -p for (only) main package. + cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", pObj, pSrc) + out, err = cmd.CombinedOutput() + if err != nil { + t.Fatalf("compile p.go failed: %v. output:\n%s", err, out) + } + cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", tmpdir, "-o", xObj, xSrc) // without -p + out, err = cmd.CombinedOutput() + if err != nil { + t.Fatalf("compile failed: %v. output:\n%s", err, out) + } + cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-L", tmpdir, "-o", exe, xObj) + out, err = cmd.CombinedOutput() + if err != nil { + t.Errorf("link failed: %v. output:\n%s", err, out) + } }