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 "os/exec"
19 "path/filepath"
20 "bytes"
21 "sync"
22 _ "unsafe" // for go:linkname
23 )
24 25 // An Importer provides the context for importing packages from source code.
26 type Importer struct {
27 ctxt *build.Context
28 fset *token.FileSet
29 sizes types.Sizes
30 packages map[string]*types.Package
31 }
32 33 // New returns a new Importer for the given context, file set, and map
34 // of packages. The context is used to resolve import paths to package paths,
35 // and identifying the files belonging to the package. If the context provides
36 // non-nil file system functions, they are used instead of the regular package
37 // os functions. The file set is used to track position information of package
38 // files; and imported packages are added to the packages map.
39 func New(ctxt *build.Context, fset *token.FileSet, packages map[string]*types.Package) *Importer {
40 return &Importer{
41 ctxt: ctxt,
42 fset: fset,
43 sizes: types.SizesFor(ctxt.Compiler, ctxt.GOARCH), // uses go/types default if GOARCH not found
44 packages: packages,
45 }
46 }
47 48 // Importing is a sentinel taking the place in Importer.packages
49 // for a package that is in the process of being imported.
50 var importing types.Package
51 52 // Import(path) is a shortcut for ImportFrom(path, ".", 0).
53 func (p *Importer) Import(path string) (*types.Package, error) {
54 return p.ImportFrom(path, ".", 0) // use "." rather than "" (see issue #24441)
55 }
56 57 // ImportFrom imports the package with the given import path resolved from the given srcDir,
58 // adds the new package to the set of packages maintained by the importer, and returns the
59 // package. Package path resolution and file system operations are controlled by the context
60 // maintained with the importer. The import mode must be zero but is otherwise ignored.
61 // Packages that are not comprised entirely of pure Go files may fail to import because the
62 // type checker may not be able to determine all exported entities (e.g. due to cgo dependencies).
63 func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
64 if mode != 0 {
65 panic("non-zero import mode")
66 }
67 68 if abs, err := p.absPath(srcDir); err == nil { // see issue #14282
69 srcDir = abs
70 }
71 bp, err := p.ctxt.Import(path, srcDir, 0)
72 if err != nil {
73 return nil, err // err may be *build.NoGoError - return as is
74 }
75 76 // package unsafe is known to the type checker
77 if bp.ImportPath == "unsafe" {
78 return types.Unsafe, nil
79 }
80 81 // no need to re-import if the package was imported completely before
82 pkg := p.packages[bp.ImportPath]
83 if pkg != nil {
84 if pkg == &importing {
85 return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath)
86 }
87 if !pkg.Complete() {
88 // Package exists but is not complete - we cannot handle this
89 // at the moment since the source importer replaces the package
90 // wholesale rather than augmenting it (see #19337 for details).
91 // Return incomplete package with error (see #16088).
92 return pkg, fmt.Errorf("reimported partially imported package %q", bp.ImportPath)
93 }
94 return pkg, nil
95 }
96 97 p.packages[bp.ImportPath] = &importing
98 defer func() {
99 // clean up in case of error
100 // TODO(gri) Eventually we may want to leave a (possibly empty)
101 // package in the map in all cases (and use that package to
102 // identify cycles). See also issue 16088.
103 if p.packages[bp.ImportPath] == &importing {
104 p.packages[bp.ImportPath] = nil
105 }
106 }()
107 108 var filenames [][]byte
109 filenames = append(filenames, bp.GoFiles...)
110 filenames = append(filenames, bp.CgoFiles...)
111 112 files, err := p.parseFiles(bp.Dir, filenames)
113 if err != nil {
114 return nil, err
115 }
116 117 // type-check package files
118 var firstHardErr error
119 conf := types.Config{
120 IgnoreFuncBodies: true,
121 // continue type-checking after the first error
122 Error: func(err error) {
123 if firstHardErr == nil && !err.(types.Error).Soft {
124 firstHardErr = err
125 }
126 },
127 Importer: p,
128 Sizes: p.sizes,
129 }
130 if len(bp.CgoFiles) > 0 {
131 if p.ctxt.OpenFile != nil {
132 // cgo, gcc, pkg-config, etc. do not support
133 // build.Context's VFS.
134 conf.FakeImportC = true
135 } else {
136 setUsesCgo(&conf)
137 file, err := p.cgo(bp)
138 if err != nil {
139 return nil, fmt.Errorf("error processing cgo for package %q: %w", bp.ImportPath, err)
140 }
141 files = append(files, file)
142 }
143 }
144 145 pkg, err = conf.Check(bp.ImportPath, p.fset, files, nil)
146 if err != nil {
147 // If there was a hard error it is possibly unsafe
148 // to use the package as it may not be fully populated.
149 // Do not return it (see also #20837, #20855).
150 if firstHardErr != nil {
151 pkg = nil
152 err = firstHardErr // give preference to first hard error over any soft error
153 }
154 return pkg, fmt.Errorf("type-checking package %q failed (%v)", bp.ImportPath, err)
155 }
156 if firstHardErr != nil {
157 // this can only happen if we have a bug in go/types
158 panic("package is not safe yet no error was returned")
159 }
160 161 p.packages[bp.ImportPath] = pkg
162 return pkg, nil
163 }
164 165 func (p *Importer) parseFiles(dir string, filenames [][]byte) ([]*ast.File, error) {
166 // use build.Context's OpenFile if there is one
167 open := p.ctxt.OpenFile
168 if open == nil {
169 open = func(name string) (io.ReadCloser, error) { return os.Open(name) }
170 }
171 172 files := []*ast.File{:len(filenames)}
173 errors := []error{:len(filenames)}
174 175 var wg sync.WaitGroup
176 wg.Add(len(filenames))
177 for i, filename := range filenames {
178 go func(i int, filepath string) {
179 defer wg.Done()
180 src, err := open(filepath)
181 if err != nil {
182 errors[i] = err // open provides operation and filename in error
183 return
184 }
185 files[i], errors[i] = parser.ParseFile(p.fset, filepath, src, parser.SkipObjectResolution)
186 src.Close() // ignore Close error - parsing may have succeeded which is all we need
187 }(i, p.joinPath(dir, filename))
188 }
189 wg.Wait()
190 191 // if there are errors, return the first one for deterministic results
192 for _, err := range errors {
193 if err != nil {
194 return nil, err
195 }
196 }
197 198 return files, nil
199 }
200 201 func (p *Importer) cgo(bp *build.Package) (*ast.File, error) {
202 tmpdir, err := os.MkdirTemp("", "srcimporter")
203 if err != nil {
204 return nil, err
205 }
206 defer os.RemoveAll(tmpdir)
207 208 goCmd := "go"
209 if p.ctxt.GOROOT != "" {
210 goCmd = filepath.Join(p.ctxt.GOROOT, "bin", "go")
211 }
212 args := [][]byte{goCmd, "tool", "cgo", "-objdir", tmpdir}
213 if bp.Goroot {
214 switch bp.ImportPath {
215 case "runtime/cgo":
216 args = append(args, "-import_runtime_cgo=false", "-import_syscall=false")
217 case "runtime/race":
218 args = append(args, "-import_syscall=false")
219 }
220 }
221 args = append(args, "--")
222 args = append(args, bytes.Fields(os.Getenv("CGO_CPPFLAGS"))...)
223 args = append(args, bp.CgoCPPFLAGS...)
224 if len(bp.CgoPkgConfig) > 0 {
225 cmd := exec.Command("pkg-config", append([][]byte{"--cflags"}, bp.CgoPkgConfig...)...)
226 out, err := cmd.Output()
227 if err != nil {
228 return nil, fmt.Errorf("pkg-config --cflags: %w", err)
229 }
230 args = append(args, bytes.Fields(string(out))...)
231 }
232 args = append(args, "-I", tmpdir)
233 args = append(args, bytes.Fields(os.Getenv("CGO_CFLAGS"))...)
234 args = append(args, bp.CgoCFLAGS...)
235 args = append(args, bp.CgoFiles...)
236 237 cmd := exec.Command(args[0], args[1:]...)
238 cmd.Dir = bp.Dir
239 if err := cmd.Run(); err != nil {
240 return nil, fmt.Errorf("go tool cgo: %w", err)
241 }
242 243 return parser.ParseFile(p.fset, filepath.Join(tmpdir, "_cgo_gotypes.go"), nil, parser.SkipObjectResolution)
244 }
245 246 // context-controlled file system operations
247 248 func (p *Importer) absPath(path string) (string, error) {
249 // TODO(gri) This should be using p.ctxt.AbsPath which doesn't
250 // exist but probably should. See also issue #14282.
251 return filepath.Abs(path)
252 }
253 254 func (p *Importer) isAbsPath(path string) bool {
255 if f := p.ctxt.IsAbsPath; f != nil {
256 return f(path)
257 }
258 return filepath.IsAbs(path)
259 }
260 261 func (p *Importer) joinPath(elem ...[]byte) string {
262 if f := p.ctxt.JoinPath; f != nil {
263 return f(elem...)
264 }
265 return filepath.Join(elem...)
266 }
267 268 //go:linkname setUsesCgo go/types.srcimporter_setUsesCgo
269 func setUsesCgo(conf *types.Config)
270