this repo has no description
0
fork

Configure Feed

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

cmd/cue: suggest `cue mod tidy` when --check says "not tidy"

When a user runs `cue mod tidy --check` and it says "not tidy",
it might not be obvious that they should run `cue mod tidy`.
This is particularly important for the central registry,
which enforces that published modules are tidy via --check:

cue mod publish v0.0.8 cannot put module: [...]: module is not tidy

The central registry should suggest the user what to do to resolve
the issue, and in the majority of cases, it's to run 'cue mod tidy'.

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

+31 -5
+11 -1
cmd/cue/cmd/modtidy.go
··· 16 16 17 17 import ( 18 18 "bytes" 19 + "errors" 19 20 "fmt" 20 21 "os" 21 22 "path/filepath" ··· 66 67 return err 67 68 } 68 69 if flagCheck.Bool(cmd) { 69 - return modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg) 70 + err := modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg) 71 + notTidyErr := new(modload.ErrModuleNotTidy) 72 + if errors.As(err, &notTidyErr) { 73 + if notTidyErr.Reason == "" { 74 + err = fmt.Errorf("module is not tidy, use 'cue mod tidy'") 75 + } else { 76 + err = fmt.Errorf("module is not tidy, use 'cue mod tidy': %v", notTidyErr.Reason) 77 + } 78 + } 79 + return err 70 80 } 71 81 mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, cueversion.LanguageVersion()) 72 82 if err != nil {
+1 -1
cmd/cue/cmd/testdata/script/modtidy_check_fail.txtar
··· 4 4 cmp stderr want-stderr 5 5 6 6 -- want-stderr -- 7 - module is not tidy: cannot find module providing package example.com@v0:main 7 + module is not tidy, use 'cue mod tidy': cannot find module providing package example.com@v0:main 8 8 -- cue.mod/module.cue -- 9 9 module: "main.org@v0" 10 10 language: version: "v0.8.0"
+19 -3
internal/mod/modload/tidy.go
··· 101 101 return nil, fmt.Errorf("cannot tidy requirements: %v", err) 102 102 } 103 103 if ld.checkTidy && !equalRequirements(origRs, rs) { 104 - // TODO be more specific in this error? 105 - return nil, fmt.Errorf("module is not tidy") 104 + // TODO: provide a reason, perhaps in structured form rather than a string 105 + return nil, &ErrModuleNotTidy{} 106 106 } 107 107 return modfileFromRequirements(mf, rs, cueVers), nil 108 + } 109 + 110 + // ErrModuleNotTidy is returned by CheckTidy when a module is not tidy, 111 + // such as when there are missing or unnecessary dependencies listed. 112 + type ErrModuleNotTidy struct { 113 + // Reason summarizes why the module is not tidy. 114 + Reason string 115 + } 116 + 117 + func (e ErrModuleNotTidy) Error() string { 118 + if e.Reason == "" { 119 + return "module is not tidy" 120 + } 121 + return "module is not tidy: " + e.Reason 108 122 } 109 123 110 124 func equalRequirements(rs0, rs1 *modrequirements.Requirements) bool { ··· 171 185 if ld.checkTidy { 172 186 for _, pkg := range pkgs.All() { 173 187 if err := pkg.Error(); err != nil { 174 - return nil, nil, fmt.Errorf("module is not tidy: %v", err) 188 + // TODO: transform "cannot find module" error as it may be confusing 189 + // when the module does exist in the registry. 190 + return nil, nil, &ErrModuleNotTidy{Reason: err.Error()} 175 191 } 176 192 } 177 193 // All packages could be loaded OK so there are no new