Skip to content

Commit 1174ad3

Browse files
committed
cmd/link: close go.o before deleting it
Windows does not allow to delete opened file. Fixes #24704 Change-Id: Idfca2d00a2c46bdd9bd2a721478bfd003c474ece Reviewed-on: https://go-review.googlesource.com/113935 Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5776bd5 commit 1174ad3

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

src/cmd/go/go_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6244,3 +6244,47 @@ func TestNoRelativeTmpdir(t *testing.T) {
62446244
tg.grepStderr("relative tmpdir", "wrong error")
62456245
}
62466246
}
6247+
6248+
// Issue 24704.
6249+
func TestLinkerTmpDirIsDeleted(t *testing.T) {
6250+
tg := testgo(t)
6251+
defer tg.cleanup()
6252+
tg.parallel()
6253+
tg.tempFile("a.go", `package main; import "C"; func main() {}`)
6254+
tg.run("build", "-ldflags", "-v", "-o", os.DevNull, tg.path("a.go"))
6255+
// Find line that has "host link:" in linker output.
6256+
stderr := tg.getStderr()
6257+
var hostLinkLine string
6258+
for _, line := range strings.Split(stderr, "\n") {
6259+
if !strings.Contains(line, "host link:") {
6260+
continue
6261+
}
6262+
hostLinkLine = line
6263+
break
6264+
}
6265+
if hostLinkLine == "" {
6266+
t.Fatal(`fail to find with "host link:" string in linker output`)
6267+
}
6268+
// Find parameter, like "/tmp/go-link-408556474/go.o" inside of
6269+
// "host link:" line, and extract temp directory /tmp/go-link-408556474
6270+
// out of it.
6271+
tmpdir := hostLinkLine
6272+
i := strings.Index(tmpdir, `go.o"`)
6273+
if i == -1 {
6274+
t.Fatalf(`fail to find "go.o" in "host link:" line %q`, hostLinkLine)
6275+
}
6276+
tmpdir = tmpdir[:i-1]
6277+
i = strings.LastIndex(tmpdir, `"`)
6278+
if i == -1 {
6279+
t.Fatalf(`fail to find " in "host link:" line %q`, hostLinkLine)
6280+
}
6281+
tmpdir = tmpdir[i+1:]
6282+
// Verify that temp directory has been removed.
6283+
_, err := os.Stat(tmpdir)
6284+
if err == nil {
6285+
t.Fatalf("temp directory %q has not been removed", tmpdir)
6286+
}
6287+
if !os.IsNotExist(err) {
6288+
t.Fatalf("Stat(%q) returns unexpected error: %v", tmpdir, err)
6289+
}
6290+
}

src/cmd/link/internal/ld/lib.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -921,12 +921,6 @@ func hostobjs(ctxt *Link) {
921921
}
922922
}
923923

924-
// provided by lib9
925-
926-
func rmtemp() {
927-
os.RemoveAll(*flagTmpdir)
928-
}
929-
930924
func hostlinksetup(ctxt *Link) {
931925
if ctxt.LinkMode != LinkExternal {
932926
return
@@ -945,7 +939,10 @@ func hostlinksetup(ctxt *Link) {
945939
log.Fatal(err)
946940
}
947941
*flagTmpdir = dir
948-
AtExit(rmtemp)
942+
AtExit(func() {
943+
ctxt.Out.f.Close()
944+
os.RemoveAll(*flagTmpdir)
945+
})
949946
}
950947

951948
// change our output to temporary object file

0 commit comments

Comments
 (0)