diff --git a/build/build.go b/build/build.go index 399f3b5e0..84e486a7f 100644 --- a/build/build.go +++ b/build/build.go @@ -55,7 +55,8 @@ func Import(path string, mode build.ImportMode, archSuffix string) (*build.Packa } if _, err := os.Stat(pkg.PkgObj); os.IsNotExist(err) && strings.HasPrefix(pkg.PkgObj, build.Default.GOROOT) { // fall back to GOPATH - gopathPkgObj := build.Default.GOPATH + pkg.PkgObj[len(build.Default.GOROOT):] + firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: Need to check inside all GOPATH workspaces. + gopathPkgObj := filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(build.Default.GOROOT):]) if _, err := os.Stat(gopathPkgObj); err == nil { pkg.PkgObj = gopathPkgObj } @@ -361,8 +362,9 @@ func (s *Session) BuildPackage(pkg *PackageData) error { if err := s.writeLibraryPackage(pkg, pkg.PkgObj); err != nil { if strings.HasPrefix(pkg.PkgObj, s.options.GOROOT) { - // fall back to GOPATH - if err := s.writeLibraryPackage(pkg, s.options.GOPATH+pkg.PkgObj[len(s.options.GOROOT):]); err != nil { + // fall back to first GOPATH workspace + firstGopathWorkspace := filepath.SplitList(s.options.GOPATH)[0] + if err := s.writeLibraryPackage(pkg, filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(s.options.GOROOT):])); err != nil { return err } return nil @@ -421,9 +423,9 @@ func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error { } pos := fileSet.Position(originalPos) file := pos.Filename - switch { - case strings.HasPrefix(file, s.options.GOPATH): - file = filepath.ToSlash(filepath.Join("/gopath", file[len(s.options.GOPATH):])) + switch hasGopathPrefix, prefixLen := hasGopathPrefix(file, s.options.GOPATH); { + case hasGopathPrefix: + file = filepath.ToSlash(filepath.Join("/gopath", file[prefixLen:])) case strings.HasPrefix(file, s.options.GOROOT): file = filepath.ToSlash(filepath.Join("/goroot", file[len(s.options.GOROOT):])) default: @@ -440,6 +442,18 @@ func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error { return compiler.WriteProgramCode(deps, s.ImportContext, sourceMapFilter) } +// hasGopathPrefix returns true and the length of the matched GOPATH workspace, +// iff file has a prefix that matches one of the GOPATH workspaces. +func hasGopathPrefix(file, gopath string) (hasGopathPrefix bool, prefixLen int) { + gopathWorkspaces := filepath.SplitList(gopath) + for _, gopathWorkspace := range gopathWorkspaces { + if strings.HasPrefix(file, gopathWorkspace) { + return true, len(gopathWorkspace) + } + } + return false, 0 +} + func (s *Session) WaitForChange() { fmt.Println("\x1B[32mwatching for changes...\x1B[39m") select { diff --git a/tool.go b/tool.go index cd58ec856..e166564f3 100644 --- a/tool.go +++ b/tool.go @@ -136,7 +136,8 @@ func main() { exitCode := handleError(func() error { pkgs := installFlags.Args() if len(pkgs) == 0 { - srcDir, err := filepath.EvalSymlinks(filepath.Join(build.Default.GOPATH, "src")) + firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: The GOPATH workspace that contains the package source should be chosen. + srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src")) if err != nil { return err } @@ -220,7 +221,8 @@ func main() { } } if len(pkgs) == 0 { - srcDir, err := filepath.EvalSymlinks(filepath.Join(build.Default.GOPATH, "src")) + firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: Not sure if always picking first GOPATH workspace here is the right thing. + srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src")) if err != nil { return err }