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

     1  [short] skip
     2  
     3  # If GOROOT is set, 'go build -trimpath' bakes that into the resulting
     4  # binary. Explicitly unset it here.
     5  env GOROOT=
     6  
     7  # Set up two identical directories that can be used as GOPATH.
     8  env GO111MODULE=on
     9  mkdir $WORK/a/src/paths $WORK/b/src/paths
    10  cp paths.go $WORK/a/src/paths
    11  cp paths.go $WORK/b/src/paths
    12  cp overlay.json $WORK/a/src/paths
    13  cp overlay.json $WORK/b/src/paths
    14  cp go.mod $WORK/a/src/paths/
    15  cp go.mod $WORK/b/src/paths/
    16  
    17  
    18  # A binary built without -trimpath should contain the module root dir
    19  # and GOROOT for debugging and stack traces.
    20  cd $WORK/a/src/paths
    21  go build -o $WORK/paths-dbg.exe .
    22  exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
    23  stdout 'binary contains module root: true'
    24  stdout 'binary contains an empty GOROOT'
    25  
    26  # A binary built with -trimpath should not contain the current workspace.
    27  go build -trimpath -o $WORK/paths-a.exe .
    28  exec $WORK/paths-a.exe $WORK/paths-a.exe
    29  stdout 'binary contains module root: false'
    30  stdout 'binary contains an empty GOROOT'
    31  
    32  # A binary from an external module built with -trimpath should not contain
    33  # the current workspace or GOROOT.
    34  go get rsc.io/fortune
    35  go install -trimpath rsc.io/fortune
    36  exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
    37  stdout 'binary contains module root: false'
    38  stdout 'binary contains an empty GOROOT'
    39  go mod edit -droprequire rsc.io/fortune
    40  
    41  # Two binaries built from identical packages in different directories
    42  # should be identical.
    43  cd $WORK/b/src/paths
    44  go build -trimpath -o $WORK/paths-b.exe
    45  cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
    46  
    47  
    48  # Same sequence of tests but with overlays.
    49  # A binary built without -trimpath should contain the module root dir
    50  # and GOROOT for debugging and stack traces.
    51  cd $WORK/a/src/paths
    52  go build -overlay overlay.json -o $WORK/paths-dbg.exe ./overlaydir
    53  exec $WORK/paths-dbg.exe $WORK/paths-dbg.exe
    54  stdout 'binary contains module root: true'
    55  stdout 'binary contains an empty GOROOT'
    56  
    57  # A binary built with -trimpath should not contain the current workspace.
    58  go build -overlay overlay.json -trimpath -o $WORK/paths-a.exe ./overlaydir
    59  exec $WORK/paths-a.exe $WORK/paths-a.exe
    60  stdout 'binary contains module root: false'
    61  stdout 'binary contains an empty GOROOT'
    62  
    63  # Two binaries built from identical packages in different directories
    64  # should be identical.
    65  cd $WORK/b/src/paths
    66  go build -overlay overlay.json -trimpath -o $WORK/paths-b.exe ./overlaydir
    67  cmp -q $WORK/paths-a.exe $WORK/paths-b.exe
    68  
    69  
    70  # Same sequence of tests but in GOPATH mode.
    71  # A binary built without -trimpath should contain GOPATH and GOROOT.
    72  env GO111MODULE=off
    73  cd $WORK
    74  env GOPATH=$WORK/a
    75  go build -o paths-dbg.exe paths
    76  exec ./paths-dbg.exe paths-dbg.exe
    77  stdout 'binary contains GOPATH: true'
    78  stdout 'binary contains an empty GOROOT'
    79  
    80  # A binary built with -trimpath should not contain GOPATH.
    81  go build -trimpath -o paths-a.exe paths
    82  exec ./paths-a.exe paths-a.exe
    83  stdout 'binary contains GOPATH: false'
    84  stdout 'binary contains an empty GOROOT'
    85  
    86  # Two binaries built from identical packages in different GOPATH roots
    87  # should be identical.
    88  env GOPATH=$WORK/b
    89  go build -trimpath -o paths-b.exe paths
    90  cmp -q paths-a.exe paths-b.exe
    91  
    92  
    93  # Same sequence of tests but with gccgo.
    94  # gccgo does not support builds in module mode.
    95  [!exec:gccgo] stop
    96  [cross] stop  # gccgo can't necessarily cross-compile
    97  env GOPATH=$WORK/a
    98  
    99  # A binary built with gccgo without -trimpath should contain the current
   100  # GOPATH and GOROOT.
   101  go build -compiler=gccgo -o paths-dbg.exe paths
   102  exec ./paths-dbg.exe paths-dbg.exe
   103  stdout 'binary contains GOPATH: true'
   104  stdout 'binary contains an empty GOROOT'
   105  
   106  # gccgo doesn't load std from GOROOT.
   107  # A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
   108  go build -compiler=gccgo -trimpath -o paths-a.exe paths
   109  exec ./paths-a.exe paths-a.exe
   110  stdout 'binary contains GOPATH: false'
   111  stdout 'binary contains an empty GOROOT'
   112  
   113  # Two binaries built from identical packages in different directories
   114  # should be identical.
   115  env GOPATH=$WORK/b
   116  go build -compiler=gccgo -trimpath -o paths-b.exe paths
   117  cmp -q paths-a.exe paths-b.exe
   118  
   119  -- paths.go --
   120  package main
   121  
   122  import (
   123  	"bytes"
   124  	"fmt"
   125  	"io/ioutil"
   126  	"log"
   127  	"os"
   128  	"os/exec"
   129  	"path/filepath"
   130  	"strings"
   131  )
   132  
   133  func main() {
   134  	exe := os.Args[1]
   135  	data, err := ioutil.ReadFile(exe)
   136  	if err != nil {
   137  		log.Fatal(err)
   138  	}
   139  
   140  	if os.Getenv("GO111MODULE") == "on" {
   141  		out, err := exec.Command("go", "env", "GOMOD").Output()
   142  		if err != nil {
   143  			log.Fatal(err)
   144  		}
   145  		modRoot := filepath.Dir(strings.TrimSpace(string(out)))
   146  		check(data, "module root", modRoot)
   147  	} else {
   148  		check(data, "GOPATH", os.Getenv("GOPATH"))
   149  	}
   150  	check(data, "GOROOT", os.Getenv("GOROOT"))
   151  }
   152  
   153  func check(data []byte, desc, dir string) {
   154  	if dir == "" {
   155  		fmt.Printf("binary contains an empty %s\n", desc)
   156  		return
   157  	}
   158  	containsDir := bytes.Contains(data, []byte(dir))
   159  	containsSlashDir := bytes.Contains(data, []byte(filepath.ToSlash(dir)))
   160  	fmt.Printf("binary contains %s: %v\n", desc, containsDir || containsSlashDir)
   161  }
   162  -- overlay.json --
   163  { "Replace": { "overlaydir/paths.go": "paths.go" } }
   164  -- go.mod --
   165  module paths
   166  
   167  go 1.14
   168  

View as plain text