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

     1  env GO111MODULE=on
     2  [short] skip
     3  
     4  # This script tests commands in module mode outside of any module.
     5  #
     6  # First, ensure that we really are in module mode, and that we really don't have
     7  # a go.mod file.
     8  go env GOMOD
     9  stdout 'NUL|/dev/null'
    10  
    11  
    12  # 'go list' without arguments implicitly operates on the current directory,
    13  # which is not in a module.
    14  ! go list
    15  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    16  go list -m
    17  stdout '^command-line-arguments$'
    18  # 'go list' in the working directory should fail even if there is a a 'package
    19  # main' present: without a main module, we do not know its package path.
    20  ! go list ./needmod
    21  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    22  
    23  # 'go list all' lists the transitive import graph of the main module,
    24  # which is empty if there is no main module.
    25  go list all
    26  ! stdout .
    27  stderr 'warning: "all" matched no packages'
    28  
    29  # 'go list' on standard-library packages should work, since they do not depend
    30  # on the contents of any module.
    31  go list -deps cmd
    32  stdout '^fmt$'
    33  stdout '^cmd/go$'
    34  
    35  go list $GOROOT/src/fmt
    36  stdout '^fmt$'
    37  
    38  # 'go list' should work with file arguments.
    39  go list ./needmod/needmod.go
    40  stdout 'command-line-arguments'
    41  
    42  # 'go list' on a package from a module should fail.
    43  ! go list example.com/printversion
    44  stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    45  
    46  
    47  # 'go list -m' with an explicit version should resolve that version.
    48  go list -m example.com/version@latest
    49  stdout 'example.com/version v1.1.0'
    50  
    51  # 'go list -m -versions' should succeed even without an explicit version.
    52  go list -m -versions example.com/version
    53  stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'
    54  
    55  # 'go list -m all' should fail. "all" is not meaningful outside of a module.
    56  ! go list -m all
    57  stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    58  
    59  # 'go list -m <mods> all' should also fail.
    60  ! go list -m example.com/printversion@v1.0.0 all
    61  stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    62  ! stdout 'example.com/version'
    63  
    64  # 'go list -m <mods>' should fail if any of the mods lacks an explicit version.
    65  ! go list -m example.com/printversion
    66  stderr 'go: cannot match "example.com/printversion" without -versions or an explicit version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    67  ! stdout 'example.com/version'
    68  
    69  # 'go list -m' with wildcards should fail. Wildcards match modules in the
    70  # build list, so they aren't meaningful outside a module.
    71  ! go list -m ...
    72  stderr 'go: cannot match "...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    73  ! go list -m rsc.io/quote/...
    74  stderr 'go: cannot match "rsc.io/quote/...": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    75  
    76  
    77  # 'go clean' should skip the current directory if it isn't in a module.
    78  go clean -n
    79  ! stdout .
    80  ! stderr .
    81  
    82  # 'go mod graph' should fail, since there's no module graph.
    83  ! go mod graph
    84  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    85  
    86  # 'go mod why' should fail, since there is no main module to depend on anything.
    87  ! go mod why -m example.com/version
    88  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    89  
    90  # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
    91  # there is no go.mod file to edit.
    92  ! go mod tidy
    93  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    94  ! go mod edit -fmt
    95  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    96  ! go mod edit -require example.com/version@v1.0.0
    97  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
    98  
    99  
   100  # 'go mod download' without arguments should report an error.
   101  ! go mod download
   102  stderr 'no modules specified'
   103  
   104  # 'go mod download' should download exactly the requested module without dependencies.
   105  rm -r $GOPATH/pkg/mod/cache/download/example.com
   106  go mod download example.com/printversion@v1.0.0
   107  exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
   108  ! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip
   109  
   110  # 'go mod download all' should fail. "all" is not meaningful outside of a module.
   111  ! go mod download all
   112  stderr 'go: cannot match "all": go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   113  
   114  
   115  # 'go mod vendor' should fail: it starts by clearing the existing vendor
   116  # directory, and we don't know where that is.
   117  ! go mod vendor
   118  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   119  
   120  
   121  # 'go mod verify' should fail: we have no modules to verify.
   122  ! go mod verify
   123  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   124  
   125  
   126  # 'go get' has no go.mod file to update outside a module and should fail.
   127  ! go get
   128  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   129  stderr '^\t''go get'' is no longer supported outside a module.$'
   130  ! go get -u
   131  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   132  stderr '^\t''go get'' is no longer supported outside a module.$'
   133  ! go get -u ./needmod
   134  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   135  stderr '^\t''go get'' is no longer supported outside a module.$'
   136  ! go get -u all
   137  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   138  stderr '^\t''go get'' is no longer supported outside a module.$'
   139  ! go get example.com/printversion@v1.0.0 example.com/version@none
   140  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   141  stderr '^\t''go get'' is no longer supported outside a module.$'
   142  
   143  # 'go get' should not download anything.
   144  go clean -modcache
   145  ! go get example.com/printversion@v1.0.0
   146  stderr '^go: go.mod file not found in current directory or any parent directory.$'
   147  stderr '^\t''go get'' is no longer supported outside a module.$'
   148  ! exists $GOPATH/pkg/mod/example.com/printversion@v1.0.0
   149  ! exists $GOPATH/pkg/mod/example.com/version@v1.0.0
   150  
   151  
   152  # 'go build' without arguments implicitly operates on the current directory, and should fail.
   153  cd needmod
   154  ! go build
   155  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   156  cd ..
   157  
   158  # 'go build' of a non-module directory should fail too.
   159  ! go build ./needmod
   160  stderr '^go: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   161  
   162  # 'go build' of source files should fail if they import anything outside std.
   163  ! go build -n ./needmod/needmod.go
   164  stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   165  
   166  # 'go build' of source files should succeed if they do not import anything outside std.
   167  go build -n -o ignore ./stdonly/stdonly.go
   168  
   169  # 'go build' should succeed for standard-library packages.
   170  go build -n fmt
   171  
   172  # 'go build' should use the latest version of the Go language.
   173  go build ./newgo/newgo.go
   174  
   175  # 'go doc' without arguments implicitly operates on the current directory, and should fail.
   176  # TODO(golang.org/issue/32027): currently, it succeeds.
   177  cd needmod
   178  go doc
   179  cd ..
   180  
   181  # 'go doc' of a non-module directory should also succeed.
   182  go doc ./needmod
   183  
   184  # 'go doc' should succeed for standard-library packages.
   185  go doc fmt
   186  
   187  # 'go doc' should fail for a package path outside a module.
   188  ! go doc example.com/version
   189  stderr 'doc: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   190  
   191  # 'go install' with a version should succeed if all constraints are met.
   192  # See mod_install_pkg_version.
   193  rm $GOPATH/bin
   194  go install example.com/printversion@v0.1.0
   195  exists $GOPATH/bin/printversion$GOEXE
   196  
   197  # 'go install' should fail if a package argument must be resolved to a module.
   198  ! go install example.com/printversion
   199  stderr '^go: ''go install'' requires a version when current directory is not in a module\n\tTry ''go install example.com/printversion@latest'' to install the latest version$'
   200  
   201  # 'go install' should fail if a source file imports a package that must be
   202  # resolved to a module.
   203  ! go install ./needmod/needmod.go
   204  stderr 'needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   205  
   206  # 'go run' should fail if a package argument must be resolved to a module.
   207  ! go run example.com/printversion
   208  stderr '^no required module provides package example.com/printversion: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   209  
   210  # 'go run' should fail if a source file imports a package that must be
   211  # resolved to a module.
   212  ! go run ./needmod/needmod.go
   213  stderr '^needmod[/\\]needmod.go:10:2: no required module provides package example.com/version: go.mod file not found in current directory or any parent directory; see ''go help modules''$'
   214  
   215  
   216  # 'go fmt' should be able to format files outside of a module.
   217  go fmt needmod/needmod.go
   218  
   219  
   220  # The remainder of the test checks dependencies by linking and running binaries.
   221  
   222  # 'go run' should work with file arguments if they don't import anything
   223  # outside std.
   224  go run ./stdonly/stdonly.go
   225  stdout 'path is command-line-arguments$'
   226  stdout 'main is  $'
   227  
   228  # 'go generate' should work with file arguments.
   229  [exec:touch] go generate ./needmod/needmod.go
   230  [exec:touch] exists ./needmod/gen.txt
   231  
   232  # 'go install' should work with file arguments.
   233  go install ./stdonly/stdonly.go
   234  
   235  # 'go test' should work with file arguments.
   236  go test -v ./stdonly/stdonly_test.go
   237  stdout 'stdonly was tested'
   238  
   239  # 'go vet' should work with file arguments.
   240  go vet ./stdonly/stdonly.go
   241  
   242  
   243  -- README.txt --
   244  There is no go.mod file in the working directory.
   245  
   246  -- needmod/needmod.go --
   247  //go:generate touch gen.txt
   248  
   249  package main
   250  
   251  import (
   252  	"fmt"
   253  	"os"
   254  	"runtime/debug"
   255  
   256  	_ "example.com/version"
   257  )
   258  
   259  func main() {
   260  	info, ok := debug.ReadBuildInfo()
   261  	if !ok {
   262  		panic("missing build info")
   263  	}
   264  	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
   265  	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
   266  	for _, m := range info.Deps {
   267  		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
   268  	}
   269  }
   270  
   271  -- stdonly/stdonly.go --
   272  package main
   273  
   274  import (
   275  	"fmt"
   276  	"os"
   277  	"runtime/debug"
   278  )
   279  
   280  func main() {
   281  	info, ok := debug.ReadBuildInfo()
   282  	if !ok {
   283  		panic("missing build info")
   284  	}
   285  	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
   286  	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
   287  	for _, m := range info.Deps {
   288  		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
   289  	}
   290  }
   291  
   292  -- stdonly/stdonly_test.go --
   293  package main
   294  
   295  import (
   296  	"fmt"
   297  	"testing"
   298  )
   299  
   300  func Test(t *testing.T) {
   301  	fmt.Println("stdonly was tested")
   302  }
   303  
   304  -- newgo/newgo.go --
   305  // Package newgo requires Go 1.14 or newer.
   306  package newgo
   307  
   308  import "io"
   309  
   310  const C = 299_792_458
   311  
   312  type ReadWriteCloser interface {
   313  	io.ReadCloser
   314  	io.WriteCloser
   315  }
   316  

View as plain text