在Goland IDE中导入位于子文件夹中的模块。

huangapple go评论79阅读模式
英文:

Import module located in sub folder in Goland IDE

问题

如何让Goland正确地对我的go.mod进行lint,并且不报告我在主项目的子目录中维护的模块的"missing dependency"错误?

我试图遵循hashicorp的vault项目中的模式,其中我在一个子文件夹中维护一个api模块(可以被其他人导入,而不会引入主项目使用的所有依赖项)。就像在vault中一样,我在主项目的go.mod中使用URL引用api模块,并使用指向相对路径位置的replace进行覆盖。

module github.com/hashicorp/vault

go 1.16

replace github.com/hashicorp/vault/api => ./api

// ...
require (
	// ...
	github.com/hashicorp/vault/api v1.0.5-0.20210210214158-405eced08457
	// ...
)

从构建的角度来看,所有这些都按预期工作(以及源代码中的依赖项解析),但是Goland在我的主项目的go.mod中将导入语句和replace语句都标记为红色(错误为"missing dependency")。红色的灯泡建议我"Sync Dependencies",我已经尝试过,但是在收到"Finished Successfully"的消息后,错误仍然存在。

我正在使用Goland 2020.3.5和go 1.16.3。我的项目位于GOPATH之外,并且在Goland的设置菜单中启用了go模块。

再次强调,一切都可以正确构建,但是我的go.mod的linting表现得好像有一个缺少的依赖项错误。

英文:

How can I get Goland to lint my go.mod correctly and not report "missing dependency" errors for a module I am maintaining in a sub directory of my main project?

I'm trying to follow the pattern in hashicorp's vault project where I maintain an api module in a sub folder (that can be imported by others without dragging in all deps used by my main project.) Just like in vault, I reference the api module by url in my main project's go.mod and override it with a replace pointing to the relative path location.

module github.com/hashicorp/vault

go 1.16

replace github.com/hashicorp/vault/api => ./api

// ...
require (
	// ...
	github.com/hashicorp/vault/api v1.0.5-0.20210210214158-405eced08457
	// ...
)

All this works as expected from a build perspective (and dependency resolution within the source code), however Goland is highlighting both the import statement and the replace statement in my main project's go.mod in red (the error is "missing dependency"). The red lightbulb suggests I "Sync Dependencies" which i have tried, but after a "Finished Successfully" message, the error remains.

I am using Goland 2020.3.5 and go 1.16.3. My project is outside of my GOPATH and I have go modules enabled in the Goland settings menu.

Just to reiterate, everything builds correctly, however the linting for my go.mod acts like there is a missing dependency error.

答案1

得分: 2

升级到最新版本的Goland(2021.1.3)解决了这个问题。
感谢 @s0xzwasd!

英文:

Upgrading to the latest version of Goland (2021.1.3) solved the issue.
Thanks @s0xzwasd !

huangapple
  • 本文由 发表于 2021年6月24日 04:20:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68106590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定