1
0
mirror of https://github.com/golang/go synced 2024-11-21 23:34:42 -07:00

cmd/go: handle path to cmd directory

Now it works to run 'go install' (no args) in cmd/go.

Fixes #2679.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5543046
This commit is contained in:
Russ Cox 2012-01-12 15:27:57 -08:00
parent e91b31bc79
commit 4505ae3863
2 changed files with 33 additions and 2 deletions

View File

@ -192,7 +192,7 @@ func importPaths(args []string) []string {
} }
var out []string var out []string
for _, a := range args { for _, a := range args {
if (strings.HasPrefix(a, "./") || strings.HasPrefix(a, "../")) && strings.Contains(a, "...") { if isLocalPath(a) && strings.Contains(a, "...") {
out = append(out, allPackagesInFS(a)...) out = append(out, allPackagesInFS(a)...)
continue continue
} }
@ -246,6 +246,17 @@ func run(cmdargs ...interface{}) {
} }
} }
func runOut(cmdargs ...interface{}) []byte {
cmdline := stringList(cmdargs...)
out, err := exec.Command(cmdline[0], cmdline[1:]...).CombinedOutput()
if err != nil {
os.Stderr.Write(out)
errorf("%v", err)
out = nil
}
return out
}
// matchPattern(pattern)(name) reports whether // matchPattern(pattern)(name) reports whether
// name matches pattern. Pattern is a limited glob // name matches pattern. Pattern is a limited glob
// pattern in which '...' means 'any string' and there // pattern in which '...' means 'any string' and there
@ -422,3 +433,10 @@ func stringList(args ...interface{}) []string {
} }
return x return x
} }
// isLocalPath returns true if arg is an import path denoting
// a local file system directory. That is, it returns true if the
// path begins with ./ or ../ .
func isLocalPath(arg string) bool {
return arg == "." || arg == ".." || strings.HasPrefix(arg, "./") || strings.HasPrefix(arg, "../")
}

View File

@ -79,7 +79,7 @@ func loadPackage(arg string) (*Package, error) {
t, importPath, err := build.FindTree(arg) t, importPath, err := build.FindTree(arg)
dir := "" dir := ""
// Maybe it is a standard command. // Maybe it is a standard command.
if err != nil && !filepath.IsAbs(arg) && strings.HasPrefix(arg, "cmd/") { if err != nil && strings.HasPrefix(arg, "cmd/") {
goroot := build.Path[0] goroot := build.Path[0]
p := filepath.Join(goroot.Path, "src", arg) p := filepath.Join(goroot.Path, "src", arg)
if st, err1 := os.Stat(p); err1 == nil && st.IsDir() { if st, err1 := os.Stat(p); err1 == nil && st.IsDir() {
@ -89,6 +89,19 @@ func loadPackage(arg string) (*Package, error) {
err = nil err = nil
} }
} }
// Maybe it is a path to a standard command.
if err != nil && (filepath.IsAbs(arg) || isLocalPath(arg)) {
arg, _ := filepath.Abs(arg)
goroot := build.Path[0]
cmd := filepath.Join(goroot.Path, "src", "cmd") + string(filepath.Separator)
if st, err1 := os.Stat(arg); err1 == nil && st.IsDir() && strings.HasPrefix(arg, cmd) {
t = goroot
importPath = filepath.FromSlash(arg[len(cmd):])
dir = arg
err = nil
}
}
if err != nil { if err != nil {
return nil, err return nil, err
} }