登录
首页 >  Golang >  Go问答

没法使用Go Buildpack找到本地模块,我遗漏了什么?

来源:stackoverflow

时间:2024-02-10 20:33:23 299浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《没法使用Go Buildpack找到本地模块,我遗漏了什么?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在尝试在 https://fly.io 上构建并启动 go 应用程序,但在构建时无法找到我的测试和模板包,如下所示:

.
    ├── cmd
    │   ├── doc
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── git
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── imp
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── log
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── met
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── orc
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── pub
    │   │   ├── go.mod
    │   │   └── main.go
    │   ├── rep
    │   │   ├── go.mod
    │   │   └── main.go
    │   └── web
    │       ├── fly.toml
    │       ├── go.gen
    │       ├── go.mod
    │       ├── go.sum
    │       ├── handlers.go
    │       ├── handlers_test.go
    │       ├── main.go
    │       ├── main_test.go
    │       ├── router.go
    │       └── router_test.go
    ├── contributing.md
    ├── go.mod
    ├── go.work
    ├── internal
    ├── license.txt
    ├── main.go
    ├── pctl
    ├── pkg
    │   ├── **templates**
    │   │   ├── base.qtpl
    │   │   ├── base.qtpl.go
    │   │   ├── go.mod
    │   │   ├── go.sum
    │   │   ├── whoami.qtpl
    │   │   └── whoami.qtpl.go
    │   └── **test**
    │       ├── go.mod
    │       └── test.go
    └── readme.md

https://paketo.io/docs/reference/go-reference/#package-management-with-go-modules 状态:

the buildpack will vendor dependencies using go modules if the app source code contains a go.mod file. during the build phase, the go-mod-vendor buildpack(opens in a new tab) checks to see if the application requires any external modules and if it does, runs the go mod vendor command for your app. the resulting vendor directory will exist in the app’s root directory and will contain all packages required for the build.

查看构建日志,我发现 go modvendor 确实已运行。

Paketo Buildpack for Go Distribution 2.2.3
Resolving Go version
Candidate version sources (in priority order):
go.mod    -> ">= 1.19"
 -> ""
Selected Go version (using go.mod): 1.19.5
Executing build process
Installing Go 1.19.5
Completed in 35.526s
Generating SBOM for /layers/paketo-buildpacks_go-dist/go
Completed in 0s
Paketo Buildpack for Go Mod Vendor 1.0.7
Checking module graph
Running 'go mod graph'
Completed in 1.166s
Executing build process
Running 'go mod vendor'
Completed in 9.851s
Generating SBOM for /workspace/go.mod
Completed in 21ms
Paketo Buildpack for Go Build 2.0.8
Executing build process
Running 'go build -o /layers/paketo-buildpacks_go-build/targets/bin -buildmode pie -trimpath .'
Failed after 611ms
failed to execute 'go build': exit status 1
handlers.go:5:2: cannot find package "." in:
/workspace/vendor/templates
main.go:8:2: cannot find package "." in:
/workspace/vendor/test
ERROR: failed to build: exit status 1
Error failed to fetch an image or build from source: executing lifecycle: failed with status code: 51

handlers.go:5:2: “模板” main.go:8:2: "测试"

我做了什么:

  1. 我已将模块名称更改为网址(即 test -> some.com/test),并在 go.mod 中使用替换指令来指向模块。
  2. 我运行了 go modvendor 并查看了生成的内容...some.com 就在那里!
  3. 我尝试过通过飞行发射进行本地和远程构建。两者都会导致没有这样的文件。

我现在正在尝试私人仓库。


正确答案


这不是一个很好的解决方案,但仍然可以编辑主机文件以使模块指向本地文件服务器。

# vim /etc/hosts
127.0.0.1    

如果我发现问题是出在 fly 还是 buildpack 上,我会编辑这个答案。问题是当 fly 运行 go modvendor 时,它不遵守 go.mod 中的替换指令。

只要模块名称采用 url 格式,自行运行的 go mod供应商就会正确复制所有内容。它不必是有效的网址。

另一个解决方案是简单地将代码托管在有效的 url 上并完成它。我可能缺少一面旗帜,但我还没有找到它。我希望遇到此问题的任何人都可能会发现此答案同时有所帮助。

理论要掌握,实操不能落!以上关于《没法使用Go Buildpack找到本地模块,我遗漏了什么?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>