diff --git a/go/packages/packages.go b/go/packages/packages.go index 0fb3e337..2bdac145 100644 --- a/go/packages/packages.go +++ b/go/packages/packages.go @@ -30,11 +30,9 @@ import ( type LoadMode int const ( - _ LoadMode = iota - // LoadFiles finds the packages and computes their source file lists. // Package fields: ID, Name, Errors, GoFiles, OtherFiles. - LoadFiles + LoadFiles LoadMode = iota // LoadImports adds import information for each package // and its dependencies. @@ -144,6 +142,14 @@ type Config struct { } // Load and returns the Go packages named by the given patterns. +// +// Config specifies loading options; +// nil behaves the same as an empty Config. +// +// Load returns an error if any of the patterns was invalid +// as defined by the underlying build system. +// It may return an empty list of packages without an error, +// for instance for an empty expansion of a valid wildcard. func Load(cfg *Config, patterns ...string) ([]*Package, error) { l := newLoader(cfg) rawCfg := newRawConfig(&l.Config) @@ -247,8 +253,6 @@ func newLoader(cfg *Config) *loader { ld := &loader{} if cfg != nil { ld.Config = *cfg - } else { - ld.Config.Mode = LoadAllSyntax } if ld.Context == nil { ld.Context = context.Background() diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index a76bbc8a..afb45d33 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -45,6 +45,25 @@ var usesOldGolist = false // - import cycles are gracefully handled in type checker. // - test typechecking of generated test main and cgo. +// The zero-value of Config has LoadFiles mode. +func TestLoadZeroConfig(t *testing.T) { + initial, err := packages.Load(nil, "hash") + if err != nil { + t.Fatal(err) + } + if len(initial) != 1 { + t.Fatalf("got %s, want [hash]", initial) + } + hash := initial[0] + // Even though the hash package has imports, + // they are not reported. + got := fmt.Sprintf("name=%s srcs=%v imports=%v", hash.Name, srcs(hash), hash.Imports) + want := "name=hash srcs=[hash.go] imports=map[]" + if got != want { + t.Fatalf("got %s, want %s", got, want) + } +} + func TestLoadImportsGraph(t *testing.T) { tmp, cleanup := makeTree(t, map[string]string{ "src/a/a.go": `package a; const A = 1`,