Skip to content

[master] Special treatment for "C" package.#44

Open
hotpxl wants to merge 2 commits into
rogpeppe:masterfrom
hotpxl:master
Open

[master] Special treatment for "C" package.#44
hotpxl wants to merge 2 commits into
rogpeppe:masterfrom
hotpxl:master

Conversation

@hotpxl
Copy link
Copy Markdown

@hotpxl hotpxl commented Jul 27, 2016

Fixes #41 (at least for me).

import "C" is not a real package. So godef cannot correctly parse any package that says import "C". This adds a special treatment for this package and circumvents this problem.

@TargetLiu
Copy link
Copy Markdown

TargetLiu commented Aug 1, 2016

It works, for me, thx

@applee
Copy link
Copy Markdown

applee commented Aug 22, 2016

It has no effect in go1.7 linux/amd64.

@hotpxl
Copy link
Copy Markdown
Author

hotpxl commented Aug 22, 2016

@applee How does it look on your side? It's a hard-coded workaround for "C". It cannot jump to definition on "C" but at least parses for other definitions.

Copy link
Copy Markdown

@liggitt liggitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this worked for me

Comment thread go/parser/parser.go Outdated
if declIdent == nil {
filename := p.fset.Position(path.Pos()).Filename
name, err := p.pathToName(litToString(path), filepath.Dir(filename))
if litToString(path) == "C" {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe only do this if name is also "" (and would therefore fail otherwise)?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds reasonable.

@muirdm muirdm mentioned this pull request Oct 3, 2016
@muirdm
Copy link
Copy Markdown

muirdm commented Oct 3, 2016

This fixed it for me as well. Can this be merged?

@Ambrevar
Copy link
Copy Markdown

Ambrevar commented Nov 8, 2016

I had the same issue with https://github.com/aarzilli/golua. This PR fixes it. (On 1.7 as well, btw.)

@jwendel
Copy link
Copy Markdown

jwendel commented Jan 24, 2017

At least for me, this patch doesn't work anymore with vendor directories.

 D:\go\bin\godef.exe -debug -f main.go net.LookupIP
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.SelectorExpr net.LookupIP [
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.Ident net [
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/01/23 21:33:57 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/01/23 21:33:57 ] -> 0xc04219ee60, Type{package "" *ast.ImportSpec "net"}
2017/01/23 21:33:57 member Type{package "" *ast.ImportSpec "net"} 'LookupIP' {
2017/01/23 21:33:57     C:\go\src\net\interface_bsd.go:12:2: cannot find identifier for package "golang_org/x/net/route": no buildable Go source files in C:\go\src\vendor\golang_org\x\net\route
2017/01/23 21:33:57 } -> <nil>
2017/01/23 21:33:57 ] -> 0x0, Type{bad "" <nil> }
parseLocalPackage error: no more package files found
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.SelectorExpr net.LookupIP [
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.Ident net [
2017/01/23 21:33:57 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/01/23 21:33:57 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/01/23 21:33:57 ] -> 0xc04219ee60, Type{package "" *ast.ImportSpec "net"}
2017/01/23 21:33:57 member Type{package "" *ast.ImportSpec "net"} 'LookupIP' {
2017/01/23 21:33:57     C:\go\src\net\interface_bsd.go:12:2: cannot find identifier for package "golang_org/x/net/route": no buildable Go source files in C:\go\src\vendor\golang_org\x\net\route
2017/01/23 21:33:57 } -> <nil>
2017/01/23 21:33:57 ] -> 0x0, Type{bad "" <nil> }
godef: no declaration found for net.LookupIP

@bboreham
Copy link
Copy Markdown

Since the go subtree is a copy of that part of the Go distribution, it seemed to me that overriding the pathToName function would be nicer than hacking the code to override the result, but unfortunately types.DefaultImporter has a hard-coded reference to DefaultImportPathToName, and it proved too hard to extract a copy of DefaultImporter without dragging half the rest of the library with it.

@shahuwang
Copy link
Copy Markdown

I am encountering this problem, too.
My OS System is Mint Qiana, which based on Ubuntu 14.04

Distributor ID: LinuxMint
Description:    Linux Mint 17 Qiana
Release:        17
Codename:       qiana

Go version is 1.8 ( also wrong in 1.7, too).
With godef --debug -f main.go net.LookIP , the debug log shows:

2017/03/23 15:03:13 exprType tuple:false pkg: *ast.SelectorExpr net.LookIP [
2017/03/23 15:03:13 exprType tuple:false pkg: *ast.Ident net [
2017/03/23 15:03:13 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/03/23 15:03:13 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:03:13 ] -> 0xc42013de00, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:03:13 member Type{package "" *ast.ImportSpec "net"} 'LookIP' {
2017/03/23 15:03:13     /home/shahuwang/go/src/net/cgo_openbsd.go:12:8: cannot find identifier for package "C": cannot find package "C" in any of:
        /home/shahuwang/go/src/vendor/C (vendor tree)
        /home/shahuwang/go/src/C (from $GOROOT)
        /home/shahuwang/github/src/C (from $GOPATH)
2017/03/23 15:03:13 } -> <nil>
2017/03/23 15:03:13 ] -> 0x0, Type{bad "" <nil> }
2017/03/23 15:03:13 exprType tuple:false pkg: *ast.SelectorExpr net.LookIP [
2017/03/23 15:03:13 exprType tuple:false pkg: *ast.Ident net [
2017/03/23 15:03:13 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/03/23 15:03:13 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:03:13 ] -> 0xc42013de00, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:03:13 member Type{package "" *ast.ImportSpec "net"} 'LookIP' {
2017/03/23 15:03:13     /home/shahuwang/go/src/net/cgo_openbsd.go:12:8: cannot find identifier for package "C": cannot find package "C" in any of:
        /home/shahuwang/go/src/vendor/C (vendor tree)
        /home/shahuwang/go/src/C (from $GOROOT)
        /home/shahuwang/github/src/C (from $GOPATH)
2017/03/23 15:03:13 } -> <nil>
2017/03/23 15:03:13 ] -> 0x0, Type{bad "" <nil> }
godef: no declaration found for net.LookIP

#44 fixes the "C" import problem, but hit another problem:

2017/03/23 15:10:59 exprType tuple:false pkg: *ast.SelectorExpr net.LookIP [
2017/03/23 15:10:59 exprType tuple:false pkg: *ast.Ident net [
2017/03/23 15:10:59 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/03/23 15:10:59 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:10:59 ] -> 0xc42012fe00, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:10:59 member Type{package "" *ast.ImportSpec "net"} 'LookIP' {
2017/03/23 15:10:59     /home/shahuwang/go/src/net/interface_bsd.go:12:2: cannot find identifier for package "golang_org/x/net/route": no buildable Go source files in /home/shahuwang/go/src/vendor/golang_org/x/net/route
2017/03/23 15:10:59 } -> <nil>
2017/03/23 15:10:59 ] -> 0x0, Type{bad "" <nil> }
2017/03/23 15:10:59 exprType tuple:false pkg: *ast.SelectorExpr net.LookIP [
2017/03/23 15:10:59 exprType tuple:false pkg: *ast.Ident net [
2017/03/23 15:10:59 exprType tuple:false pkg: *ast.ImportSpec "net" [
2017/03/23 15:10:59 ] -> 0x0, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:10:59 ] -> 0xc42012fe00, Type{package "" *ast.ImportSpec "net"}
2017/03/23 15:10:59 member Type{package "" *ast.ImportSpec "net"} 'LookIP' {
2017/03/23 15:10:59     /home/shahuwang/go/src/net/interface_bsd.go:12:2: cannot find identifier for package "golang_org/x/net/route": no buildable Go source files in /home/shahuwang/go/src/vendor/golang_org/x/net/route
2017/03/23 15:10:59 } -> <nil>
2017/03/23 15:10:59 ] -> 0x0, Type{bad "" <nil> }
godef: no declaration found for net.LookIP

@bboreham
Copy link
Copy Markdown

bboreham commented May 4, 2017

@jwendel @shahuwang you might like to try #61 as an alternative solution

@bboreham
Copy link
Copy Markdown

Now I believe this is obsoleted by #62

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants