GoLand (JetBrains) shows error message "Unresolved Reference". But Code compiles and runs
Asked Answered
P

28

116

I am writing a project using the Go language with GoLand IDE by Jetbrains.

While writing the code, GoLand shows me an error message such as "unresolved reference" when the reference do exist and that the program compiles and runs correctly.

Here is a similar (but simpler) example of some code that I have found here on stackoverflow (Go - append to slice in struct) to reproduce this issue.

The same error message appears even though I have implemented the methods just a few lines above.

package main

import (
    "fmt"
)

type MyBoxItem struct {
    Name string
}

type MyBox struct {
    Items []MyBoxItem
}

func (box *MyBox) AddItem(item MyBoxItem) {
    box.Items = append(box.Items, item)
}

func main() {

    item1 := MyBoxItem{Name: "Test Item 1"}
    item2 := MyBoxItem{Name: "Test Item 2"}

    box := MyBox{}

    box.AddItem(item1)
    box.AddItem(item2)

    // checking the output
    fmt.Println(len(box.Items))
    fmt.Println(box.Items)
}

box.AddItem(item1) and box.AddItem(item2) are marked red as an error. If I move my cursor above it, it says unresolved reference "AddItem". Yet the code compiles and runs. And as this was the solution to an other stackoverflow question, I do not think that the code is wrong. Furthermore I cannot find any mistakes in it.

enter image description here

[EDIT: I load the code from a remote server and edit it locally on my private pc. After finishing my changes, I upload it to the remote server (using GoLands tools like "Browse remote host") and build and compile it there. After trying it out locally with the very same code, the error message sometimes is there and sometimes not. I am totally confused]

Puente answered 1/1, 2020 at 21:53 Comment(7)
I couldn't reproduce the problem, running GoLand 2019.2.5. However, I think that if you use box := &MyBox{} or box := new(MyBox) will "fix" that. Since AddItem is a (box *MyBox).Sapsucker
thanks for the answer @inkeliz . I tried both variants but no difference.Puente
Same issue on version 2020.1.4Worldbeater
I've had this issue but for external package, go mod vendor solved it (just copy the modules to the local folder). This does not answer OP's specific issue but this was the first link I came across for my issue so adding this here for my future self, sorry.Merciful
I had this issue after deleting the go.mod file by mistake and then undoing the delete. Invalidate Cache helpedComplexity
Same issue on version 2022.2, windows, and wsl. Unfortunately, none of the answers worked for me.Hebraic
The fix for me was to go to menu FILE | Invalidate Caches... and click on Invalidate and Restart. It will re-index caches and all references will be resolved. per: https://mcmap.net/q/187506/-goland-jetbrains-shows-error-message-quot-unresolved-reference-quot-but-code-compiles-and-runsImmense
R
232

I experienced a similar issue, but it was a lot more prevalent. Even things like fmt.Printf() were showing as unresolved. Was able to resolve the issue by going to File -> Invalidate Caches / Restart.

Royden answered 22/4, 2020 at 2:49 Comment(16)
Worked also for IntelliJ Ultimate 2020.2Eastbound
THIS should be the accepted answer, thank you! This will 100% fix the issue for everyone experiencing this issue in GoLand only. My Go Modules were set up perfectly, so suggesting to move from GOPATH to GOMODULES was of no help from comments aboveLacunar
Excelent. This solved my issue with Goland 2020.3Putman
thanks. i was getting desperate... File->InvalidateCaches solved this for me in Goland 2021.1Peplos
This is a life saver! Worked for me as well on Goland 2019.3.2Chairman
I tried to update the Go to the lastest and change de SDK version on the IDE but THIS answer is the right one! This must be marked as accepted!Shizue
This worked for me earlier but doesn't anymore :/ Not sure why.Mcquiston
Worked for IntelliJ Ultimate GoLand 2021.3.3 as well. Indeed a life saver. Was getting irritated why even restarting the goland didn't fix it. Thank you!Diction
I cleared the cache to try to fix another issue, then I started getting this error after clearing the caches (marked all the options to clear everything). Then cleared the cache again, this time only with the default options checked, and it solved.Slumberland
As this doesn't always work, try the below answer on removing the .idea folder.Bailey
I thought it went without saying that on any question, if one answer isn't acceptable, to consider the others.Royden
This did not work for me just now. Using latest as of today and selected all of the options.Mexico
Did not work for me. Answer https://mcmap.net/q/187506/-goland-jetbrains-shows-error-message-quot-unresolved-reference-quot-but-code-compiles-and-runs worked.Apical
2023 GoLand 2022.3.4. Fixed my issue.Anatolia
Works for goland 2021.2.5Hygrothermograph
OMG thankyou! I'm new to Golang and I kept thinking my code was wrong so I spend hours trying to debug the way I was importing etc.Hakim
M
38

I found best way to fix this issue.

  1. close your project and IDE
  2. go to your project directory
  3. remove ./.idea
  4. try to open it again

woops woops, fixed

Edit : better way try to use this one in Goland Menu
file -> repair IDE it will be refresh index module

Metz answered 20/1, 2022 at 17:23 Comment(3)
Thanks, can confirm this works for mac!Borecole
Thanks, can confirm this works for Windows 10Acentric
Thanks! Helped when I renamed project folder!Krafftebing
C
36

Today I faced that problem and I fixed it by enabling go module integration. For that Settings -> Other Settings -> Go Modules then enable go modules integration. This will work if you using go modules in your project.

Collocutor answered 15/1, 2021 at 9:33 Comment(2)
This solved it for me in GoLand. I used VSCode for the same project but did not have this problem and I am not actually sure why.Privateer
This is the best answer! Worked like charm in 2023.3.4Territorialism
B
15

I'm using go module and it's solved by:

  • Deselect Preferences->Go->GOPATH->Use GOPATH that's defined in system environment
  • File->Invalidate caches / Restart
Bumgardner answered 11/1, 2021 at 14:51 Comment(1)
The problem occurs again if I generate a vendor directory by go mod vendor, and it will disappear after I remove the vendor directoryBumgardner
F
11

I'm a bit late to the answer lol but incase anyone is still running into this, all I did was delete the .idea file and reloaded the project on GoLand (by clicking File -> Open -> file location). Did the trick for me.

Forman answered 2/9, 2021 at 21:22 Comment(3)
Make sure that you create a backup of your .idea folder since it contains some local environment IDE settings (Go version, etc).Brabble
Thank you very much, I was about to abort and switch to vscode. It seems that this tip worked for me.Facelifting
This is what I had to do. None of the other steps (fix ide, delete cache, reinstall) worked.Patsis
S
8

I just removed the project from Goland and re-create it from existing files. It was weird but it worked.

Sarnoff answered 25/2, 2021 at 3:37 Comment(0)
T
6

I cannot reproduce the issue in GoLand 2020.2. I suggest upgrading to it.

If that doesn't fix the issue then you can take the following steps to investigate the issue:

  • Is your project using Go modules or the traditional GOPATH?
  • If it's using GOPATH, have you enabled indexing of GOPATH under Settings/Preferences | Go | GOPATH?
  • If it's using Go modules, check to see that the support is enabled under Settings/Preferences | Go | Go Modules and then use Alt+Enter | Sync packages of‍‍ <project>
Trapper answered 6/1, 2020 at 16:29 Comment(5)
I can still reproduce it in 2020.2Dunseath
How? More details would help us understand your problem and replicate it to either provide a solution or fix any problems we may have on our side.Trapper
@disniper same problem. My project is in go/src/project/test.go and I keep getting this error for everything in the github.com/thedevsaddam/govalidator packageWorldbeater
@AminShojaei is your project using Go modules or the traditional GOPATH? If it's using GOPATH, have you enabled indexing of GOPATH under Settings/Preferences | Go | GOPATH? If not, is Go modules support enabled under Settings/Preferences | Go | Go Modules and then use Alt+Enter | Sync packages of <project>. If neither of these solve the problem, then please open an issue on our tracker at youtrack.jetbrains.com/issues/GoTrapper
@disniper That's right. Git module solved my problem. thanksWorldbeater
G
5

For Mac users, i solved this by setting Custom Tags to 'unix'. In Preferences > Go > Build Tags & Vendoring. In the Custom Tags, input unix.

enter image description here

I'm using GoLand 2022.1.2

Greatly answered 7/2, 2023 at 8:18 Comment(1)
Same problem here on Linux (Ubuntu). The unix build tag was added recently, so older versions of GoLand don't know how to handle it properly.Justinjustina
F
4

For me the issue was the version of Golang, I had been using go1.19 which threw unreferenced errors with .Close methods, switching back to an older version go16.15 helped me resolve this issue.

Farver answered 12/10, 2022 at 9:33 Comment(2)
Same issue with me, I have to switch to go1.18.7 and solved this problem.Dumyat
Refer to this answer, jetbrains-Unresolved-reference-Close-for-os.File, add unix tag to File | Settings | Go | Build Tags & Vendoring | Custom tags in 2022.1.4 version. And it worked for me.Cur
S
4

Today face the same problem using GoLand 2021.3 MacOS.

Fixed it with go to File Menu -> Repaire IDE...

Then it will automatic fixing issues

Sclerous answered 20/2, 2023 at 3:4 Comment(0)
B
2

I had a similar issue for gin.WrapH function utils.go. Tried the Override File Type option for utils.go in local module path which changed the file as a Go file but had a little cross sign where a tooltip marked that the file is excluded from compilation. The error Unresolved reference only went away when I selected the file, navigated to File -> File Properties -> Associate with File Type -> Register new file type association, and chose Go files

Bedpost answered 15/3, 2022 at 13:4 Comment(0)
S
2

I encountered the same problem and struggled to find a solution online. Most of the suggested fixes didn't work for me. After some experimentation, I compiled a comprehensive list of steps that successfully resolved the issue.

Step 1: Invalidate Cache

Go to File > Invalidate Caches / Restart.
Check all the checkboxes in the pop-up window.
If the issue persists, go to File > Repair IDE.
Select the second option in the pop-up at the bottom right.
    This will reopen, reindex, and invalidate the project.

Step 2: Check Go Path

Open settings with Ctrl + Alt + S.
Ensure a proper Go Path is set (delete pkg/ folder in .idea if it exists).
    Example: In Linux, use ~/go; in Windows, use /users/{YOUR USERNAME}/go.
Verify Go Modules integration:
    Go to Language & Frameworks > Go > Go Modules.
    Enable Go module integrations and support automatic vendor.

These steps successfully addressed the "unresolved" interfaces/types issue in my Go project opened in inteliJ. If you've faced similar challenges, give these steps a try for a comprehensive resolution.

Saffier answered 30/1 at 12:3 Comment(0)
M
1

In Goland preferences, if you're using the Global GOPATH, check the option "Index entire GOPATH" and hit apply.

Manrope answered 6/4, 2021 at 22:26 Comment(1)
like magic, I had the problem, tried the removing cache, then delete .idea folder, finally found this one, should be the final answer.Giefer
L
1

I had the same issue, I did try invalidates cache, but that did not work.

But the thing worked is just add a below line in your idea.properties file. And then restart IDE.

# custom GoLand properties (expand/override 'bin/idea.properties')
idea.max.intellisense.filesize=100000

This is because, Goland does not index huge packages. But you can force it to do that.

Lapierre answered 26/7, 2022 at 12:50 Comment(0)
S
1

If your code compiles and runs, you only need to check "Enable Go modules integration" flag ( IDE ultimate version): enter image description here

Scheers answered 27/6, 2023 at 9:58 Comment(0)
T
1

I found the same issue after i upgrade my goland to 2023.2.4 from 2022.3 on macOS x86, it appear from time to time. Later I found out that the problem was after I formatted the code using CMD + OPTION + L.

After i go to File -> Invalidate Caches / Restart, the issue was resolved. I hope it will not appear again :).

Transhumance answered 21/11, 2023 at 0:40 Comment(0)
W
1

File -> Repair IDE and then click next on each step until "Reopen Project" did the trick for me.

Withrow answered 7/12, 2023 at 23:42 Comment(0)
C
0

I had the same problem and it got fix weirdly.So I installed and opened project in vscode in order to continue coding.It started installing a extension called gopls. After installation completed I returned to GoLand to close project, but I waited for indexing to complete.Suddenly references were green !

Cryptomeria answered 5/3, 2020 at 10:21 Comment(1)
Opening it VSCode must've cleared the GoLand caching somehow, and re-indexed your code. File > Invalidate Cache/Restart... is the better way to do itLacunar
A
0

No option from other comments helped me. I had to switch GO SDK version in Setting -> Go -> GOROOT. Goland automatically downloaded 1.16 beta 1 version and it worked.

Adhern answered 19/1, 2021 at 20:27 Comment(0)
S
0

Goland version 2020.1: I opened a folder with subfolders of golang projects and goland didn't recognize dependencies. I solved this problem setting Project GOPATH

  1. ctrl + alt + s
  2. Go > GOPATH
  3. Click on plus button + In Project GOPATH
  4. Add your golang's project folder, example: ~/projects/my-golang-projects
Supervise answered 4/3, 2021 at 12:35 Comment(0)
H
0

I faced the same issue when I do bazel run //:gazelle, I fixed this issue by doing bazel sync (Bazel -> Sync -> Sync Project with BUILD Files). But you should have the below settings. GOPATH Setup Go Module Setup

(This fix is for Goland IDE, Mac. Of course we should add GOPATH in .bash_profile or .zshrc)

Hebron answered 15/9, 2021 at 18:10 Comment(0)
A
0

I had the same issue with aws go sdk, changing the Custom Properties (Help -> Edit Custom Properties) helped me. here is the reference to the JetBrains thread https://youtrack.jetbrains.com/issue/GO-5029

idea.max.intellisense.filesize=10000
idea.max.content.load.filesize=20000

Restart the IDE to let the changes take effect.

Adenoma answered 14/10, 2022 at 12:22 Comment(0)
D
0

I had been using go1.19 which threw unreferenced errors with .Close methods. I download the GoLand 2022.2.4 package and install, issue has been solved.

Dumyat answered 26/10, 2022 at 13:42 Comment(0)
C
0

In my case, I fixed it by running go mod tidy in the console.

Counterproof answered 28/10, 2022 at 2:39 Comment(0)
B
0

In my case go mod was 1.18 and IDE indexed my project with 1.19. At Preferences -> GOROOT I selected Go 1.18 and problem solved.

Barometry answered 6/12, 2022 at 18:33 Comment(0)
S
0

If this issue is for tests, be sure that all file in the same package includes the suffix _test, for example:

 - userTest
   |_ user_test.go
   |_ helper_test.go
Smooth answered 22/9, 2023 at 18:1 Comment(0)
M
0

I had the same problem in Goland2023.2 on MacOS x86 darwin.

File->Invalidate caches / Restart. DON'T WORK FOR ME

NOTE: For other versions of GoLAnd use yours version number. This is the solution:

  1. Exit GoLand

  2. Go to the Cache files in Mac: /usr/<username>/library/caches/

  3. Find GoLand2023.2 folder and Delete this folder

  4. Open GoLand Wait for "Indexing Files...".

  5. After this sould work fine.

I hope this solution help's you.

Melodious answered 9/11, 2023 at 10:11 Comment(0)
N
-5

I solved it by reinstall Go to D:\go, then reset Go sdk.

Narcosis answered 17/5, 2021 at 1:37 Comment(1)
This should never be the option for Professional products, MS has made us weak.Giefer

© 2022 - 2024 — McMap. All rights reserved.