Skip to content

Commit 20d5ce0

Browse files
committed
x/exp/cmd/gorelease: specify which incomplete requirements were missing
Fixes golang/go#38164 Change-Id: I71a3edeb4445c568ae7a94f7c30da68682104f02 Reviewed-on: https://go-review.googlesource.com/c/exp/+/253039 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Trust: Jay Conrod <[email protected]> Trust: Jean de Klerk <[email protected]>
1 parent ae8ad44 commit 20d5ce0

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

cmd/gorelease/gorelease.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -957,17 +957,17 @@ func loadPackages(modPath, modRoot, loadDir string, goModData, goSumData []byte)
957957

958958
// Report new requirements in go.mod.
959959
goModPath := filepath.Join(loadDir, "go.mod")
960-
loadReqs := func(data []byte) (string, error) {
960+
loadReqs := func(data []byte) ([]string, error) {
961961
modFile, err := modfile.ParseLax(goModPath, data, nil)
962962
if err != nil {
963-
return "", err
963+
return nil, err
964964
}
965965
lines := make([]string, len(modFile.Require))
966966
for i, req := range modFile.Require {
967967
lines[i] = req.Mod.String()
968968
}
969969
sort.Strings(lines)
970-
return strings.Join(lines, "\n"), nil
970+
return lines, nil
971971
}
972972

973973
oldReqs, err := loadReqs(goModData)
@@ -983,21 +983,30 @@ func loadPackages(modPath, modRoot, loadDir string, goModData, goSumData []byte)
983983
return nil, nil, err
984984
}
985985

986-
goModChanged := oldReqs != newReqs
987-
if goModChanged {
988-
diagnostics = append(diagnostics, "go.mod: requirements are incomplete.\nRun 'go mod tidy' to add missing requirements.")
986+
oldMap := make(map[string]bool)
987+
for _, req := range oldReqs {
988+
oldMap[req] = true
989989
}
990-
991-
if !goModChanged {
992-
newGoSumData, err := ioutil.ReadFile(filepath.Join(loadDir, "go.sum"))
993-
if err != nil && !os.IsNotExist(err) {
994-
return nil, nil, err
995-
}
996-
if !bytes.Equal(goSumData, newGoSumData) {
997-
diagnostics = append(diagnostics, "go.sum: one or more sums are missing.\nRun 'go mod tidy' to add missing sums.")
990+
var missing []string
991+
for _, req := range newReqs {
992+
if !oldMap[req] {
993+
missing = append(missing, req)
998994
}
999995
}
1000996

997+
if len(missing) > 0 {
998+
diagnostics = append(diagnostics, fmt.Sprintf("go.mod: the following requirements are needed\n\t%s\nRun 'go mod tidy' to add missing requirements.", strings.Join(missing, "\n\t")))
999+
return pkgs, diagnostics, nil
1000+
}
1001+
1002+
newGoSumData, err := ioutil.ReadFile(filepath.Join(loadDir, "go.sum"))
1003+
if err != nil && !os.IsNotExist(err) {
1004+
return nil, nil, err
1005+
}
1006+
if !bytes.Equal(goSumData, newGoSumData) {
1007+
diagnostics = append(diagnostics, "go.sum: one or more sums are missing.\nRun 'go mod tidy' to add missing sums.")
1008+
}
1009+
10011010
return pkgs, diagnostics, nil
10021011
}
10031012

cmd/gorelease/testdata/tidy/misleading_req.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ package tidy
2222
import _ "example.com/tidy/a"
2323
import _ "example.com/tidy/b"
2424
-- want --
25-
go.mod: requirements are incomplete.
25+
go.mod: the following requirements are needed
26+
example.com/tidy/[email protected]
2627
Run 'go mod tidy' to add missing requirements.

cmd/gorelease/testdata/tidy/missing_req.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ mod=example.com/tidy
22
base=v0.0.1
33
success=false
44
-- want --
5-
go.mod: requirements are incomplete.
5+
go.mod: the following requirements are needed
6+
example.com/[email protected]
67
Run 'go mod tidy' to add missing requirements.
78
-- go.mod --
89
module example.com/tidy

0 commit comments

Comments
 (0)