Skip to content

Commit

Permalink
update-repos use pruned graph
Browse files Browse the repository at this point in the history
  • Loading branch information
sluongng committed Jul 3, 2024
1 parent 852fdcf commit a5ca20f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
76 changes: 53 additions & 23 deletions language/go/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,35 @@ package golang

import (
"bytes"
"fmt"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"

"github.com/bazelbuild/bazel-gazelle/language"
)

func copyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()

destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()

_, err = io.Copy(destination, source)
if err != nil {
return err
}

return nil
}

func importReposFromModules(args language.ImportReposArgs) language.ImportReposResult {
// run go list in the dir where go.mod is located
data, err := goListModules(filepath.Dir(args.Path))
Expand All @@ -37,29 +58,38 @@ func importReposFromModules(args language.ImportReposArgs) language.ImportReposR
return language.ImportReposResult{Error: err}
}

// Load sums from go.sum. Ideally, they're all there.
goSumPath := filepath.Join(filepath.Dir(args.Path), "go.sum")
data, _ = os.ReadFile(goSumPath)
lines := bytes.Split(data, []byte("\n"))
for _, line := range lines {
line = bytes.TrimSpace(line)
fields := bytes.Fields(line)
if len(fields) != 3 {
continue
}
path, version, sum := string(fields[0]), string(fields[1]), string(fields[2])
if strings.HasSuffix(version, "/go.mod") {
continue
}
if mod, ok := pathToModule[path+"@"+version]; ok {
mod.Sum = sum
}
// filtered 'go list' result with 'go mod download'
filteredMapToModule := make(map[string]*moduleFromList)
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return language.ImportReposResult{Error: err}
}

pathToModule, err = fillMissingSums(pathToModule)
defer os.RemoveAll(tmpDir)
wsPath := filepath.Dir(args.Path)
if err := copyFile(filepath.Join(wsPath, "go.mod"), filepath.Join(tmpDir, "go.mod")); err != nil {
return language.ImportReposResult{Error: err}
}
if err := copyFile(filepath.Join(wsPath, "go.sum"), filepath.Join(tmpDir, "go.sum")); err != nil {
// ignore if go.sum is not found
}
downloadData, err := goModDownload(tmpDir, []string{})
if err != nil {
return language.ImportReposResult{Error: fmt.Errorf("finding module sums: %v", err)}
return language.ImportReposResult{Error: err}
}
dec := json.NewDecoder(bytes.NewReader(downloadData))
for dec.More() {
var dl moduleFromDownload
if err := dec.Decode(&dl); err != nil {
return language.ImportReposResult{Error: err}
}
path := dl.Path + "@" + dl.Version
if mod, ok := pathToModule[path]; ok {
if mod.Sum == "" {
mod.Sum = dl.Sum
}
filteredMapToModule[path] = mod
}
}

return language.ImportReposResult{Gen: toRepositoryRules(pathToModule)}
return language.ImportReposResult{Gen: toRepositoryRules(filteredMapToModule)}
}
3 changes: 3 additions & 0 deletions language/go/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func (*goLang) CanImport(path string) bool {

func (*goLang) ImportRepos(args language.ImportReposArgs) language.ImportReposResult {
res := repoImportFuncs[filepath.Base(args.Path)](args)
if res.Error != nil {
return res
}
for _, r := range res.Gen {
setBuildAttrs(getGoConfig(args.Config), r)
}
Expand Down
2 changes: 1 addition & 1 deletion repo/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ func (rc *RemoteCache) initTmp() {
if rc.tmpErr != nil {
return
}
rc.tmpErr = os.WriteFile(filepath.Join(rc.tmpDir, "go.mod"), []byte("module gazelle_remote_cache\ngo 1.15\n"), 0o666)
rc.tmpErr = os.WriteFile(filepath.Join(rc.tmpDir, "go.mod"), []byte("module gazelle_remote_cache\ngo 1.22\n"), 0o666)
})
}

Expand Down

0 comments on commit a5ca20f

Please sign in to comment.