Text file src/cmd/go/testdata/script/list_export_trimpath.txt

     1  # Regression test for https://go.dev/issue/82438:
     2  # "go list -export" should rewrite source file paths as the compiler does.
     3  
     4  env GOEXPERIMENT=golistexportnewformat
     5  
     6  # With -trimpath, the package directory should be replaced by the import path.
     7  go list -trimpath -export -json .
     8  cp stdout out.json
     9  go run q.go out.json V
    10  stdout '^m/p\.go$'
    11  
    12  # Std export data does not depend on -trimpath, so it should be reused.
    13  go list -export internal/goarch
    14  go list -x -trimpath -export internal/goarch
    15  ! stderr 'export\.cfg'
    16  
    17  [!cgo] stop
    18  
    19  # The object directory should be removed from cgo-generated file names.
    20  go list -export -json ./c
    21  cp stdout out.json
    22  go run q.go out.json _Ctype_int
    23  stdout '^_cgo_gotypes\.go$'
    24  
    25  -- go.mod --
    26  module m
    27  
    28  go 1.27
    29  -- p.go --
    30  package p
    31  
    32  var V = 42
    33  -- c/c.go --
    34  package c
    35  
    36  import "C"
    37  
    38  var V = C.int(42)
    39  -- q.go --
    40  //go:build ignore
    41  
    42  package main
    43  
    44  import (
    45  	"encoding/json"
    46  	"fmt"
    47  	"go/importer"
    48  	"go/token"
    49  	"io"
    50  	"log"
    51  	"os"
    52  )
    53  
    54  func main() {
    55  	data, err := os.ReadFile(os.Args[1])
    56  	if err != nil {
    57  		log.Fatal(err)
    58  	}
    59  	// This is a minimal output of go list -export -json.
    60  	type output struct {
    61  		ImportPath	string
    62  		Export		string
    63  	}
    64  	var out output
    65  	if err := json.Unmarshal(data, &out); err != nil {
    66  		log.Fatal(err)
    67  	}
    68  	fset := token.NewFileSet()
    69  	imp := importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) {
    70  		if path != out.ImportPath {
    71  			log.Fatalf("unexpected import: %q", path)
    72  		}
    73  		return os.Open(out.Export)
    74  	})
    75  	pkg, err := imp.Import(out.ImportPath)
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	fmt.Println(fset.Position(pkg.Scope().Lookup(os.Args[2]).Pos()).Filename)
    80  }
    81  

View as plain text