Commit f891b7c3 authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/doc: avoid calling token.IsExported on non-tokens

token.IsExported expects to be passed a token, and does not check for
non-token arguments such as "C:\workdir\go\src\text".

While we're at it, clean up a few other parts of the code that
are assuming a package path where a directory may be passed instead.
There are probably others lurking around here, but I believe this
change is sufficient to get past the test failures on the
windows-amd64-longtest builder.

Fixes #35236

Change-Id: Ic79fa035531ca0777f64b1446c2f9237397b1bdf
Reviewed-on: https://go-review.googlesource.com/c/go/+/204442
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: default avatarRob Pike <r@golang.org>
Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
parent 6dc250f4
...@@ -227,19 +227,28 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo ...@@ -227,19 +227,28 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
return nil, args[0], args[1], false return nil, args[0], args[1], false
} }
// Usual case: one argument. // Usual case: one argument.
// If it contains slashes, it begins with a package path. // If it contains slashes, it begins with either a package path
// or an absolute directory.
// First, is it a complete package path as it is? If so, we are done. // First, is it a complete package path as it is? If so, we are done.
// This avoids confusion over package paths that have other // This avoids confusion over package paths that have other
// package paths as their prefix. // package paths as their prefix.
pkg, importErr := build.Import(arg, wd, build.ImportComment) var importErr error
if importErr == nil { if filepath.IsAbs(arg) {
return pkg, arg, "", false pkg, importErr = build.ImportDir(arg, build.ImportComment)
if importErr == nil {
return pkg, arg, "", false
}
} else {
pkg, importErr = build.Import(arg, wd, build.ImportComment)
if importErr == nil {
return pkg, arg, "", false
}
} }
// Another disambiguator: If the symbol starts with an upper // Another disambiguator: If the argument starts with an upper
// case letter, it can only be a symbol in the current directory. // case letter, it can only be a symbol in the current directory.
// Kills the problem caused by case-insensitive file systems // Kills the problem caused by case-insensitive file systems
// matching an upper case name as a package name. // matching an upper case name as a package name.
if token.IsExported(arg) { if !strings.ContainsAny(arg, `/\`) && token.IsExported(arg) {
pkg, err := build.ImportDir(".", build.ImportComment) pkg, err := build.ImportDir(".", build.ImportComment)
if err == nil { if err == nil {
return pkg, "", arg, false return pkg, "", arg, false
...@@ -373,9 +382,6 @@ func isExported(name string) bool { ...@@ -373,9 +382,6 @@ func isExported(name string) bool {
// findNextPackage returns the next full file name path that matches the // findNextPackage returns the next full file name path that matches the
// (perhaps partial) package path pkg. The boolean reports if any match was found. // (perhaps partial) package path pkg. The boolean reports if any match was found.
func findNextPackage(pkg string) (string, bool) { func findNextPackage(pkg string) (string, bool) {
if pkg == "" || token.IsExported(pkg) { // Upper case symbol cannot be a package name.
return "", false
}
if filepath.IsAbs(pkg) { if filepath.IsAbs(pkg) {
if dirs.offset == 0 { if dirs.offset == 0 {
dirs.offset = -1 dirs.offset = -1
...@@ -383,6 +389,9 @@ func findNextPackage(pkg string) (string, bool) { ...@@ -383,6 +389,9 @@ func findNextPackage(pkg string) (string, bool) {
} }
return "", false return "", false
} }
if pkg == "" || token.IsExported(pkg) { // Upper case symbol cannot be a package name.
return "", false
}
pkg = path.Clean(pkg) pkg = path.Clean(pkg)
pkgSuffix := "/" + pkg pkgSuffix := "/" + pkg
for { for {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment