Source file test/fixedbugs/issue22660.go

     1  // run
     2  
     3  //go:build !js && !wasip1 && gc
     4  
     5  // Copyright 2017 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  
     9  package main
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"log"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"strings"
    19  )
    20  
    21  func main() {
    22  	f, err := os.CreateTemp("", "issue22660.go")
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	f.Close()
    27  	defer os.Remove(f.Name())
    28  
    29  	// path must appear in error messages even if we strip them with -trimpath
    30  	path := filepath.Join("users", "xxx", "go")
    31  	filename := filepath.Join(path, "foo.go")
    32  	var src bytes.Buffer
    33  	fmt.Fprintf(&src, "//line %s:1\n", filename)
    34  	if err := os.WriteFile(f.Name(), src.Bytes(), 0660); err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	out, err := exec.Command("go", "tool", "compile", "-p=p", fmt.Sprintf("-trimpath=%s", path), f.Name()).CombinedOutput()
    39  	if err == nil {
    40  		// The file only contains a line directive, w/o a package clause.
    41  		log.Fatalf("expected compiling %s to fail with syntax error", f.Name())
    42  	}
    43  
    44  	// The error message position depends on the line directive.
    45  	// The resolved path is <tempdir>/filename, so the directive's components
    46  	// must appear as a path suffix in the error message's error position
    47  	// (go.dev/issue/70478).
    48  	if !strings.Contains(string(out), string(filepath.Separator)+filename) {
    49  		log.Fatalf("expected full path and filename (%s) in error message, got:\n%s", filename, out)
    50  	}
    51  }
    52  

View as plain text