Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

User-written packages fail to import with go.mod. GO111MOD=on #1942

Closed
dreamheld opened this issue Sep 25, 2018 · 13 comments
Closed

User-written packages fail to import with go.mod. GO111MOD=on #1942

dreamheld opened this issue Sep 25, 2018 · 13 comments
Labels
go-modules Related to Go modules

Comments

@dreamheld
Copy link

dreamheld commented Sep 25, 2018

The issue is not with the custom packages vendored through online repos, but with those that you would write for your application yourself. They do not import and are invisible to VSCode.

Steps to Reproduce:

  1. Create a new directory (vstest) outside of the current GOPATH.

  2. Navigate to the new directory and run $go mod init vstest

  3. Create a subfolder "packtest" with a packtest.go in it.
    3.a packtest.go contains:
    package packtest import "fmt" //PackPrint tests the autocomplete of user packages in vscode func PackPrint(){fmt.Println("PackTest Called")

  4. Navigate back up to the vstest folder and create main.go.

  5. In main.go try to call packtest.PackPrint(). It fails. Not only VSCode doesn't auto-complete the new package, it doesn't import it either. It also doesn't have it listed in the "Add Import..." option.

(go.autocompleteUnimportedPackages": true)
(env GO111MODULE=on)

Using the import statement like 'import "packtest"' doesn't seem to work either. package main gets highlighted in red with the error "Build vstest: cannot find module for path packtest".

packtest

image

image

@lkarlslund
Copy link

This is not a VS Code problem. This is because that's not the way you import packages if you use Go Modules :-)

When using Go, there is no such thing as a relative path. This will not work with "go build main.go" from the commandline either, given your scenario.

When you do an import, Go looks for the package in:

GOROOT/src
GOPATH/src
vendor paths in current folder or parent folders, working its way upwards (if you're inside GOPATH)
modules from go.mod in current folder or parent folders, working its way upwards (if you're outside GOPATH)
.... and nowhere else.

In your go.mod, your own package name is defined in the first line as module , typically "github.com/username/packagename" if you're using Github at least.

Your import statement in main.go should be:

import "/packtest"

And you don't need to turn GO111MODULE=on, if you're outside GOPATH it just works from Go 1.11 and onwards.

@dreamheld
Copy link
Author

dreamheld commented Sep 26, 2018

This is not a VS Code problem. This is because that's not the way you import packages if you use Go Modules :-)
...
Right. That's my bad. However, I did try the import "/packtest" statement too this afternoon (just in case it was something along the lines of what you were saying). The terminal would just hang. I'd have to Ctrl+C to break out of that.

And even w/ the "/packtest" line, there was still no auto-fill of the package methods. :/

P.S.
Also, the functionality of the plugin with other packages was to fetch the method/autofill lists either after a "." or pressing Ctrl+S after typing out the package name. This doesn't seem to happen anywhere outside of the GOPATH anymore. I could reproduce that.

Then again, I might just be doing something utterly wrong!

@ramya-rao-a ramya-rao-a added the go-modules Related to Go modules label Sep 26, 2018
@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 27, 2018

@dreamheld After updating the import statement to "vstest/packtest" and adding a line that uses the package, I am able to get the completions when using the latest beta version of this extension

image

If you are using the same beta version, did you get the prompts to install the fork of gocode? And did you install it?

Can you try killing all running processes for gocode and gocode-gomod and try again?

@ramya-rao-a
Copy link
Contributor

@uudashr In the above code set-up, if I run Go: Browse Packages, the vstest/packtest doesnt get listed i.e gopkgs doesnt return the vstest/packtest.

@dreamheld
Copy link
Author

I wasn't able to respond to this last night, but I think my issue was indeed that I never killed gocode process.

The new beta (as of 20 min ago) seems to do the autocomplete for methods fine, but doesn't fill out the import statement automatically anymore. Also, package/method highlighting for the new package seems off. I'll poke more at it though

@uudashr
Copy link
Contributor

uudashr commented Sep 27, 2018

My mistakes... gopkgs fixed, please give it a try, you need to re-install thegopkgs cc @ramya-rao-a

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Sep 27, 2018

Thanks @uudashr! It works now.

@dreamheld

but doesn't fill out the import statement automatically anymore

You mean that in a file where packtest is not yet imported, typing pac doesnt give you a completion item that would add the import?

If yes, then I can confirm that @uudashr's updated gopkgs tool will fix that.

Run Go: Install/Update Tools, select gopkgs, press OK. That will update the tool.

@dreamheld
Copy link
Author

@ramya-rao-a , nope. I've updated @uudashr 's tool and everything works swimmingly with automatically adding the import statement and auto-completing the 'packtest' package name.

The only two issues remaining, it seems:

  1. The one you mentioned in a thread above (with method filling showing up only after the method was used already).
  2. Highlighting. Method/Package colors seem to not work anymore. Might be an issue on my end though. Still diagnosing that.

@ramya-rao-a
Copy link
Contributor

method filling showing up only after the method was used already

It also works if the any other symbol from the imported package is used already. This is being tracked in #1944 and I have an upstream issue for that in stamblerre/gocode#3

Method/Package colors seem to not work anymore.

Can you share a screenshot for this?

@dreamheld
Copy link
Author

dreamheld commented Sep 27, 2018

@ramya-rao-a , there! I'm not sure if I broke it somehow and the solution is trivial.
image

@ramya-rao-a
Copy link
Contributor

I dont see any color issue .....

@dreamheld
Copy link
Author

Ah, geez! My screen was in the night mode. so everything looked yellow. Duh... I apologize for wasting you time!

@ramya-rao-a
Copy link
Contributor

lol.... I'll close this issue then.

Thanks for reporting @dreamheld, it helped us catch and fix a bug!

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
go-modules Related to Go modules
Projects
None yet
Development

No branches or pull requests

4 participants