// Package workspace loads Go modules from a VCS repository revision via // go.work files or VCS tree walks.
package workspace import ( ) // Mode selects how Go modules are loaded from a repository. type Mode string const ( // ModeAuto uses ModeWork when a go.work file exists and falls back to ModeWalk. ModeAuto Mode = "auto" // ModeWork loads modules listed in a go.work file. ModeWork Mode = "work" // ModeWalk loads all tracked go.mod files in the VCS tree. ModeWalk Mode = "walk" ) // DefaultWorkFile is the default Go workspace filename. const DefaultWorkFile = "go.work" // Loader loads Go modules at a given VCS revision. type Loader interface { Name() string Load(ctx context.Context, v vcs.VCS, rev string) ([]*modgraph.Module, error) } // New returns a Loader for mode (ModeAuto, ModeWork, or ModeWalk). // If workFile is non-empty, ModeWork and ModeAuto read that repo-relative work // file instead of DefaultWorkFile. 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)", ) } } // AutoLoader uses WorkLoader when a work file exists at the target revision and // falls back to WalkLoader otherwise. type AutoLoader struct { WorkFile string } // Name returns "auto". func ( *AutoLoader) () string { return string(ModeAuto) } // Load delegates to WorkLoader if a go.work file is present, else WalkLoader. 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(, , ) } // WorkLoader loads modules listed in a go.work file. type WorkLoader struct { WorkFile string } // Name returns "work". func ( *WorkLoader) () string { return string(ModeWork) } // Load parses WorkFile (default "go.work") at rev and loads each used module. 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(, , , ) } // WalkLoader loads all tracked go.mod files in the VCS tree at rev. type WalkLoader struct{} // Name returns "walk". func ( *WalkLoader) () string { return string(ModeWalk) } // Load lists tracked files at rev and parses every non-ignored go.mod file. 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 }