Skip to content

Treat GOPATH as a list of workspaces; use first workspace. #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down