Can't run go tests from GoLand (Intellij Idea): compilation failed - go

I've made simple application in go and trying to run tests with GUI tools of GoLand.
In myfile_test.go file near test func I press green button that should start test. But I get error message: "Compilation failed" and message in console:
# command-line-arguments [command-line-arguments.test]
./myfile_test.go:21:11: undefined: MyStruct
./myfile_test.go:22:12: undefined: MyFuncName
./myfile_test.go:33:12: undefined: AnotherStruct
Compilation finished with exit code 2
Other variants (Run test with Coverage/CPU Profile) don't work either. GoLand 2020.1 EAP. The same problem occurred in older versions of GoLand.
But test from console starts normally:
go test -v
=== RUN TestMyStruct_MyMethod
--- PASS: TestMyStruct_MyMethod (0.00s)
PASS
ok _/home/username/projects/my_project_name 0.002s

The answer from #porfirion worked for me.
Basically, you need to tick "Enable Go Modules integration" in GoLand under "Preferences" -> "Go" -> "Go Modules"
Goland will then reindex your project and this can take a long time if you have a large project with many modules. Mine took 30 mins. You can check the reindexing progress status at the bottom of the IDE.
The test run will work after the reindexing.

I solved the problem with initializing new go module and enabling Go Modules Integration:
1) Run in Terminal go mod init my_module_name
2) Click "File -> Settings" or press Ctrl+Alt+S
3) Check "Enable Go Modules Integration" and Apply button
Now all test functionality in GoLand works well (including tests with coverage, etc.)

The issue here is that you're pointing to a single file. All other files, even though they're in the package, aren't going to be imported. go build has the same behaviour, if you are targeting a single file to build and forget to include other files it uses in the command.
Change the test kind to "Directory" and files to "/project-root-dir". This will then include all files in the directory during build and should look for all _test.go files to execute tests.

Related

Can't run test function in GoLand: cannot find package "."

I have used VS Code as my code editor for building service using Go for almost one year. Then, I tried to switch to GoLand. But, when I tried run a test function there is an error: cannot find package "." What is the problem?
Note: I use go module as go dependency management tool. When I use dep (in another project), there is no error when running a test function. My project is in GOPATH.
Please make sure there's a valid go.mod file defined at the ROOT of your project i.e. in $GOPATH/src/<Project-name>.
If you don't have, you can create one using go mod init command using the shell. More information on the same - https://github.com/golang/go/wiki/Modules
After that please try running the test from the shell. First cd into the directory where the test file is present. Then, use go test . -test "<TestName>" -v to run your test. If the issue goes away, you can run the test from IDE and it should work fine.

Running GUI apps in Goland IDE

When I build in Terminal, I can use a flag to say I want to build for GUI:
go build -ldflags="-H windowsgui"
However, I just started using JetBrains Goland and I don't how to run GUI apps. What can I do?
go build will only build the application.
To actually run the application, you should go to Run | Edit Configurations... | + | Go Application and configure the application as you need.
Here you will need to set two options:
add -ldflags="-H windowsgui" to the Go tool arguments option
configure the Output directory to be in the same directory as your .manifest file
Setting the output directory is critical in order to run the the application without encountering the following panic panic: TTM_ADDTOOL failed described in this issue.
Then you can run the configuration via Run | Run... and select the configuration you've just created.

How to run/debug a beego app using Gogland (go language)

Im using Gogland (IDE from JetBrains. Version 1.0 Preview/EAP Feb, 10 2017) to create a Beego web app.
I can run it from command line with:
bee run
and everything works.
However if I run it from the IDE with the following configuration
when I go to localhost:8080 it says it can not find the template file in path:
I thought it was related to GOPATH, but then I realized that Gogland IDE is probably running
go run main.go
instead of
bee go
and when I checked runing
go run main.go
from the shell, I got the same issue: cant find the template.
I even tried to run the 'bee' command from the IDE. I partially succeed. With this configuration:
I can run it from the IDE, but the debugger doesn't stop in any breakpoint. IE: I can only run (but not debug) it, from Gogland.
So my question is how to make gogland IDE debug a beego project
Use Delve and Remote Debugging Configuration(start from gogland eap9).
Run your bee app like:
bee dlv -package="app_name" -port=2345
On gogland make configuration Go Remote with port which you set previous.
You cannot change the IDE to run the bee command. However, you can change the run configuration to a Go Application by going to Run | Edit Configurations | + | Go Application select the package type then type the full package name (github.com/dlsniper/demo for example if your package main is under GOPATH/src/github.com/dlsniper/demo) and make sure the working directory is where you need it to be. Hope it helps.
I resolve this by setting:
beego.BConfig.WebConfig.ViewsPath="/Users/xxxxx/go/src/xxxxxxx/views" // your views directory
by the way: if you have staticpath , you should also use absolute path.
eg: beego.SetStaticPath("/static", "/Users/xxx/go/src/xxxx/static")
it's only used when debug, if you publish your program, you should remove this hard code config.

How to run the whole project in GoLand?

I have a small project with several .go files. When I run main.go only this compiles but nothing else, so my application crushes. I understand that I have to change settings in Run -> Edit Configurations, but don't know what to do exactly. IDE also doesn’t see terminal pre-compiled package, so "Package" option instead of "File" doesn't work.
To run the whole project you have to go Run -> Edit Configuration, set Run Kind to Package and type in field Package your project directory name.

Recommended Go build system for a CI server?

So I have a go project with go-gettable dependencies, tests, etc.
I want to integrate it into Jenkins. Is there an automated build system for go that anyone recommends for this use case, other than writing makefiles?
I need:
automatic installation of go-get deps (they can be in a spec file of course)
recursive build.
running tests.
GOPATH/GOROOT management (to isolate SDKs and paths)
I've used godag in the past for this sort of job but it seems a bit unmaintained.
EDIT: For the time being I'm living with the following script entered directly into Jenkins as a build step:
#this gets the dependencies but doesn't install them, avoiding permission problems
go get -d
#build the packages, -x outputs the compiler command line
go build -x
#this was tricky - ./... means "for each sub-package recursively"
go test ./...
You can do it with teamcity as well.
Here is an example for building terraform.
Teamcity agent setup:
Install Go
Add Go to path
Don't forget to restart agent as path was changed
Teamcity build setup:
Use agent side checkout (if we want to include the .git folder, which a lot of build scripts make use of)
Use checkout rule (we want to use the Go convention):
+:. => src/github.com/mitchellh/terraform
Build steps:
echo cd %system.teamcity.build.checkoutDir%
cd "%system.teamcity.build.checkoutDir%"
path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
echo path=C:\Program Files (x86)\Git\bin;%env.Path%;%system.teamcity.build.checkoutDir%\bin;
set GOPATH=%system.teamcity.build.checkoutDir%
echo set GOPATH=%system.teamcity.build.checkoutDir%
echo "Getting dependancies"
go get golang.org/x/tools/cmd/vet
go get golang.org/x/tools/cmd/cover
go get golang.org/x/tools/cmd/stringer
go get github.com/mitchellh/gox
echo cd %system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform
cd "%system.teamcity.build.checkoutDir%\src\github.com\mitchellh\terraform"
echo "Update resources"
go get ./...
echo "Run static code analysis"
go tool vet -asmdecl -atomic -bool -buildtags -copylocks -methods -nilfunc -printf -rangeloops -shift -structtags -unsafeptr .
echo "Build"
cd scripts
sh build.sh
echo "Run unit tests"
go test -timeout=30s -parallel=4
echo "Run code coverage"
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
rm coverage.out
I am using a Team City build server on a Mac that runs a rake file, in the rake file I do all the commands to get dependancies, (go gets), tests and builds to the correct environment.
let me know if you want any pointers in writing the Rake files,
As a side note, i have been creating functional tests for REST Api's using this frame work. This has saved my code many times over. http://github.com/DigitalInnovation/cucumber_rest_api
Since August 2019, TeamCity 2019.1 now supports Go directly.
See "Building Go programs in TeamCity"
To enable Go support in TeamCity,
go to Build Configuration Settings | Build Features,
click Add build feature, and
select Golang from the list.
Support for Go is provided by TeamCity out of the box, there are no external plugins required.
TeamCity parses results of go test command execution. The results are persisted and it is possible to review the figures in a historical perspective.
Consequently, all the TeamCity features that are related to test reporting are now available for Go developers.

Resources