Go: Cannot find package in any of Vendor Tree, $GOROOT or $GOPATH even though a private package exists in Vendor Tree

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

Go: Cannot find package in any of Vendor Tree, $GOROOT or $GOPATH even though a private package exists in Vendor Tree

问题

这是我的项目结构:

Go: Cannot find package in any of Vendor Tree, $GOROOT or $GOPATH even though a private package exists in Vendor Tree

这是我的模块文件:

module github.com/bloodcompany/blood-microservices/stores/sync-stores-google-sheet-to-firestore

go 1.16

require (
	cloud.google.com/go/firestore v1.6.1 // indirect
	cloud.google.com/go/kms v1.4.0 // indirect
	github.com/bloodcompany/blood-microservices/utils v1.0.0
)

replace (
	github.com/bloodcompany/blood-microservices/models => ../../models
	github.com/bloodcompany/blood-microservices/utils => ../../utils
)

这是我在 "function.go" 文件中的导入部分:

import (
	"github.com/bloodcompany/blood-microservices/utils"
)

问题是:
当我将函数部署到服务器时,在终端中出现以下错误。从项目结构中可以看出,在错误中似乎缺少的包已经在供应商树下可用。但错误仍然出现。可能出了什么问题?

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: src/stores/function.go:9:2: cannot find package "github.com/bloodcompany/blood-microservices/utils" in any of:
        /workspace/src/stores/vendor/github.com/bloodcompany/blood-microservices/utils (vendor tree)
        /layers/google.go.runtime/go/src/github.com/bloodcompany/blood-microservices/utils (from $GOROOT)
        /workspace/src/github.com/bloodcompany/blood-microservices/utils (from $GOPATH); Error ID: 2f5e35a0

注意:
我的项目不在 $GOPATH 的位置。这是一个问题吗?$GOPATH 的位置是 "C:\Users\thisa\go",而项目位于另一个位置。

英文:

Here's my project structure:

Go: Cannot find package in any of Vendor Tree, $GOROOT or $GOPATH even though a private package exists in Vendor Tree

Here's my mod file:

module github.com/bloodcompany/blood-microservices/stores/sync-stores-google-sheet-to-firestore

go 1.16

require (
	cloud.google.com/go/firestore v1.6.1 // indirect
	cloud.google.com/go/kms v1.4.0 // indirect
	github.com/bloodcompany/blood-microservices/utils v1.0.0
)

replace (
	github.com/bloodcompany/blood-microservices/models => ../../models
	github.com/bloodcompany/blood-microservices/utils => ../../utils
)

Here is my import in "function.go" file:

import (
	"github.com/bloodcompany/blood-microservices/utils"
)

The problem:
When I deploy my function to server, I get below error in the Terminal. You can see in my project structure that the package that seems missing in the error is already available under Vendor Tree. But the error still appears. What could have possibly gone wrong?

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: src/stores/function.go:9:2: cannot find package "github.com/bloodcompany/blood-microservices/utils" in any of:
        /workspace/src/stores/vendor/github.com/bloodcompany/blood-microservices/utils (vendor tree)
        /layers/google.go.runtime/go/src/github.com/bloodcompany/blood-microservices/utils (from $GOROOT)
        /workspace/src/github.com/bloodcompany/blood-microservices/utils (from $GOPATH); Error ID: 2f5e35a0

Note:
My project is not in the location of $GOPATH. Is this an issue? $GOPATH location is "C:\Users\thisa\go" while the project is on another location.

答案1

得分: 1

这里有两个可能的问题:

缺少 go.mod 文件

根据文档的说明,你的目标路径需要有一个 go.mod 文件。

> 如果箭头右侧的路径是绝对路径或相对路径(以 ./ 或 ../ 开头),它将被解释为替换模块根目录的本地文件路径,该路径必须包含一个 go.mod 文件。在这种情况下,替换的版本必须被省略。

文件不存在

看起来你引用了项目根目录之外的文件。这些文件是否被下载到部署的位置?如果已经下载,问题可能是上面提到的那个问题。

英文:

There are two possible issues here:

Lack of go.mod

According to the documentation, your destination needs to have a go.mod file.

> If the path on the right side of the arrow is an absolute or relative path (beginning with ./ or ../), it is interpreted as the local file path to the replacement module root directory, which must contain a go.mod file. The replacement version must be omitted in this case.

The files are not there

It looks like you're referencing files outside of your project root. Are those files being downloaded to wherever they are being deployed to? If they are downloaded, the problem is probably the one above.

huangapple
  • 本文由 发表于 2022年10月4日 19:00:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/73946882.html
匿名

发表评论

匿名网友

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

确定