Skip to content

Commit 849fd25

Browse files
dmitshurneelance
authored andcommitted
Treat GOPATH as a list of workspaces; use first workspace.
Fixes #60. The behavior should be improved (fixes #60) if user has multiple GOPATH workspaces. The behavior should be unchanged if user has a single GOPATH workspace. First cut at correct GOPATH handling as a list, rather than assuming it contains no more than 1 entry. It's unfinished and needs more work to correctly handle all scenarios with multiple GOPATH workspaces, but it's an improvement over the original version. Use filepath.Join instead of concatenating strings for path manipulation.
1 parent baa96e9 commit 849fd25

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

build/build.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func Import(path string, mode build.ImportMode, archSuffix string) (*build.Packa
5959
}
6060
if _, err := os.Stat(pkg.PkgObj); os.IsNotExist(err) && strings.HasPrefix(pkg.PkgObj, build.Default.GOROOT) {
6161
// fall back to GOPATH
62-
gopathPkgObj := build.Default.GOPATH + pkg.PkgObj[len(build.Default.GOROOT):]
62+
firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: Need to check inside all GOPATH workspaces.
63+
gopathPkgObj := filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(build.Default.GOROOT):])
6364
if _, err := os.Stat(gopathPkgObj); err == nil {
6465
pkg.PkgObj = gopathPkgObj
6566
}
@@ -401,8 +402,9 @@ func (s *Session) BuildPackage(pkg *PackageData) error {
401402

402403
if err := s.writeLibraryPackage(pkg, pkg.PkgObj); err != nil {
403404
if strings.HasPrefix(pkg.PkgObj, s.options.GOROOT) {
404-
// fall back to GOPATH
405-
if err := s.writeLibraryPackage(pkg, s.options.GOPATH+pkg.PkgObj[len(s.options.GOROOT):]); err != nil {
405+
// fall back to first GOPATH workspace
406+
firstGopathWorkspace := filepath.SplitList(s.options.GOPATH)[0]
407+
if err := s.writeLibraryPackage(pkg, filepath.Join(firstGopathWorkspace, pkg.PkgObj[len(s.options.GOROOT):])); err != nil {
406408
return err
407409
}
408410
return nil
@@ -461,9 +463,9 @@ func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error {
461463
}
462464
pos := fileSet.Position(originalPos)
463465
file := pos.Filename
464-
switch {
465-
case strings.HasPrefix(file, s.options.GOPATH):
466-
file = filepath.ToSlash(filepath.Join("/gopath", file[len(s.options.GOPATH):]))
466+
switch hasGopathPrefix, prefixLen := hasGopathPrefix(file, s.options.GOPATH); {
467+
case hasGopathPrefix:
468+
file = filepath.ToSlash(filepath.Join("/gopath", file[prefixLen:]))
467469
case strings.HasPrefix(file, s.options.GOROOT):
468470
file = filepath.ToSlash(filepath.Join("/goroot", file[len(s.options.GOROOT):]))
469471
default:
@@ -480,6 +482,18 @@ func (s *Session) WriteCommandPackage(pkg *PackageData, pkgObj string) error {
480482
return compiler.WriteProgramCode(deps, s.ImportContext, sourceMapFilter)
481483
}
482484

485+
// hasGopathPrefix returns true and the length of the matched GOPATH workspace,
486+
// iff file has a prefix that matches one of the GOPATH workspaces.
487+
func hasGopathPrefix(file, gopath string) (hasGopathPrefix bool, prefixLen int) {
488+
gopathWorkspaces := filepath.SplitList(gopath)
489+
for _, gopathWorkspace := range gopathWorkspaces {
490+
if strings.HasPrefix(file, gopathWorkspace) {
491+
return true, len(gopathWorkspace)
492+
}
493+
}
494+
return false, 0
495+
}
496+
483497
func (s *Session) WaitForChange() {
484498
fmt.Println("\x1B[32mwatching for changes...\x1B[39m")
485499
select {

tool.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ func main() {
136136
exitCode := handleError(func() error {
137137
pkgs := installFlags.Args()
138138
if len(pkgs) == 0 {
139-
srcDir, err := filepath.EvalSymlinks(filepath.Join(build.Default.GOPATH, "src"))
139+
firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: The GOPATH workspace that contains the package source should be chosen.
140+
srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src"))
140141
if err != nil {
141142
return err
142143
}
@@ -220,7 +221,8 @@ func main() {
220221
}
221222
}
222223
if len(pkgs) == 0 {
223-
srcDir, err := filepath.EvalSymlinks(filepath.Join(build.Default.GOPATH, "src"))
224+
firstGopathWorkspace := filepath.SplitList(build.Default.GOPATH)[0] // TODO: Not sure if always picking first GOPATH workspace here is the right thing.
225+
srcDir, err := filepath.EvalSymlinks(filepath.Join(firstGopathWorkspace, "src"))
224226
if err != nil {
225227
return err
226228
}

0 commit comments

Comments
 (0)