Skip to content

Commit

Permalink
Encapsulate the build.Package Import() into a func
Browse files Browse the repository at this point in the history
It is used a couple times, part of further cleanup.
  • Loading branch information
thockin committed Jan 3, 2017
1 parent 5df4121 commit 7ca94dd
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,42 +112,32 @@ func (b *Builder) AddBuildTags(tags ...string) {
// e.g. test files and files for other platforms-- there is quite a bit of
// logic of that nature in the build package.
func (b *Builder) importBuildPackage(pkgPath string) (*build.Package, error) {
// This is a bit of a hack. The srcDir argument to Import() should
// properly be the dir of the file which depends on the package to be
// imported, so that vendoring can work properly. We assume that there is
// only one level of vendoring, and that the CWD is inside the GOPATH, so
// this should be safe.
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("unable to get current directory: %v", err)
}

// First, find it, so we know what path to use.
buildPackage, err := b.context.Import(pkgPath, cwd, build.FindOnly)
buildPkg, err := b.importWithMode(pkgPath, build.FindOnly)
if err != nil {
return nil, fmt.Errorf("unable to *find* %q: %v", pkgPath, err)
}

pkgPath = buildPackage.ImportPath
pkgPath = buildPkg.ImportPath

if buildPackage, ok := b.buildPackages[pkgPath]; ok {
return buildPackage, nil
if buildPkg, ok := b.buildPackages[pkgPath]; ok {
return buildPkg, nil
}
buildPackage, err = b.context.Import(pkgPath, cwd, build.ImportComment)
buildPkg, err = b.importWithMode(pkgPath, build.ImportComment)
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
return nil, fmt.Errorf("unable to import %q: %v", pkgPath, err)
}
}
b.buildPackages[pkgPath] = buildPackage
b.buildPackages[pkgPath] = buildPkg

if b.importGraph[pkgPath] == nil {
b.importGraph[pkgPath] = map[string]struct{}{}
}
for _, p := range buildPackage.Imports {
for _, p := range buildPkg.Imports {
b.importGraph[pkgPath][p] = struct{}{}
}
return buildPackage, nil
return buildPkg, nil
}

// AddFileForTest adds a file to the set. The pkg must be of the form
Expand Down Expand Up @@ -204,18 +194,8 @@ func (b *Builder) AddDir(dir string) error {
// subdirectories; it returns an error only if the path couldn't be resolved;
// any directories recursed into without go source are ignored.
func (b *Builder) AddDirRecursive(dir string) error {
// This is a bit of a hack. The srcDir argument to Import() should
// properly be the dir of the file which depends on the package to be
// imported, so that vendoring can work properly. We assume that there is
// only one level of vendoring, and that the CWD is inside the GOPATH, so
// this should be safe.
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get current directory: %v", err)
}

// First, find it, so we know what path to use.
pkg, err := b.context.Import(dir, cwd, build.FindOnly)
pkg, err := b.importWithMode(dir, build.FindOnly)
if err != nil {
return fmt.Errorf("unable to *find* %q: %v", dir, err)
}
Expand Down Expand Up @@ -488,6 +468,25 @@ func (b *Builder) findTypesIn(pkgPath string, u *types.Universe) error {
return nil
}

func (b *Builder) importWithMode(dir string, mode build.ImportMode) (*build.Package, error) {
// This is a bit of a hack. The srcDir argument to Import() should
// properly be the dir of the file which depends on the package to be
// imported, so that vendoring can work properly and local paths can
// resolve. We assume that there is only one level of vendoring, and that
// the CWD is inside the GOPATH, so this should be safe. Nobody should be
// using local (relative) paths except on the CLI, so CWD is also
// sufficient.
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("unable to get current directory: %v", err)
}
buildPkg, err := b.context.Import(dir, cwd, mode)
if err != nil {
return nil, err
}
return buildPkg, nil
}

// if there's a comment on the line `lines` before pos, return its text, otherwise "".
func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup {
position := b.fset.Position(pos)
Expand Down

0 comments on commit 7ca94dd

Please sign in to comment.