Skip to content

Commit

Permalink
Improve replace directive tooling messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Oct 24, 2024
1 parent bb97cd3 commit fc673e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
8 changes: 6 additions & 2 deletions tooling/go/cmd/local-overrides-enforcer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ func main() {
if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err)
}
expectedContents, ok, err := checker.CheckFile(path, content, parentModule)
expectedContents, ok, missingReplaces, err := checker.CheckFile(path, content, parentModule)
if err != nil {
return fmt.Errorf("failed to check %s: %w", path, err)
}
if !ok {
sawBadFile = true
fmt.Printf("⚠️ File at path %s didn't have some local overrides - its contents should be:\n%s\n", path, expectedContents)
missingReplacesStr := ""
for _, missingReplace := range missingReplaces {
missingReplacesStr += " - " + missingReplace + "\n"
}
fmt.Printf("⚠️ File at path %s didn't have some replace directives.\nMissing replace directives for modules:\n%s\nYou should replace %s with exactly this string:\n%s\n", path, missingReplacesStr, path, expectedContents)
}
return nil
})
Expand Down
26 changes: 13 additions & 13 deletions tooling/go/internal/local-overrides-enforcer/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import (

// CheckFile checks that a go.mod file has local overrides for all children of the passed parent module.
// It returns one of:
// - If the contents was correct: "", true, nil
// - If the contents was not correct: the expected contents, false, nil
// - If an error occurred: "", false, error
func CheckFile(goModPath string, contents []byte, parentModule string) (string, bool, error) {
// - If the contents was correct: "", true, nil, nil
// - If the contents was not correct: the expected contents, false, []string{"missing/override"...}, nil
// - If an error occurred: "", false, nil, error
func CheckFile(goModPath string, contents []byte, parentModule string) (string, bool, []string, error) {
gomodFile, err := modfile.Parse(goModPath, contents, nil)
if err != nil {
return "", false, fmt.Errorf("failed to parse %s as go.mod file: %w", goModPath, err)
return "", false, nil, fmt.Errorf("failed to parse %s as go.mod file: %w", goModPath, err)
}

parentModuleWithTrailingSlash := parentModule + "/"

if !strings.HasPrefix(gomodFile.Module.Mod.Path, parentModuleWithTrailingSlash) {
return "", false, fmt.Errorf("module at path %s was named %s which isn't a child of %s", goModPath, gomodFile.Module.Mod.Path, parentModule)
return "", false, nil, fmt.Errorf("module at path %s was named %s which isn't a child of %s", goModPath, gomodFile.Module.Mod.Path, parentModule)
}
slashCount := strings.Count(gomodFile.Module.Mod.Path[len(parentModule):], "/")

Expand All @@ -30,7 +30,7 @@ func CheckFile(goModPath string, contents []byte, parentModule string) (string,
replaces[replace.Old.Path] = struct{}{}
}

missingReplaces := false
var missingReplaces []string
for _, require := range gomodFile.Require {
modPath := require.Mod.Path
if !strings.HasPrefix(modPath, parentModuleWithTrailingSlash) {
Expand All @@ -39,18 +39,18 @@ func CheckFile(goModPath string, contents []byte, parentModule string) (string,
if _, isReplaced := replaces[modPath]; isReplaced {
continue
}
missingReplaces = true
missingReplaces = append(missingReplaces, modPath)
rel := modPath[len(parentModuleWithTrailingSlash):]
if err := gomodFile.AddReplace(modPath, "", strings.Repeat("../", slashCount)+rel, ""); err != nil {
return "", false, fmt.Errorf("failed to add replace: %w", err)
return "", false, nil, fmt.Errorf("failed to add replace: %w", err)
}
}
if missingReplaces {
if len(missingReplaces) != 0 {
formatted, err := gomodFile.Format()
if err != nil {
return "", false, fmt.Errorf("failed to serialize go.mod file: %w", err)
return "", false, nil, fmt.Errorf("failed to serialize go.mod file: %w", err)
}
return string(formatted), false, nil
return string(formatted), false, missingReplaces, nil
}
return "", true, nil
return "", true, nil, nil
}

0 comments on commit fc673e1

Please sign in to comment.