Skip to content

Commit

Permalink
fix(cli): sync gazelle cli code to include latest fixes (#3913)
Browse files Browse the repository at this point in the history
### Type of change

- Bug fix (change which fixes an issue)
- Chore (any other change that doesn't affect source or test files, such
as configuration)

### Test plan

- Covered by existing test cases
- Manual testing; please provide instructions so we can reproduce:

GitOrigin-RevId: 40732bccbda3ec783dcc3c01f889270bf588c698
  • Loading branch information
jbedard authored and gregmagolan committed Dec 5, 2023
1 parent 20fb2ad commit 3eab48b
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 49 deletions.
14 changes: 7 additions & 7 deletions pkg/aspect/configure/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (

var errExit = fmt.Errorf("encountered changes while running diff")

func diffFile(c *config.Config, f *rule.File) (bool, error) {
func diffFile(c *config.Config, f *rule.File) error {
rel, err := filepath.Rel(c.RepoRoot, f.Path)
if err != nil {
return false, fmt.Errorf("error getting old path for file %q: %v", f.Path, err)
return fmt.Errorf("error getting old path for file %q: %v", f.Path, err)
}
rel = filepath.ToSlash(rel)

Expand All @@ -50,13 +50,13 @@ func diffFile(c *config.Config, f *rule.File) (bool, error) {
newContent := f.Format()
if bytes.Equal(newContent, f.Content) {
// No change.
return false, nil
return nil
}

if _, err := os.Stat(f.Path); os.IsNotExist(err) {
diff.FromFile = "/dev/null"
} else if err != nil {
return false, fmt.Errorf("error reading original file: %v", err)
return fmt.Errorf("error reading original file: %v", err)
} else if c.ReadBuildFilesDir == "" {
diff.FromFile = rel
} else {
Expand All @@ -81,11 +81,11 @@ func diffFile(c *config.Config, f *rule.File) (bool, error) {
out = &uc.patchBuffer
}
if err := difflib.WriteUnifiedDiff(out, diff); err != nil {
return false, fmt.Errorf("error diffing %s: %v", f.Path, err)
return fmt.Errorf("error diffing %s: %v", f.Path, err)
}
if ds, _ := difflib.GetUnifiedDiffString(diff); ds != "" {
return false, errExit
return errExit
}

return true, nil
return nil
}
31 changes: 16 additions & 15 deletions pkg/aspect/configure/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"flag"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -56,7 +56,7 @@ type updateConfig struct {
print0 bool
}

type emitFunc func(c *config.Config, f *rule.File) (bool, error)
type emitFunc func(c *config.Config, f *rule.File) error

var modeFromName = map[string]emitFunc{
"print": printFile,
Expand Down Expand Up @@ -382,6 +382,14 @@ func runFixUpdate(wd string, languages []language.Language, cmd command, args []
r.SetKind(repl.KindName)
}
}
for _, r := range empty {
if repl, ok := c.KindMap[r.Kind()]; ok {
mappedKindInfo[repl.KindName] = kinds[r.Kind()]
mappedKinds = append(mappedKinds, repl)
mrslv.MappedKind(rel, repl)
r.SetKind(repl.KindName)
}
}

// Insert or merge rules into the build file.
if f == nil {
Expand Down Expand Up @@ -463,20 +471,18 @@ func runFixUpdate(wd string, languages []language.Language, cmd command, args []
var exit error
for _, v := range visits {
merger.FixLoads(v.file, applyKindMappings(v.mappedKinds, loads))
updated, err := uc.emit(v.c, v.file)
if err != nil {
if err := uc.emit(v.c, v.file); err != nil {
if err == errExit {
exit = err
} else {
log.Print(err)
}
}
if updated {
} else {
stats.NumBuildFilesUpdated++
}
}
if uc.patchPath != "" {
if err := os.WriteFile(uc.patchPath, uc.patchBuffer.Bytes(), 0o666); err != nil {
if err := ioutil.WriteFile(uc.patchPath, uc.patchBuffer.Bytes(), 0o666); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -563,7 +569,7 @@ func fixRepoFiles(c *config.Config, loads []rule.LoadInfo) error {
return err
}
}
if _, err := uc.emit(c, f); err != nil {
if err := uc.emit(c, f); err != nil {
return err
}
}
Expand Down Expand Up @@ -622,17 +628,12 @@ func findOutputPath(c *config.Config, f *rule.File) string {
}
outputDir := filepath.Join(baseDir, filepath.FromSlash(f.Pkg))
defaultOutputPath := filepath.Join(outputDir, c.DefaultBuildFileName())
files, err := os.ReadDir(outputDir)
files, err := ioutil.ReadDir(outputDir)
if err != nil {
// Ignore error. Directory probably doesn't exist.
return defaultOutputPath
}
infos := make([]fs.FileInfo, 0, len(files))
for _, f := range files {
info, _ := f.Info() // We ignore the error here because we know the file exists.
infos = append(infos, info)
}
outputPath := rule.MatchBuildFileName(outputDir, c.ValidBuildFileNames, infos)
outputPath := rule.MatchBuildFileName(outputDir, c.ValidBuildFileNames, files)
if outputPath == "" {
return defaultOutputPath
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/aspect/configure/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@ package configure
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/rule"
)

func fixFile(c *config.Config, f *rule.File) (bool, error) {
func fixFile(c *config.Config, f *rule.File) error {
newContent := f.Format()
if bytes.Equal(f.Content, newContent) {
return false, nil
return nil
}
outPath := findOutputPath(c, f)
if err := os.MkdirAll(filepath.Dir(outPath), 0o777); err != nil {
return false, err
return err
}
if err := os.WriteFile(outPath, newContent, 0o666); err != nil {
return false, err
if err := ioutil.WriteFile(outPath, newContent, 0o666); err != nil {
return err
}
f.Content = newContent
if getUpdateConfig(c).print0 {
fmt.Printf("%s\x00", outPath)
}
return true, nil
return nil
}
2 changes: 1 addition & 1 deletion pkg/aspect/configure/internal/module/module.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copy of gazelle internal https://github.com/bazelbuild/bazel-gazelle/blob/7feffe17f56e2b76eae8a4f2933215d6c5924176/internal/module/module.go
// Copy of gazelle internal https://github.com/bazelbuild/bazel-gazelle/blob/b62589672b5c32264ddf40585247d684c29bdd15/internal/module/module.go

// Package module provides functions to read information out of MODULE.bazel files.

Expand Down
2 changes: 1 addition & 1 deletion pkg/aspect/configure/internal/wspace/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "wspace",
srcs = ["wspace.go"],
srcs = ["finder.go"],
importpath = "aspect.build/cli/pkg/aspect/configure/internal/wspace",
visibility = ["//pkg/aspect/configure:__subpackages__"],
)
35 changes: 35 additions & 0 deletions pkg/aspect/configure/internal/wspace/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copy of gazelle internal https://github.com/bazelbuild/bazel-gazelle/blob/b62589672b5c32264ddf40585247d684c29bdd15/internal/wspace/finder.go

// Package wspace provides functions to locate and modify a bazel WORKSPACE file.
package wspace

import (
"os"
"path/filepath"
)

var workspaceFiles = []string{"WORKSPACE.bazel", "WORKSPACE"}

// IsWORKSPACE checks whether path is named WORKSPACE or WORKSPACE.bazel
func IsWORKSPACE(path string) bool {
base := filepath.Base(path)
for _, workspaceFile := range workspaceFiles {
if base == workspaceFile {
return true
}
}
return false
}

// FindWORKSPACEFile returns a path to a file in the provided root directory,
// either to an existing WORKSPACE or WORKSPACE.bazel file, or to root/WORKSPACE
// if neither exists. Note that this function does NOT recursively check parent directories.
func FindWORKSPACEFile(root string) string {
for _, workspaceFile := range workspaceFiles {
path := filepath.Join(root, workspaceFile)
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
return path
}
}
return filepath.Join(root, "WORKSPACE")
}
17 changes: 0 additions & 17 deletions pkg/aspect/configure/internal/wspace/wspace.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/aspect/configure/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"github.com/bazelbuild/bazel-gazelle/rule"
)

func printFile(c *config.Config, f *rule.File) (bool, error) {
func printFile(c *config.Config, f *rule.File) error {
fmt.Printf(">>> %s\n", f.Path)
content := f.Format()
_, err := os.Stdout.Write(content)
return false, err
return err
}

0 comments on commit 3eab48b

Please sign in to comment.