...

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

Documentation: cmd/go/testdata/script

     1[short] skip
     2
     3# go/build's Import should find modules by invoking the go command
     4
     5go build -o $WORK ./testimport ./testfindonly
     6
     7# GO111MODULE=off
     8env GO111MODULE=off
     9! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
    10
    11# GO111MODULE=auto in GOPATH/src
    12env GO111MODULE=auto
    13exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
    14
    15# GO111MODULE=auto outside GOPATH/src
    16cd $GOPATH/other
    17env GO111MODULE=auto
    18exec $WORK/testimport$GOEXE other/x/y/z/w .
    19stdout w2.go
    20
    21! exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
    22stderr 'no required module provides package gobuild.example.com/x/y/z/w; to add it:\n\tgo get gobuild.example.com/x/y/z/w'
    23
    24cd z
    25exec $WORK/testimport$GOEXE other/x/y/z/w .
    26stdout w2.go
    27
    28# GO111MODULE=on outside GOPATH/src
    29env GO111MODULE=
    30exec $WORK/testimport$GOEXE other/x/y/z/w .
    31stdout w2.go
    32env GO111MODULE=on
    33exec $WORK/testimport$GOEXE other/x/y/z/w .
    34stdout w2.go
    35
    36# GO111MODULE=on in GOPATH/src
    37cd $GOPATH/src
    38env GO111MODULE=
    39exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
    40stdout w1.go
    41env GO111MODULE=on
    42exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w .
    43stdout w1.go
    44cd w
    45exec $WORK/testimport$GOEXE gobuild.example.com/x/y/z/w ..
    46stdout w1.go
    47
    48# go/build's Import in FindOnly mode should find directories by invoking the go command
    49#
    50# Calling build.Import in build.FindOnly mode on an import path of a Go package
    51# that produces errors when loading (e.g., due to build constraints not matching
    52# the current build context) should return the package directory and nil error.
    53
    54# Issue 31603: Import with non-empty srcDir should work.
    55env GO111MODULE=on
    56exec $WORK/testfindonly$GOEXE gobuild.example.com/x/y/z/i $WORK
    57! stdout 'build constraints'
    58stdout '^dir='$WORK'.+i err=<nil>$'
    59
    60# Issue 37153: Import with empty srcDir should work.
    61env GO111MODULE=on
    62exec $WORK/testfindonly$GOEXE gobuild.example.com/x/y/z/i ''
    63! stdout 'build constraints'
    64stdout '^dir='$WORK'.+i err=<nil>$'
    65
    66-- go.mod --
    67module gobuild.example.com/x/y/z
    68
    69-- z.go --
    70package z
    71
    72-- w/w1.go --
    73package w
    74
    75-- i/i.go --
    76// +build i
    77
    78package i
    79
    80-- testimport/x.go --
    81package main
    82
    83import (
    84	"fmt"
    85	"go/build"
    86	"log"
    87	"os"
    88	"path/filepath"
    89	"strings"
    90)
    91
    92func main() {
    93	// build.Import should support relative and absolute source dir paths.
    94	path := os.Args[1]
    95	srcDir := os.Args[2]
    96	p1, err := build.Import(path, srcDir, 0)
    97	if err != nil {
    98		log.Fatal(err)
    99	}
   100	absSrcDir, err := filepath.Abs(srcDir)
   101	if err != nil {
   102		log.Fatal(err)
   103	}
   104	p2, err := build.Import(path, absSrcDir, 0)
   105	if err != nil {
   106		log.Fatal(err)
   107	}
   108	if p1.Dir != p2.Dir {
   109		log.Fatalf("different packages loaded with relative and absolute paths:\n\t%s\n\t%s", p1.Dir, p2.Dir)
   110	}
   111
   112	fmt.Printf("%s\n%s\n", p1.Dir, strings.Join(p1.GoFiles, " "))
   113}
   114
   115-- testfindonly/x.go --
   116package main
   117
   118import (
   119	"fmt"
   120	"go/build"
   121	"os"
   122)
   123
   124func main() {
   125	p, err := build.Import(os.Args[1], os.Args[2], build.FindOnly)
   126	fmt.Printf("dir=%s err=%v\n", p.Dir, err)
   127}
   128
   129-- $GOPATH/other/go.mod --
   130module other/x/y
   131
   132-- $GOPATH/other/z/w/w2.go --
   133package w

View as plain text