func checkPkgFiles(files []*ast.File) { type bailout struct{} conf := types.Config{ FakeImportC: true, Error: func(err error) { if !*allErrors && errorCount >= 10 { panic(bailout{}) } report(err) }, Sizes: sizes, } if *gccgo { var inst gccgoimporter.GccgoInstallation inst.InitFromDriver("gccgo") conf.Import = inst.GetImporter(nil, nil) } defer func() { switch p := recover().(type) { case nil, bailout: // normal return or early exit default: // re-panic panic(p) } }() const path = "pkg" // any non-empty string will do for now conf.Check(path, fset, files, nil) }
func GetAllDependencies(pkg string, config *types.Config) ([]*types.Package, error) { var dependencies []*types.Package // ordered imported := make(map[string]bool) var importPkg func(string, []string) error importPkg = func(importPath string, importing []string) error { if importPath == "unsafe" || importPath == "go/doc" { return nil } if _, found := imported[importPath]; found { return nil } for _, path := range importing { if path == importPath { return fmt.Errorf("package import cycle: %s -> %s", strings.Join(importing, " -> "), importPath) } } typesPkg, err := config.Import(config.Packages, importPath) if err != nil { return err } var imps []string for _, imp := range typesPkg.Imports() { imps = append(imps, imp.Path()) } sort.Strings(imps) for _, imp := range imps { if err := importPkg(imp, append(importing, importPath)); err != nil { return err } } dependencies = append(dependencies, typesPkg) imported[importPath] = true return nil } importPkg("runtime", nil) // all packages depend on runtime err := importPkg(pkg, nil) return dependencies, err }