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

     1  # Integration test for golang.org/issue/33848: automatically check and use vendored packages.
     2  
     3  env GO111MODULE=on
     4  
     5  [short] skip
     6  
     7  cd $WORK/auto
     8  cp go.mod go.mod.orig
     9  cp $WORK/modules-1.13.txt $WORK/auto/modules.txt
    10  
    11  # An explicit -mod=vendor should force use of the vendor directory.
    12  env GOFLAGS=-mod=vendor
    13  
    14  # Pass -e to permit an error: tools.go imports a main package
    15  # "example.com/printversion".
    16  # TODO(#59186): investigate why it didn't fail without -e.
    17  go list -f {{.Dir}} -tags tools -e all
    18  stdout '^'$WORK'[/\\]auto$'
    19  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
    20  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
    21  
    22  ! go list -m all
    23  stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
    24  
    25  ! go list -m -f '{{.Dir}}' all
    26  stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
    27  
    28  # An explicit -mod=mod should force the vendor directory to be ignored.
    29  env GOFLAGS=-mod=mod
    30  
    31  go list -f {{.Dir}} -tags tools -e all
    32  stdout '^'$WORK'[/\\]auto$'
    33  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    34  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    35  
    36  go list -m all
    37  stdout '^example.com/auto$'
    38  stdout 'example.com/printversion v1.0.0'
    39  stdout 'example.com/version v1.0.0'
    40  
    41  go list -m -f '{{.Dir}}' all
    42  stdout '^'$WORK'[/\\]auto$'
    43  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    44  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    45  
    46  # If the main module's "go" directive says 1.13, we should default to -mod=mod.
    47  env GOFLAGS=
    48  go mod edit -go=1.13
    49  
    50  go list -f {{.Dir}} -tags tools -e all
    51  stdout '^'$WORK'[/\\]auto$'
    52  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    53  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    54  
    55  go list -m -f '{{.Dir}}' all
    56  stdout '^'$WORK'[/\\]auto$'
    57  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    58  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    59  
    60  # A 'go 1.14' directive in the main module's go.mod file should enable
    61  # -mod=vendor by default, along with stronger checks for consistency
    62  # between the go.mod file and vendor/modules.txt.
    63  # A 'go 1.13' vendor/modules.txt file is not usually sufficient
    64  # to pass those checks.
    65  go mod edit -go=1.14
    66  
    67  ! go list -f {{.Dir}} -tags tools all
    68  stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
    69  stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
    70  stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
    71  stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
    72  stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
    73  
    74  # Module-specific subcommands should continue to load the full module graph.
    75  go mod graph
    76  stdout '^example.com/printversion@v1.0.0 example.com/version@v1.0.0$'
    77  
    78  # An explicit -mod=mod should still force the vendor directory to be ignored.
    79  env GOFLAGS=-mod=mod
    80  
    81  go list -f {{.Dir}} -tags tools -e all
    82  stdout '^'$WORK'[/\\]auto$'
    83  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    84  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    85  
    86  go list -m all
    87  stdout '^example.com/auto$'
    88  stdout 'example.com/printversion v1.0.0'
    89  stdout 'example.com/version v1.0.0'
    90  
    91  go list -m -f '{{.Dir}}' all
    92  stdout '^'$WORK'[/\\]auto$'
    93  stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    94  stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    95  
    96  # 'go mod vendor' should repair vendor/modules.txt so that the implicit
    97  # -mod=vendor works again.
    98  env GOFLAGS=
    99  
   100  go mod edit -go=1.14
   101  go mod vendor
   102  
   103  go list -f {{.Dir}} -tags tools -e all
   104  stdout '^'$WORK'[/\\]auto$'
   105  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   106  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   107  
   108  # ...but 'go list -m' should continue to fail, this time without
   109  # referring to a -mod default that the user didn't set.
   110  ! go list -m all
   111  stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
   112  
   113  ! go list -m -f '{{.Dir}}' all
   114  stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
   115  
   116  
   117  # 'go mod init' should work if there is already a GOPATH-mode vendor directory
   118  # present. If there are no module dependencies, -mod=vendor should be used by
   119  # default and should not fail the consistency check even though no module
   120  # information is present.
   121  
   122  rm go.mod
   123  rm vendor/modules.txt
   124  
   125  go mod init example.com/auto
   126  go list -f {{.Dir}} -tags tools -e all
   127  stdout '^'$WORK'[/\\]auto$'
   128  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   129  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   130  
   131  # If information about dependencies is added to a 1.14 go.mod file, subsequent
   132  # list commands should error out if vendor/modules.txt is missing or incomplete.
   133  
   134  cp go.mod.orig go.mod
   135  go mod edit -go=1.14
   136  ! go list -f {{.Dir}} -tags tools -e all
   137  stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
   138  stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
   139  stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
   140  stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
   141  stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
   142  
   143  # If -mod=vendor is set, limited consistency checks should apply even when
   144  # the go version is 1.13 or earlier.
   145  # An incomplete or missing vendor/modules.txt should resolve the vendored packages...
   146  go mod edit -go=1.13
   147  go list -mod=vendor -f {{.Dir}} -tags tools -e all
   148  stdout '^'$WORK'[/\\]auto$'
   149  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   150  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   151  
   152  # ...but a version mismatch for an explicit dependency should be noticed.
   153  cp $WORK/modules-bad-1.13.txt vendor/modules.txt
   154  ! go list -mod=vendor -f {{.Dir}} -tags tools all
   155  stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
   156  stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/printversion@v1.1.0$'
   157  stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
   158  
   159  # If the go version is still 1.13, 'go mod vendor' should write a
   160  # matching vendor/modules.txt containing the corrected 1.13 data.
   161  go mod vendor
   162  cmp $WORK/modules-1.13.txt vendor/modules.txt
   163  
   164  go list -mod=vendor -f {{.Dir}} -tags tools -e all
   165  stdout '^'$WORK'[/\\]auto$'
   166  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   167  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   168  
   169  # When the version is upgraded to 1.14, 'go mod vendor' should write a
   170  # vendor/modules.txt with the updated 1.14 annotations.
   171  go mod edit -go=1.14
   172  go mod vendor
   173  cmp $WORK/modules-1.14.txt vendor/modules.txt
   174  
   175  # Then, -mod=vendor should kick in automatically and succeed.
   176  go list -f {{.Dir}} -tags tools -e all
   177  stdout '^'$WORK'[/\\]auto$'
   178  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   179  stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   180  
   181  # 'go get' should update from the network or module cache,
   182  # even if a vendor directory is present.
   183  go get example.com/version@v1.1.0
   184  ! go list -f {{.Dir}} -tags tools all
   185  stderr '^go: inconsistent vendoring'
   186  
   187  -- $WORK/auto/go.mod --
   188  module example.com/auto
   189  
   190  go 1.13
   191  
   192  require example.com/printversion v1.0.0
   193  
   194  replace (
   195  	example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
   196  	example.com/version v1.0.0 => ./replacement-version
   197  	example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
   198  )
   199  -- $WORK/auto/tools.go --
   200  // +build tools
   201  
   202  package auto
   203  
   204  import _ "example.com/printversion"
   205  -- $WORK/auto/auto.go --
   206  package auto
   207  -- $WORK/auto/replacement-version/go.mod --
   208  module example.com/version
   209  -- $WORK/auto/replacement-version/version.go --
   210  package version
   211  
   212  const V = "v1.0.0-replaced"
   213  -- $WORK/modules-1.14.txt --
   214  # example.com/printversion v1.0.0
   215  ## explicit
   216  example.com/printversion
   217  # example.com/version v1.0.0 => ./replacement-version
   218  example.com/version
   219  # example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
   220  # example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
   221  -- $WORK/modules-1.13.txt --
   222  # example.com/printversion v1.0.0
   223  example.com/printversion
   224  # example.com/version v1.0.0 => ./replacement-version
   225  example.com/version
   226  -- $WORK/modules-bad-1.13.txt --
   227  # example.com/printversion v1.1.0
   228  example.com/printversion
   229  # example.com/version v1.1.0
   230  example.com/version
   231  -- $WORK/auto/vendor/example.com/printversion/go.mod --
   232  module example.com/printversion
   233  
   234  require example.com/version v1.0.0
   235  replace example.com/version v1.0.0 => ../oops v0.0.0
   236  exclude example.com/version v1.0.1
   237  -- $WORK/auto/vendor/example.com/printversion/printversion.go --
   238  package main
   239  
   240  import (
   241  	"fmt"
   242  	"os"
   243  	"runtime/debug"
   244  
   245  	_ "example.com/version"
   246  )
   247  
   248  func main() {
   249  	info, _ := debug.ReadBuildInfo()
   250  	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
   251  	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
   252  	for _, m := range info.Deps {
   253  		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
   254  	}
   255  }
   256  -- $WORK/auto/vendor/example.com/version/version.go --
   257  package version
   258  
   259  const V = "v1.0.0-replaced"
   260  

View as plain text