4

I have a Go project with a structure like this:

cmd
  myapp
    main.go
internal
  app
    myapp
      impl.go

Now in main.go, I want to import impl.go. GoLand tells me that I should do it like this:

import (
  "myproj/internal/app/myapp"
)

However, I compile the project on another machine (because I use EGL and that's not available on macOS). When I do

go build cmd/myapp/main.go

there, it cannot resolve the import. So I changed the import to use the full path of my github repo, like this:

import (
  "github.com/flyx/myproj/internal/app/myapp"
)

Code compiles fine now. However, after I pulled that back into GoLand, it cannot resolve the import anymore. Curiously, every item in the path gives a separate error, e.g.

Cannot resolve directory 'github.com'

That's very strange since github.com is very obviously in my $GOPATH/src and other imports from there that are not my current project are working.

So… what am I doing wrong? I don't even understand how GoLand can resolve the path starting with myproj. I checked whether GOPATH is correctly set in GoLand and that's the case.

flyx
  • 35,506
  • 7
  • 89
  • 126
  • Is it a Go Modules project? If not is it located under `$GOPATH` on disk? – artspb Apr 20 '19 at 17:15
  • 1
    None of this should work. In Go one imports *packages*, not *files*. See [How to Write Go Code](https://golang.org/doc/code.html#Library). – Peter Apr 20 '19 at 17:34
  • @Peter Sorry, screwed the paths during writeup out of habit. Actual code does import the packages, I fixed it. – flyx Apr 20 '19 at 17:41
  • 1
    @artspb The project is at `$GOPATH/src/github.com/flyx/myproj/` on disk. I am unsure what would make the project a *Go Modules project*; I do have a `go.mod` lying around, but I don't use it since the compiler on the target machine is too old. – flyx Apr 20 '19 at 17:44
  • @flyx Do you have Go Modules integration enabled under _Settings | Go | Go Modules (vgo)_? If yes then try switching it off. – artspb Apr 20 '19 at 17:48
  • @artspb That actually fixed it \o/. But why would it not import my package with that thing turned on? I use the whole path for external packages I import and it works just fine there. – flyx Apr 20 '19 at 17:52
  • @flyx What GoLand version do you use? There are some fixes for Go Modules projects under `$GOPATH` in the latest 2019.1.1. – artspb Apr 20 '19 at 17:54
  • @artspb `go version go1.12.1 darwin/amd64` on the dev machine and `go version go1.10.4 linux/arm64` on the target machine – flyx Apr 20 '19 at 17:58
  • @flyx Sorry, I was asking about GoLand. I've edited my message too late, apparently. – artspb Apr 20 '19 at 17:59
  • @artspb I just updated to 2019.1.1 but the problem stays the same when I enable module integration. If this is a GoLand bug, you can post your solution as answer. – flyx Apr 20 '19 at 18:01
  • @flyx It's hard to tell. I've just tried to recreate the problem but didn't succeed. It'd be great if you file a new issue [here](https://youtrack.jetbrains.com/issues/GO) where describe steps to reproduce or better attach an example project. – artspb Apr 20 '19 at 18:13
  • 1
    @artspb In my go.mod, there was `module myproj` instead of `module github.com/flyx/myproj`, that was the problem. I'll still open a bug report since I don't see why GoLang should fail that way in this setup. – flyx Apr 20 '19 at 18:20
  • Note I can't find the error you see (`Cannot resolve directory 'github.com'`) in the Go toolchain, which suggests that the error is in Goland, and not the Go toolchain (unless I missed it, of course). – Martin Tournoij Apr 21 '19 at 03:57

0 Answers0