Commit 1174ad3a authored by Alex Brainman's avatar Alex Brainman

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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 5776bd55
......@@ -6244,3 +6244,47 @@ func TestNoRelativeTmpdir(t *testing.T) {
tg.grepStderr("relative tmpdir", "wrong error")
}
}
// Issue 24704.
func TestLinkerTmpDirIsDeleted(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()
tg.tempFile("a.go", `package main; import "C"; func main() {}`)
tg.run("build", "-ldflags", "-v", "-o", os.DevNull, tg.path("a.go"))
// Find line that has "host link:" in linker output.
stderr := tg.getStderr()
var hostLinkLine string
for _, line := range strings.Split(stderr, "\n") {
if !strings.Contains(line, "host link:") {
continue
}
hostLinkLine = line
break
}
if hostLinkLine == "" {
t.Fatal(`fail to find with "host link:" string in linker output`)
}
// Find parameter, like "/tmp/go-link-408556474/go.o" inside of
// "host link:" line, and extract temp directory /tmp/go-link-408556474
// out of it.
tmpdir := hostLinkLine
i := strings.Index(tmpdir, `go.o"`)
if i == -1 {
t.Fatalf(`fail to find "go.o" in "host link:" line %q`, hostLinkLine)
}
tmpdir = tmpdir[:i-1]
i = strings.LastIndex(tmpdir, `"`)
if i == -1 {
t.Fatalf(`fail to find " in "host link:" line %q`, hostLinkLine)
}
tmpdir = tmpdir[i+1:]
// Verify that temp directory has been removed.
_, err := os.Stat(tmpdir)
if err == nil {
t.Fatalf("temp directory %q has not been removed", tmpdir)
}
if !os.IsNotExist(err) {
t.Fatalf("Stat(%q) returns unexpected error: %v", tmpdir, err)
}
}
......@@ -921,12 +921,6 @@ func hostobjs(ctxt *Link) {
}
}
// provided by lib9
func rmtemp() {
os.RemoveAll(*flagTmpdir)
}
func hostlinksetup(ctxt *Link) {
if ctxt.LinkMode != LinkExternal {
return
......@@ -945,7 +939,10 @@ func hostlinksetup(ctxt *Link) {
log.Fatal(err)
}
*flagTmpdir = dir
AtExit(rmtemp)
AtExit(func() {
ctxt.Out.f.Close()
os.RemoveAll(*flagTmpdir)
})
}
// change our output to temporary object file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment