github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/go/internal/srcimporter/srcimporter.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package srcimporter implements importing directly 6 // from source files rather than installed packages. 7 package srcimporter // import "go/internal/srcimporter" 8 9 import ( 10 "fmt" 11 "go/ast" 12 "go/build" 13 "go/parser" 14 "go/token" 15 "go/types" 16 "io" 17 "os" 18 "path/filepath" 19 "sync" 20 ) 21 22 // An Importer provides the context for importing packages from source code. 23 type Importer struct { 24 ctxt *build.Context 25 fset *token.FileSet 26 sizes types.Sizes 27 packages map[string]*types.Package 28 } 29 30 // NewImporter returns a new Importer for the given context, file set, and map 31 // of packages. The context is used to resolve import paths to package paths, 32 // and identifying the files belonging to the package. If the context provides 33 // non-nil file system functions, they are used instead of the regular package 34 // os functions. The file set is used to track position information of package 35 // files; and imported packages are added to the packages map. 36 func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Package) *Importer { 37 return &Importer{ 38 ctxt: ctxt, 39 fset: fset, 40 sizes: types.SizesFor(ctxt.Compiler, ctxt.GOARCH), // uses go/types default if GOARCH not found 41 packages: packages, 42 } 43 } 44 45 // Importing is a sentinel taking the place in Importer.packages 46 // for a package that is in the process of being imported. 47 var importing types.Package 48 49 // Import(path) is a shortcut for ImportFrom(path, "", 0). 50 func (p *Importer) Import(path string) (*types.Package, error) { 51 return p.ImportFrom(path, "", 0) 52 } 53 54 // ImportFrom imports the package with the given import path resolved from the given srcDir, 55 // adds the new package to the set of packages maintained by the importer, and returns the 56 // package. Package path resolution and file system operations are controlled by the context 57 // maintained with the importer. The import mode must be zero but is otherwise ignored. 58 // Packages that are not comprised entirely of pure Go files may fail to import because the 59 // type checker may not be able to determine all exported entities (e.g. due to cgo dependencies). 60 func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) { 61 if mode != 0 { 62 panic("non-zero import mode") 63 } 64 65 // determine package path (do vendor resolution) 66 var bp *build.Package 67 var err error 68 switch { 69 default: 70 if abs, err := p.absPath(srcDir); err == nil { // see issue #14282 71 srcDir = abs 72 } 73 bp, err = p.ctxt.Import(path, srcDir, build.FindOnly) 74 75 case build.IsLocalImport(path): 76 // "./x" -> "srcDir/x" 77 bp, err = p.ctxt.ImportDir(filepath.Join(srcDir, path), build.FindOnly) 78 79 case p.isAbsPath(path): 80 return nil, fmt.Errorf("invalid absolute import path %q", path) 81 } 82 if err != nil { 83 return nil, err // err may be *build.NoGoError - return as is 84 } 85 86 // package unsafe is known to the type checker 87 if bp.ImportPath == "unsafe" { 88 return types.Unsafe, nil 89 } 90 91 // no need to re-import if the package was imported completely before 92 pkg := p.packages[bp.ImportPath] 93 if pkg != nil { 94 if pkg == &importing { 95 return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath) 96 } 97 if !pkg.Complete() { 98 // Package exists but is not complete - we cannot handle this 99 // at the moment since the source importer replaces the package 100 // wholesale rather than augmenting it (see #19337 for details). 101 // Return incomplete package with error (see #16088). 102 return pkg, fmt.Errorf("reimported partially imported package %q", bp.ImportPath) 103 } 104 return pkg, nil 105 } 106 107 p.packages[bp.ImportPath] = &importing 108 defer func() { 109 // clean up in case of error 110 // TODO(gri) Eventually we may want to leave a (possibly empty) 111 // package in the map in all cases (and use that package to 112 // identify cycles). See also issue 16088. 113 if p.packages[bp.ImportPath] == &importing { 114 p.packages[bp.ImportPath] = nil 115 } 116 }() 117 118 // collect package files 119 bp, err = p.ctxt.ImportDir(bp.Dir, 0) 120 if err != nil { 121 return nil, err // err may be *build.NoGoError - return as is 122 } 123 var filenames []string 124 filenames = append(filenames, bp.GoFiles...) 125 filenames = append(filenames, bp.CgoFiles...) 126 127 files, err := p.parseFiles(bp.Dir, filenames) 128 if err != nil { 129 return nil, err 130 } 131 132 // type-check package files 133 var firstHardErr error 134 conf := types.Config{ 135 IgnoreFuncBodies: true, 136 FakeImportC: true, 137 // continue type-checking after the first error 138 Error: func(err error) { 139 if firstHardErr == nil && !err.(types.Error).Soft { 140 firstHardErr = err 141 } 142 }, 143 Importer: p, 144 Sizes: p.sizes, 145 } 146 pkg, err = conf.Check(bp.ImportPath, p.fset, files, nil) 147 if err != nil { 148 // If there was a hard error it is possibly unsafe 149 // to use the package as it may not be fully populated. 150 // Do not return it (see also #20837, #20855). 151 if firstHardErr != nil { 152 pkg = nil 153 err = firstHardErr // give preference to first hard error over any soft error 154 } 155 return pkg, fmt.Errorf("type-checking package %q failed (%v)", bp.ImportPath, err) 156 } 157 if firstHardErr != nil { 158 // this can only happen if we have a bug in go/types 159 panic("package is not safe yet no error was returned") 160 } 161 162 p.packages[bp.ImportPath] = pkg 163 return pkg, nil 164 } 165 166 func (p *Importer) parseFiles(dir string, filenames []string) ([]*ast.File, error) { 167 // use build.Context's OpenFile if there is one 168 open := p.ctxt.OpenFile 169 if open == nil { 170 open = func(name string) (io.ReadCloser, error) { return os.Open(name) } 171 } 172 173 files := make([]*ast.File, len(filenames)) 174 errors := make([]error, len(filenames)) 175 176 var wg sync.WaitGroup 177 wg.Add(len(filenames)) 178 for i, filename := range filenames { 179 go func(i int, filepath string) { 180 defer wg.Done() 181 src, err := open(filepath) 182 if err != nil { 183 errors[i] = err // open provides operation and filename in error 184 return 185 } 186 files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, 0) 187 src.Close() // ignore Close error - parsing may have succeeded which is all we need 188 }(i, p.joinPath(dir, filename)) 189 } 190 wg.Wait() 191 192 // if there are errors, return the first one for deterministic results 193 for _, err := range errors { 194 if err != nil { 195 return nil, err 196 } 197 } 198 199 return files, nil 200 } 201 202 // context-controlled file system operations 203 204 func (p *Importer) absPath(path string) (string, error) { 205 // TODO(gri) This should be using p.ctxt.AbsPath which doesn't 206 // exist but probably should. See also issue #14282. 207 return filepath.Abs(path) 208 } 209 210 func (p *Importer) isAbsPath(path string) bool { 211 if f := p.ctxt.IsAbsPath; f != nil { 212 return f(path) 213 } 214 return filepath.IsAbs(path) 215 } 216 217 func (p *Importer) joinPath(elem ...string) string { 218 if f := p.ctxt.JoinPath; f != nil { 219 return f(elem...) 220 } 221 return filepath.Join(elem...) 222 }