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.
5 // Package srcimporter implements importing directly
6 // from source files rather than installed packages.
7 package srcimporter
// import "go/internal/srcimporter"
22 // An Importer provides the context for importing packages from source code.
23 type Importer
struct {
27 packages
map[string]*types
.Package
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
{
40 sizes
: types
.SizesFor(ctxt
.Compiler
, ctxt
.GOARCH
), // uses go/types default if GOARCH not found
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
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) // use "." rather than "" (see issue #24441)
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
) {
62 panic("non-zero import mode")
65 if abs
, err
:= p
.absPath(srcDir
); err
== nil { // see issue #14282
68 bp
, err
:= p
.ctxt
.Import(path
, srcDir
, 0)
70 return nil, err
// err may be *build.NoGoError - return as is
73 // package unsafe is known to the type checker
74 if bp
.ImportPath
== "unsafe" {
75 return types
.Unsafe
, nil
78 // no need to re-import if the package was imported completely before
79 pkg
:= p
.packages
[bp
.ImportPath
]
81 if pkg
== &importing
{
82 return nil, fmt
.Errorf("import cycle through package %q", bp
.ImportPath
)
85 // Package exists but is not complete - we cannot handle this
86 // at the moment since the source importer replaces the package
87 // wholesale rather than augmenting it (see #19337 for details).
88 // Return incomplete package with error (see #16088).
89 return pkg
, fmt
.Errorf("reimported partially imported package %q", bp
.ImportPath
)
94 p
.packages
[bp
.ImportPath
] = &importing
96 // clean up in case of error
97 // TODO(gri) Eventually we may want to leave a (possibly empty)
98 // package in the map in all cases (and use that package to
99 // identify cycles). See also issue 16088.
100 if p
.packages
[bp
.ImportPath
] == &importing
{
101 p
.packages
[bp
.ImportPath
] = nil
105 var filenames
[]string
106 filenames
= append(filenames
, bp
.GoFiles
...)
107 filenames
= append(filenames
, bp
.CgoFiles
...)
109 files
, err
:= p
.parseFiles(bp
.Dir
, filenames
)
114 // type-check package files
115 var firstHardErr error
116 conf
:= types
.Config
{
117 IgnoreFuncBodies
: true,
119 // continue type-checking after the first error
120 Error
: func(err error
) {
121 if firstHardErr
== nil && !err
.(types
.Error
).Soft
{
128 pkg
, err
= conf
.Check(bp
.ImportPath
, p
.fset
, files
, nil)
130 // If there was a hard error it is possibly unsafe
131 // to use the package as it may not be fully populated.
132 // Do not return it (see also #20837, #20855).
133 if firstHardErr
!= nil {
135 err
= firstHardErr
// give preference to first hard error over any soft error
137 return pkg
, fmt
.Errorf("type-checking package %q failed (%v)", bp
.ImportPath
, err
)
139 if firstHardErr
!= nil {
140 // this can only happen if we have a bug in go/types
141 panic("package is not safe yet no error was returned")
144 p
.packages
[bp
.ImportPath
] = pkg
148 func (p
*Importer
) parseFiles(dir
string, filenames
[]string) ([]*ast
.File
, error
) {
149 // use build.Context's OpenFile if there is one
150 open
:= p
.ctxt
.OpenFile
152 open
= func(name
string) (io
.ReadCloser
, error
) { return os
.Open(name
) }
155 files
:= make([]*ast
.File
, len(filenames
))
156 errors
:= make([]error
, len(filenames
))
158 var wg sync
.WaitGroup
159 wg
.Add(len(filenames
))
160 for i
, filename
:= range filenames
{
161 go func(i
int, filepath
string) {
163 src
, err
:= open(filepath
)
165 errors
[i
] = err
// open provides operation and filename in error
168 files
[i
], errors
[i
] = parser
.ParseFile(p
.fset
, filepath
, src
, 0)
169 src
.Close() // ignore Close error - parsing may have succeeded which is all we need
170 }(i
, p
.joinPath(dir
, filename
))
174 // if there are errors, return the first one for deterministic results
175 for _
, err
:= range errors
{
184 // context-controlled file system operations
186 func (p
*Importer
) absPath(path
string) (string, error
) {
187 // TODO(gri) This should be using p.ctxt.AbsPath which doesn't
188 // exist but probably should. See also issue #14282.
189 return filepath
.Abs(path
)
192 func (p
*Importer
) isAbsPath(path
string) bool {
193 if f
:= p
.ctxt
.IsAbsPath
; f
!= nil {
196 return filepath
.IsAbs(path
)
199 func (p
*Importer
) joinPath(elem
...string) string {
200 if f
:= p
.ctxt
.JoinPath
; f
!= nil {
203 return filepath
.Join(elem
...)