@@ -647,18 +647,28 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
647
647
648
648
// Determine directory from import path.
649
649
if ctxt .GOROOT != "" {
650
- dir := ctxt .joinPath (ctxt .GOROOT , "src" , path )
651
- if ctxt .Compiler != "gccgo" {
652
- isDir := ctxt .isDir (dir )
653
- binaryOnly = ! isDir && mode & AllowBinary != 0 && pkga != "" && ctxt .isFile (ctxt .joinPath (ctxt .GOROOT , pkga ))
654
- if isDir || binaryOnly {
655
- p .Dir = dir
656
- p .Goroot = true
657
- p .Root = ctxt .GOROOT
658
- goto Found
650
+ // If the package path starts with "vendor/", only search GOROOT before
651
+ // GOPATH if the importer is also within GOROOT. That way, if the user has
652
+ // vendored in a package that is subsequently included in the standard
653
+ // distribution, they'll continue to pick up their own vendored copy.
654
+ gorootFirst := srcDir == "" || ! strings .HasPrefix (path , "vendor/" )
655
+ if ! gorootFirst {
656
+ _ , gorootFirst = ctxt .hasSubdir (ctxt .GOROOT , srcDir )
657
+ }
658
+ if gorootFirst {
659
+ dir := ctxt .joinPath (ctxt .GOROOT , "src" , path )
660
+ if ctxt .Compiler != "gccgo" {
661
+ isDir := ctxt .isDir (dir )
662
+ binaryOnly = ! isDir && mode & AllowBinary != 0 && pkga != "" && ctxt .isFile (ctxt .joinPath (ctxt .GOROOT , pkga ))
663
+ if isDir || binaryOnly {
664
+ p .Dir = dir
665
+ p .Goroot = true
666
+ p .Root = ctxt .GOROOT
667
+ goto Found
668
+ }
659
669
}
670
+ tried .goroot = dir
660
671
}
661
- tried .goroot = dir
662
672
}
663
673
if ctxt .Compiler == "gccgo" && goroot .IsStandardPackage (ctxt .GOROOT , ctxt .Compiler , path ) {
664
674
p .Dir = ctxt .joinPath (ctxt .GOROOT , "src" , path )
@@ -678,6 +688,24 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
678
688
tried .gopath = append (tried .gopath , dir )
679
689
}
680
690
691
+ // If we tried GOPATH first due to a "vendor/" prefix, fall back to GOPATH.
692
+ // That way, the user can still get useful results from 'go list' for
693
+ // standard-vendored paths passed on the command line.
694
+ if ctxt .GOROOT != "" && tried .goroot == "" {
695
+ dir := ctxt .joinPath (ctxt .GOROOT , "src" , path )
696
+ if ctxt .Compiler != "gccgo" {
697
+ isDir := ctxt .isDir (dir )
698
+ binaryOnly = ! isDir && mode & AllowBinary != 0 && pkga != "" && ctxt .isFile (ctxt .joinPath (ctxt .GOROOT , pkga ))
699
+ if isDir || binaryOnly {
700
+ p .Dir = dir
701
+ p .Goroot = true
702
+ p .Root = ctxt .GOROOT
703
+ goto Found
704
+ }
705
+ }
706
+ tried .goroot = dir
707
+ }
708
+
681
709
// package was not found
682
710
var paths []string
683
711
format := "\t %s (vendor tree)"
0 commit comments