Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr committed Jul 12, 2019
1 parent 63eb283 commit 49ff93a
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,28 @@ func NewGo(options ...Option) (Interface, error) {
// Only valid importpaths that provide commands (i.e., are "package main") are
// supported.
func (g *gobuild) IsSupportedReference(s string) bool {
p, err := g.Import(s)
p, err := g.importPackage(s)
if err != nil {
return false
}
return p.IsCommand()
}

// Import wraps go/build.Import to handle go modules.
func (g *gobuild) Import(s string) (*gb.Package, error) {
if g.mod != nil && (strings.HasPrefix(s, g.mod.Path) || gb.IsLocalImport(s)) {
pkg, err := gb.Import(s, g.mod.Dir, gb.ImportComment)
if err == nil {
return pkg, err
// importPackage wraps go/build.Import to handle go modules.
//
// Note that we will fall back to using GOPATH if the project isn't using go
// modules or the import path doesn't match the module path of the project.
func (g *gobuild) importPackage(s string) (*gb.Package, error) {
if g.mod != nil {
// If we're inside a go modules project, try to use the module's directory
// as our source root to import:
// * paths that match module path prefix (they should be in this project)
// * relative paths (they should also be in this project)
if strings.HasPrefix(s, g.mod.Path) || gb.IsLocalImport(s) {
pkg, err := gb.Import(s, g.mod.Dir, gb.ImportComment)

This comment has been minimized.

Copy link
@imjasonh

imjasonh Jul 12, 2019

Member

Honest question because I don't know what users would expect: Do we want to fallthrough to checking the importpath from the gopath here?

If a project is using modules and their YAML includes an import path outside their module (not in their go.mod), would they expect it to be resolved? Or would they expect ko to pull the package out of their gopath?

if err == nil {
return pkg, err
}
}
}
return gb.Import(s, gb.Default.GOPATH, gb.ImportComment)
Expand Down Expand Up @@ -256,7 +265,7 @@ func tarBinary(name, binary string) (*bytes.Buffer, error) {
}

func (g *gobuild) kodataPath(s string) (string, error) {
p, err := g.Import(s)
p, err := g.importPackage(s)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 49ff93a

Please sign in to comment.