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

x/tools/cmd/godoc: Examples not executable if pkg.name != basename(pkg.path) #12794

Open
alandonovan opened this Issue Sep 30, 2015 · 4 comments

Comments

Projects
None yet
5 participants
@alandonovan
Contributor

alandonovan commented Sep 30, 2015

% export GOPATH=$(pwd)
% cat src/foo1/foo.go
package foo

func Foo() {
}

% cat src/foo1/foo_test.go
package foo_test

import "foo1" // defines foo

func ExampleFoo() {
foo.Foo()
}

% godoc -play -http :9999 &
% open http://localhost:9999/pkg/foo1/#pkg-examples

The example is shown but not executable (grey not yellow); renaming foo1 to foo makes it executable, as does using an explicit (redundant) renaming import:
import foo "foo1"

Seems like some code in godoc is assuming pkg.name == basename(pkg.path) instead of finding the actual package name.

@adg adg changed the title from godoc -play: Examples not executable if pkg.name != basename(pkg.path) to cmd/godoc: Examples not executable if pkg.name != basename(pkg.path) Sep 30, 2015

@adg adg changed the title from cmd/godoc: Examples not executable if pkg.name != basename(pkg.path) to x/tools/cmd/godoc: Examples not executable if pkg.name != basename(pkg.path) Sep 30, 2015

@adg adg self-assigned this Sep 30, 2015

@rsc

This comment has been minimized.

Contributor

rsc commented Oct 23, 2015

Not such a terrible assumption. :-)

@rsc rsc added this to the Go1.6 milestone Oct 23, 2015

@adg adg modified the milestones: Unplanned, Go1.6 Oct 25, 2015

@adg adg removed their assignment Feb 14, 2018

@adg adg added the help wanted label Feb 14, 2018

@ysmolsky

This comment has been minimized.

Member

ysmolsky commented Feb 23, 2018

I see that this example is discarded as not executable in this block of code: https://github.com/golang/go/blob/master/src/go/doc/example.go#L211-L243:

	// Use unresolved identifiers to determine the imports used by this
	// example. The heuristic assumes package names match base import
	// paths for imports w/o renames (should be good enough most of the time).
	namedImports := make(map[string]string) // [name]path
	var blankImports []ast.Spec             // _ imports
	for _, s := range file.Imports {
		p, err := strconv.Unquote(s.Path.Value)
		if err != nil {
			continue
		}
		n := path.Base(p)
		if s.Name != nil {
			n = s.Name.Name
			switch n {
			case "_":
				blankImports = append(blankImports, s)
				continue
			case ".":
				// We can't resolve dot imports (yet).
				return nil
			}
		}
		if unresolved[n] {
			namedImports[n] = p
			delete(unresolved, n)
		}
	}
	// If there are other unresolved identifiers, give up because this
	// synthesized file is not going to build.
	if len(unresolved) > 0 {
		return nil
	}

Specifically, unresolved=map[foo:true] at line 242.

@ysmolsky

This comment has been minimized.

Member

ysmolsky commented Feb 23, 2018

@alandonovan can you hint me how I can figure out what is the actual package name is being imported given it's package name and path? Should program try to prser.parseDir() the actual package to get its name?

@jimmyfrasche

This comment has been minimized.

Member

jimmyfrasche commented May 18, 2018

@ysmolsky I'd use go/build. It's going to do quite a bit of work that's probably unnecessary but it will always be correct and there are a surprising number of edge cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment