package workspace
import (
)
type Mode string
const (
ModeAuto Mode = "auto"
ModeWork Mode = "work"
ModeWalk Mode = "walk"
)
const DefaultWorkFile = "go.work"
type Loader interface {
Name() string
Load(ctx context.Context, v vcs.VCS, rev string) ([]*modgraph.Module, error)
}
func ( Mode, string) (Loader, error) {
switch {
case "", ModeAuto:
return &AutoLoader{WorkFile: }, nil
case ModeWork:
return &WorkLoader{WorkFile: }, nil
case ModeWalk:
return &WalkLoader{}, nil
default:
return nil, fmt.Errorf("unknown workspace mode %q (expected auto, work, or walk)", )
}
}
type AutoLoader struct {
WorkFile string
}
func ( *AutoLoader) () string { return string(ModeAuto) }
func ( *AutoLoader) ( context.Context, vcs.VCS, string) ([]*modgraph.Module, error) {
:= cmp.Or(.WorkFile, DefaultWorkFile)
if , := .ReadFile(, , ); == nil {
return loadWorkBytes(, , , , )
} else if .WorkFile != "" {
return nil, fmt.Errorf("configured work file %q not found at %s: %w", , , )
}
return (&WalkLoader{}).Load(, , )
}
type WorkLoader struct {
WorkFile string
}
func ( *WorkLoader) () string { return string(ModeWork) }
func ( *WorkLoader) ( context.Context, vcs.VCS, string) ([]*modgraph.Module, error) {
:= cmp.Or(.WorkFile, DefaultWorkFile)
, := .ReadFile(, , )
if != nil {
return nil, fmt.Errorf("read %s at %s: %w", , , )
}
return loadWorkBytes(, , , , )
}
func ( context.Context, vcs.VCS, , string, []byte) ([]*modgraph.Module, error) {
, := modfile.ParseWork(, , nil)
if != nil {
return nil, fmt.Errorf("parse %s: %w", , )
}
:= path.Dir(path.Clean(filepath.ToSlash()))
:= make([]string, 0, len(.Use))
for , := range .Use {
:= filepath.ToSlash(.Path)
if !filepath.IsLocal(filepath.FromSlash()) {
return nil, fmt.Errorf("non-local path %q in %s", .Path, )
}
= append(, path.Clean(path.Join(, )))
}
return readModules(, , , )
}
type WalkLoader struct{}
func ( *WalkLoader) () string { return string(ModeWalk) }
func ( *WalkLoader) ( context.Context, vcs.VCS, string) ([]*modgraph.Module, error) {
, := .ListFiles(, , "")
if != nil {
return nil, fmt.Errorf("list files at %s: %w", , )
}
var []string
for , := range {
if path.Base(.Path) != "go.mod" {
continue
}
:= path.Dir(.Path)
if shouldSkipWalkDir() {
continue
}
= append(, )
}
return readModules(, , , )
}
func ( context.Context, vcs.VCS, string, []string) ([]*modgraph.Module, error) {
if len() == 0 {
return nil, nil
}
:= make([]*modgraph.Module, len())
:= make([]error, len())
, := context.WithCancel()
defer ()
:= make(chan struct{}, min(max(runtime.GOMAXPROCS(0), 1), 8))
var sync.WaitGroup
for , := range {
.Go(func() {
select {
case <- struct{}{}:
defer func() { <- }()
case <-.Done():
return
}
, := readModule(, , , )
if != nil {
[] =
()
return
}
[] =
})
}
.Wait()
for , := range {
if != nil {
return nil,
}
}
slices.SortFunc(, func(, *modgraph.Module) int {
return strings.Compare(.Path, .Path)
})
return , nil
}
func ( string) bool {
if == "." {
return false
}
for := range strings.SplitSeq(, "/") {
if == "vendor" || == "testdata" || strings.HasPrefix(, ".") {
return true
}
}
return false
}
func ( context.Context, vcs.VCS, , string) (*modgraph.Module, error) {
= path.Clean()
:= "go.mod"
if != "." {
= path.Join(, "go.mod")
}
, := .ReadFile(, , )
if != nil {
return nil, fmt.Errorf("read %s at %s: %w", , , )
}
, := modfile.ParseLax(, , nil)
if != nil {
return nil, fmt.Errorf("parse %s: %w", , )
}
if .Module == nil || .Module.Mod.Path == "" {
return nil, fmt.Errorf("%s: missing module directive", )
}
:= make([]modgraph.Require, 0, len(.Require))
for , := range .Require {
= append(, modgraph.Require{
Path: .Mod.Path,
Version: .Mod.Version,
Indirect: .Indirect,
})
}
return &modgraph.Module{
Path: .Module.Mod.Path,
Dir: ,
Requires: ,
}, nil
}