this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: ensure module is tidy before 'mod publish' publishes

Fixes a TODO in the code, and also ensures that the user
is presented with a helpful suggestion to run `cue mod tidy`
rather than being given a generic "module is not tidy" error message
from the central registry over HTTP.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I2bc3d6b8ceda38ea0ad4074121602082eb056921
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195731
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+33 -12
+14 -1
cmd/cue/cmd/modpublish.go
··· 36 36 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 37 37 "github.com/spf13/cobra" 38 38 39 + "cuelang.org/go/internal/mod/modload" 39 40 "cuelang.org/go/internal/mod/semver" 40 41 "cuelang.org/go/internal/vcs" 41 42 "cuelang.org/go/mod/modconfig" ··· 111 112 if err != nil { 112 113 return err 113 114 } 114 - // TODO ensure module tidiness. 115 + 116 + // Ensure that the module is tidy before publishing it. 117 + // TODO: can we use modload.CheckTidy on the already-parsed modfile.File below? 118 + // TODO: we might want to provide a "force" flag to skip this check, 119 + // particularly for cases where one has private deps or pushes to a custom registry. 120 + reg, err := getCachedRegistry() 121 + if err != nil { 122 + return err 123 + } 124 + if err := modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg); err != nil { 125 + return suggestModTidy(err) 126 + } 127 + 115 128 modPath := filepath.Join(modRoot, "cue.mod/module.cue") 116 129 modfileData, err := os.ReadFile(modPath) 117 130 if err != nil {
+15 -9
cmd/cue/cmd/modtidy.go
··· 67 67 } 68 68 if flagCheck.Bool(cmd) { 69 69 err := modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg) 70 - notTidyErr := new(modload.ErrModuleNotTidy) 71 - if errors.As(err, &notTidyErr) { 72 - if notTidyErr.Reason == "" { 73 - err = fmt.Errorf("module is not tidy, use 'cue mod tidy'") 74 - } else { 75 - err = fmt.Errorf("module is not tidy, use 'cue mod tidy': %v", notTidyErr.Reason) 76 - } 77 - } 78 - return err 70 + return suggestModTidy(err) 79 71 } 80 72 mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg) 81 73 if err != nil { ··· 100 92 } 101 93 return nil 102 94 } 95 + 96 + // suggestModTidy rewrites [modload.ErrModuleNotTidy] errors 97 + // so that they suggest running `cue mod tidy` to the user. 98 + func suggestModTidy(err error) error { 99 + notTidyErr := new(modload.ErrModuleNotTidy) 100 + if errors.As(err, &notTidyErr) { 101 + if notTidyErr.Reason == "" { 102 + err = fmt.Errorf("module is not tidy, use 'cue mod tidy'") 103 + } else { 104 + err = fmt.Errorf("module is not tidy, use 'cue mod tidy': %v", notTidyErr.Reason) 105 + } 106 + } 107 + return err 108 + }
+4 -2
cmd/cue/cmd/testdata/script/modpublish_non_tidy.txtar
··· 5 5 memregistry MEMREGISTRY 6 6 env CUE_REGISTRY=$MEMREGISTRY+insecure 7 7 8 - # TODO(mvdan): mod publish does not check tidiness right now. 9 - exec cue mod publish v1.0.0 8 + ! exec cue mod publish v1.0.0 9 + cmp stderr publish.stderr 10 10 11 11 # Sanity check that we can tidy the module and then publish again. 12 12 # Note that the dependency is in the original registry populated from _registry/. ··· 18 18 19 19 exec cue mod publish v1.0.1 20 20 21 + -- publish.stderr -- 22 + module is not tidy, use 'cue mod tidy': missing dependency providing package example.com@v0:main 21 23 -- cue.mod/module.cue -- 22 24 module: "main.org@v1" 23 25 language: version: "v0.9.0"