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

     1  # https://golang.org/issue/46141: 'go mod tidy' for a Go 1.17 module should by
     2  # default preserve enough checksums for the module to be used by Go 1.16.
     3  #
     4  # We don't have a copy of Go 1.16 handy, but we can simulate it by editing the
     5  # 'go' version in the go.mod file to 1.16, without actually updating the
     6  # requirements to match.
     7  
     8  [short] skip
     9  
    10  env MODFMT='{{with .Module}}{{.Path}} {{.Version}}{{end}}'
    11  
    12  
    13  # For this module, Go 1.17 prunes out a (transitive and otherwise-irrelevant)
    14  # requirement on a retracted higher version of a dependency.
    15  # However, when Go 1.16 reads the same requirements from the go.mod file,
    16  # it does not prune out that requirement, and selects the retracted version.
    17  #
    18  # The Go 1.16 module graph looks like:
    19  #
    20  # m ---- lazy v0.1.0 ---- requireincompatible v0.1.0 ---- incompatible v2.0.0+incompatible
    21  # |        |
    22  # + -------+------------- incompatible v1.0.0
    23  #
    24  # The Go 1.17 module graph is the same except that the dependencies of
    25  # requireincompatible are pruned out (because the module that requires
    26  # it — lazy v0.1.0 — specifies 'go 1.17', and it is not otherwise relevant to
    27  # the main module).
    28  
    29  
    30  # 'go mod tidy' should by default diagnose the difference in dependencies as an
    31  # error, with useful suggestions about how to resolve it.
    32  
    33  cp go.mod go.mod.orig
    34  ! go mod tidy
    35  stderr '^go: example\.com/m imports\n\texample\.net/lazy imports\n\texample\.com/retract/incompatible loaded from example\.com/retract/incompatible@v1\.0\.0,\n\tbut go 1\.16 would select v2\.0\.0\+incompatible\n\n'
    36  stderr '\n\nTo upgrade to the versions selected by go 1\.16:\n\tgo mod tidy -go=1\.16 && go mod tidy -go=1\.17\nIf reproducibility with go 1\.16 is not needed:\n\tgo mod tidy -compat=1.17\nFor other options, see:\n\thttps://golang\.org/doc/modules/pruning\n'
    37  
    38  cmp go.mod go.mod.orig
    39  
    40  
    41  # The suggested '-compat' flag to ignore differences should silence the error
    42  # and leave go.mod unchanged, resulting in checksum errors when Go 1.16 tries
    43  # to load a module pruned out by Go 1.17.
    44  
    45  go mod tidy -compat=1.17
    46  ! stderr .
    47  cmp go.mod go.mod.orig
    48  
    49  go mod edit -go=1.16
    50  ! go list -f $MODFMT -deps ./...
    51  stderr -count=1 '^go: example\.net/lazy@v0\.1\.0 requires\n\texample\.net/requireincompatible@v0\.1\.0 requires\n\texample\.com/retract/incompatible@v2\.0\.0\+incompatible: missing go.sum entry for go.mod file; to add it:\n\tgo mod download example.com/retract/incompatible$'
    52  
    53  
    54  # There are two ways for the module author to bring the two into alignment.
    55  # One is to *explicitly* 'exclude' the version that is already *implicitly*
    56  # pruned out under 1.17.
    57  
    58  go mod edit -exclude=example.com/retract/incompatible@v2.0.0+incompatible
    59  go list -f $MODFMT -deps ./...
    60  stdout '^example.com/retract/incompatible v1\.0\.0$'
    61  ! stdout 'v2\.0\.0'
    62  
    63  
    64  # The other is to explicitly upgrade the version required under Go 1.17
    65  # to match the version selected by Go 1.16. The commands suggested by
    66  # 'go mod tidy' should do exactly that.
    67  
    68  cp go.mod.orig go.mod
    69  
    70  go mod tidy -go=1.16
    71  go list -f $MODFMT -deps ./...
    72  stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
    73  ! stdout 'v1\.0\.0'
    74  
    75  go mod tidy -go=1.17
    76  go list -f $MODFMT -deps ./...
    77  stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
    78  ! stdout 'v1\.0\.0'
    79  
    80  go mod edit -go=1.16
    81  go list -f $MODFMT -deps ./...
    82  stdout '^example.com/retract/incompatible v2\.0\.0\+incompatible$'
    83  ! stdout 'v1\.0\.0'
    84  
    85  
    86  -- go.mod --
    87  // Module m indirectly imports a package from
    88  // example.com/retract/incompatible. Its selected version of
    89  // that module is lower under Go 1.17 semantics than under Go 1.16.
    90  module example.com/m
    91  
    92  go 1.17
    93  
    94  replace (
    95  	example.net/lazy v0.1.0 => ./lazy
    96  	example.net/requireincompatible v0.1.0 => ./requireincompatible
    97  )
    98  
    99  require example.net/lazy v0.1.0
   100  
   101  require example.com/retract/incompatible v1.0.0 // indirect
   102  -- incompatible.go --
   103  package incompatible
   104  
   105  import _ "example.net/lazy"
   106  
   107  -- lazy/go.mod --
   108  // Module lazy requires example.com/retract/incompatible v1.0.0.
   109  //
   110  // When viewed from the outside it also has a transitive dependency
   111  // on v2.0.0+incompatible, but in lazy mode that transitive dependency
   112  // is pruned out.
   113  module example.net/lazy
   114  
   115  go 1.17
   116  
   117  exclude example.com/retract/incompatible v2.0.0+incompatible
   118  
   119  require (
   120  	example.com/retract/incompatible v1.0.0
   121  	example.net/requireincompatible v0.1.0
   122  )
   123  -- lazy/lazy.go --
   124  package lazy
   125  
   126  import _ "example.com/retract/incompatible"
   127  
   128  -- requireincompatible/go.mod --
   129  module example.net/requireincompatible
   130  
   131  go 1.15
   132  
   133  require example.com/retract/incompatible v2.0.0+incompatible
   134  

View as plain text