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