test/fixedbugs: minor adjustments to line-directive specific tests

Mostly an attempt at understanding the new compiler behavior.
Also, replace some deprecated ioutil calls with os calls.

Follow-up on CL 706795.

Change-Id: Ia626cc44a4188674bb8f21bf9a0c207c6b1a10b1
Reviewed-on: https://go-review.googlesource.com/c/go/+/779983
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Neal Patel <nealpatel@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2026-05-20 15:02:43 -07:00 committed by Gopher Robot
parent 78f63eb790
commit 5a1c0ee6de
4 changed files with 19 additions and 21 deletions

View file

@ -17,7 +17,7 @@ import (
)
// Since go.dev/issue/70478, the compiler resolves a relative filename in a
// //line directive against the directory of the source file, so the expected
// line directive against the directory of the source file, so the expected
// path may be a suffix of the actual filename rather than equal to it.
// Accept either an exact match or the file as a path-component suffix.
// Compiler-emitted paths are always slash-normalized (cmd/internal/objabi.AbsFile).
@ -36,7 +36,7 @@ func check(file string, line int) {
func main() {
//line /foo/bar.go:123
check(`/foo/bar.go`, 123)
check("/foo/bar.go", 123)
//line c:/foo/bar.go:987
check(`c:/foo/bar.go`, 987)
check("c:/foo/bar.go", 987)
}

View file

@ -11,7 +11,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
@ -20,33 +19,33 @@ import (
)
func main() {
f, err := ioutil.TempFile("", "issue22660.go")
f, err := os.CreateTemp("", "issue22660.go")
if err != nil {
log.Fatal(err)
}
f.Close()
defer os.Remove(f.Name())
// path components from the //line directive must survive -trimpath and
// appear in error messages (since go.dev/issue/70478, as a path-component
// suffix of the resolved path, not as the bare prefix).
// path must appear in error messages even if we strip them with -trimpath
path := filepath.Join("users", "xxx", "go")
filename := filepath.Join(path, "foo.go")
var src bytes.Buffer
fmt.Fprintf(&src, "//line %s:1\n", filepath.Join(path, "foo.go"))
if err := ioutil.WriteFile(f.Name(), src.Bytes(), 0660); err != nil {
fmt.Fprintf(&src, "//line %s:1\n", filename)
if err := os.WriteFile(f.Name(), src.Bytes(), 0660); err != nil {
log.Fatal(err)
}
out, err := exec.Command("go", "tool", "compile", "-p=p", fmt.Sprintf("-trimpath=%s", path), f.Name()).CombinedOutput()
if err == nil {
log.Fatalf("expected compiling %s to fail", f.Name())
// The file only contains a line directive, w/o a package clause.
log.Fatalf("expected compiling %s to fail with syntax error", f.Name())
}
// After #70478 the resolved path is <tempdir>/users/xxx/go/foo.go,
// so the directive's components must appear as a path suffix.
want := filepath.Join(path, "foo.go")
if !strings.Contains(string(out), string(filepath.Separator)+want) {
log.Fatalf("expected path component (%s) in error message, got:\n%s", want, out)
// The error message position depends on the line directive.
// The resolved path is <tempdir>/filename, so the directive's components
// must appear as a path suffix in the error message's error position
// (go.dev/issue/70478).
if !strings.Contains(string(out), string(filepath.Separator)+filename) {
log.Fatalf("expected full path and filename (%s) in error message, got:\n%s", filename, out)
}
}

View file

@ -16,7 +16,7 @@ import (
)
// Since go.dev/issue/70478, the compiler resolves a relative filename in a
// //line directive against the directory of the source file, so the expected
// line directive against the directory of the source file, so the expected
// path may be a suffix of the actual filename rather than equal to it.
// Accept either an exact match or the file as a path-component suffix.
// Compiler-emitted paths are always slash-normalized (cmd/internal/objabi.AbsFile).

View file

@ -11,7 +11,6 @@
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
@ -38,7 +37,7 @@ var tests = []struct {
}
func main() {
f, err := ioutil.TempFile("", "issue22662b.go")
f, err := os.CreateTemp("", "issue22662b.go")
if err != nil {
log.Fatal(err)
}
@ -46,7 +45,7 @@ func main() {
defer os.Remove(f.Name())
for _, test := range tests {
if err := ioutil.WriteFile(f.Name(), []byte(test.src), 0660); err != nil {
if err := os.WriteFile(f.Name(), []byte(test.src), 0660); err != nil {
log.Fatal(err)
}