Couple of problems coming from VSCode

I recently moved over to IntelliJ, from VSCode, after a few recommendations. I've had the normal issues one gets with a new IDE (key-mappings etc), but there's a couple I just can't figure out.

 

Problem 1. (Go Tests)

Say I have the following project structure in /Users/me/project

project

\-- /db

\---- db.go

\---- db_test.go

 

I can't run a test in db_test.go (by pressing the green play button), without getting an undefined error because it can't see the function in db.go.

I've figured out that I can go in and change the Configurations to run the directory, but I still can't use the green play button because it creates a new configuration.

Is there not a default somewhere I can set to have every Go test run as "directory" so I don't need to add a brand new configuration for every test I want to run, and then have to screw around when I want to run one individual test.

This just "works" in VSCode, so I'm hoping it's an easy thing that I've overlooked for IntelliJ.

 
Problem 2. (Random formatting)
 
I have setup gofmt, on save, for my Go code. However I am also getting weird formatting behaviour when I'm in the middle of writing my code, for example:
 
  • While I'm making a new function, if I add parenthesis and don't add anything in them straight away - they get deleted. 
  • If I add a space and don't immediately start typing - it gets deleted. 

Is there a setting somewhere to stop all this formatting, because it's very annoying. 

0
31 comments
Official comment

Hi.

> Is there not a default somewhere I can set to have every Go test run as "directory" so I don't need to add a brand new configuration for every test I want to run, and then have to screw around when I want to run one individual test.

Run configurations are created from templates (Run > Edit Configurations > Templates > Go Test). Could you please check what is your default value there? In my case it is Directory, it should be the same for you (maybe you changed it at some point?).

Avatar
Permanently deleted user

OK Problem number 2 fixed. 

 

The issue was not actually IntelliJ, but gofmt. I fixed it by:

 

Preferences --> Tools --> File Watchers --> Open the go fmt watcher --> Uncheck "Auto-save edited files to trigger the watcher"

0
Avatar
Permanently deleted user

Hi Marat.

My template is set to "Directory", but it is set to the project directory. Could that be causing the issues with my "db" subdirectory? 

0

No, it should not. 

What is your IDE version?

Could try to reproduce the issue with some hello-world style project? Just create the project with one package, any function and any dummy test for it. If the issue will remain, please send this project to me.

0

Also, before sending, please open Run > Edit Configurations and check "Share" for the incorrectly generated run configuration. That way it will be saved to the project, and I will be able to see it.

0
Avatar
Permanently deleted user

Apologies for the delay, I'd gone home when your reply came through. 

I recreated the issue in a dummy project, but I can't upload here (it only accepts images). So, here's the code and the test configs instead:

main.go

package main

func Add(a, b int) int {
return a+b
}

 

main_test.go

 

package main

import "testing"

func TestAdd(t *testing.T) {
res := Add(1, 2)

if res != 3 {
t.Errorf("Fail")
}
}

 

The options I get when I want to run a test:

 

This then creates the following test config (username redacted):

 

And this is my Go Test template (username redacted):

Even if I change the test config to be "Directory" I cannot use the "Play" button, because it just creates a new config. 

 

Hope that's enough, but if you need anything else just let me know. I appreciate the help so far.

0

Also, what is your IDE version?

0
Avatar
Permanently deleted user

Sorry - yes you did ask me that. It's IntelliJ Idea Ultimate 2018.3

0

IDE also has a minor version number, the latest IntelliJ Idea Ultimate version is 2018.3.5. What is the minor version in your case? To check this, select IntelliJ -> About IntelliJ from the main menu.

0
Avatar
Permanently deleted user

Yeah - 2018.3.5

 

IntelliJ IDEA 2018.3.5 (Ultimate Edition)
Build #IU-183.5912.21, built on February 26, 2019
JRE: 1.8.0_152-release-1343-b28 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.2

0

Hm.. Your project uses Go modules, right? (I see it is outside of the GOPATH). If true, have you enabled Go modules integration (Preferences > Go > Go Modules)?

0
Avatar
Permanently deleted user

No - I'm not using Go Modules. I do mostly create my projects outside the GOPATH, but they are still using the packages defined in it. 

 

I enabled Go Modules, to see if that would fix my issue, but it actually broke a lot more things. It couldn't resolve any of my dependencies. 

0

I am able to reproduce your issue with a project outside of GOPATH, but without Go modules enabled.

But I cannot understand the setup. Actually, any Go projects should either reside in GOPATH, or use modules if they are outside it. I try to understand what is the exact behavior of the go command when we have a project outside GOPATH with no go modules defined. What is your Go version?

0
Avatar
Permanently deleted user

There are a number of devs that don't want to work in the GOPATH for their projects. All of my dependencies are in the GOPATH and that is absolutely fine, but my projects are under Version Control and in my home directory. 

 

I don't know what the answer is, but this is isolated to IntelliJ because it works absolutely fine in VS Code; so it's not an environmental issue. 

 

go version go1.11.5 darwin/amd64

0
Avatar
Permanently deleted user

Marat - do you have any further suggestions?

0

Ok, I found out what is going on here. 

First of all, when a Run configuration is created automatically (when you press a Play button) from a template, GoLand can ignore the "Run kind" option. We infer this option for each project setup separately. I admit that this behavior is a non-intuitive one, we will address this here: 

https://youtrack.jetbrains.com/issue/GO-7013

Second, you have a project that is outside of GOPATH but doesn't use Go modules. This is a valid setup, although a non-typical one (https://golang.org/doc/code.html#Overview).

In general case, we cannot detect what is a programmer's intention when he asks to run a test in such a non-typical setup. We use "File" for "Run kind" option by default for such cases as the simplest and the cheapest one (no need to compile possibly unrelated files in the whole directory).

We could change it to "Directory" to support non-typical setups, but we first need to be sure this will be useful to a lot of programmers. So, can you please describe your workflow in a bit more detail? Specifically, why don't you use Go modules/place project under GOPATH?

0
Avatar
Permanently deleted user

Hi Marat,

Thanks for the detail. Is there anything I can do to make this work with my set-up or is it simply not possible with IntelliJ at this time?

Here are the answers to your questions:

  • I keep all of my projects in my home directory. This is how the whole team works here, I guess mostly because this is how we have worked before Go. It seems unintuitive to have packages and local projects in one directory, so we don't. We made this decision when we moved over to Go (from Node) and I guess we moved our "way of working" with us. 

 

  • Why we don't use Go modules is an easy one to answer. Before I raised this discussion, I hadn't heard of them. I tried to turn on the modules setting in IntelliJ but all the imports broke. I need to read up more on Go modules and what they would do for us, before I can make that recommendation to the team. 
0

There is a way to work around the problem without changing anything. Try to Right-click on the directory (say, `db` in your first example) and select Run > go test. This way you will enforce "Directory" property. However, this will create a Run configuration for all tests in the directory, not for a single test/test file.

0
Avatar
Permanently deleted user

Hi Marat,

OK - that doesn't really give me what I want. I think I'm just going to send my license back and carry on using VSCode. 

0
Avatar
Permanently deleted user

I suppose my only options are to move all my projects to the `$GOPATH`, not use the "Play" button, run tests by file or move to VSC? 

 

Have I missed anything?

0

Hi Sdrake,

 

I have one question regarding your setup which will help us understand how your projects are created.

You mentioned that the projects are not under "$GOPATH/src", correct?
If so, how do you compile projects which for example have to import packages defined inside them? For example, `project/db` would be one such import path. Is this correct? If not, is it something along the lines of `github.com/org/project/db` (you can replace `github.com` with any hosting you may have). What is the `go build` or `go run` command you are running?
Or maybe you are using symlinks from $project to $GOPATH/src/$project?

Thank you.

 

0
Avatar
Permanently deleted user

Hi Florin, 

My projects are not under "$GOPATH/src", no. Rightly or wrongly, since we started working with Go (~6 months ago) we've always had our repositories outside and haven't had any issues. 

Regarding the import question; I can still import from the $GOPATH (e.g. "log", "testing", "fmt") absolutely fine. I can also import non-standard packages fine (e.g. "github.com/dgraph-io/dgo"). 

If I then have sub-directories in a project (e.g. a subdirectory called "db"), then I can simply import this relative to the file I am working in. So if I'm working in "main.go" I'd use a relative import of "./db", if I'm working in a different subdirectory then I'd import it using "../db" and I can use all of the methods required. 

As for the actual build commands - if I'm in the directory I just run the following:

go build -o build/darwin/d_project
env GOOS=linux GOARCH=amd64 go build -o build/linux/l_project

Which gives me the Linux and darwin compiled versions. 

For running the project we simply use "go run -race main.go"

 

I'll admit I didn't realise we were doing things that out of the ordinary.....

 

Does any of this help to understand what we're doing?

0

Ok, I can add a so-called "register key" (something like a deeply hidden setting) so that you will be able to press the Play button and have Run configuration with Directory kind even in your setup.

But first, I would like to clarify one thing.

You said that the workaround with the right-clicking and selecting Run -> go test on the directory doesn't really give you what you want. The solution I am going to implement will effectively do the same thing (create Run configuration with Directory kind) but will make it available via the Play button, not via right-clicking and selecting Run -> go test. Do I understand that you do not like the workaround solution because the Play button is better from a usability point of view?

Also, I would like to mention that you can use a keybinding instead of right-clicking and selecting, so the workaround solution could be easier to invoke that way.

0
Avatar
Permanently deleted user

Hi Marat,

 

Just to clarify - this change would run all of the tests in the file and wouldn't give me the ability to run one single test? 

0

Oh, I see now. No, it will allow running a single test. And this makes it different from the workaround I mentioned. My previous comment about "will effectively do the same thing" is incorrect.

So, I will add a special setting to cover your case. I will ping you here as I will finish with it.

0
Avatar
Permanently deleted user

Perfect - thanks! That would be great. 

 

I'm asking mostly out of curiosity, not to push - do you have an ETA on how long you think this will take you?

0

I cannot say for sure, but I will try to add the option before the end of the next week.

0
Avatar
Permanently deleted user

OK. Excellent. Thank you. 

0

I am back. I added a special flag in the settings so that when you press the Play button, generated Run Configurations will have Directory run kind.

How to enable: 

1) Open a Registry (press Shift twice, type "Registry").

2) Select "go.enforce.directory.run.kind.for.tests.enabled".

This will be available in the upcoming 2019.1 release. We already entered a beta stage (https://blog.jetbrains.com/go/2019/03/14/goland-2019-1-goes-beta/), the release will be soon. If you will have any problems with the feature, do not hesitate to ask me here.

0
Avatar
Permanently deleted user

Excellent - thank you for the update! 

 

Do you have any sort of time frame for the release?

0

Please sign in to leave a comment.