Go Buildpack无法找到本地模块。我缺少了什么?

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

Go Buildpack unable to find a local module. What am I missing?

问题

我正在尝试在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 中提到:
> 如果应用程序源代码包含 go.mod 文件,构建包将使用 go modules 来获取依赖项。在构建阶段,go-mod-vendor 构建包(opens in a new tab) 会检查应用程序是否需要任何外部模块,如果需要,会运行 go mod vendor 命令。生成的 vendor 目录将存在于应用程序的根目录中,并包含构建所需的所有包。

查看构建日志,我看到确实运行了 go mod vendor。
> Paketo Buildpack for Go Distribution 2.2.3
> 解析 Go 版本
> 候选版本来源(按优先级排序):
> go.mod -> ">= 1.19"
> <unknown> -> ""
>
> 选择的 Go 版本(使用 go.mod):1.19.5
>
> 执行构建过程
> 安装 Go 1.19.5
> 完成于 35.526s
>
> 为 /layers/paketo-buildpacks_go-dist/go 生成 SBOM
> 完成于 0s
>
> Paketo Buildpack for Go Mod Vendor 1.0.7
> 检查模块图
> 运行 'go mod graph'
> 完成于 1.166s
>
> 执行构建过程
> 运行 'go mod vendor'
> 完成于 9.851s

>
> 为 /workspace/go.mod 生成 SBOM
> 完成于 21ms
>
> Paketo Buildpack for Go Build 2.0.8
> 执行构建过程
> 运行 'go build -o /layers/paketo-buildpacks_go-build/targets/bin -buildmode pie -trimpath .'
> 失败,耗时 611ms
> 执行 'go build' 失败:退出状态 1
> handlers.go:5:2: 找不到包“。”:
> /workspace/vendor/templates
> main.go:8:2: 找不到包“。”:
> /workspace/vendor/test
> 错误:构建失败,退出状态 1
> 错误:无法获取镜像或从源代码构建:执行生命周期失败,状态码为:51

handlers.go:5:2: &quot;templates&quot;
main.go:8:2: &quot;test&quot;

我所做的事情:

  1. 我将模块名称更改为 URL(例如 test -> some.com/test),并在 go.mod 中使用 replace 指令将其指向模块。
  2. 我运行了 go mod vendor,并查看生成的内容... some.com 是存在的!
  3. 我尝试在本地和使用 fly launch 远程构建。两者都无法找到文件。

我现在正在尝试一个私有仓库。

英文:

I am trying to build and launch a Go app on https://fly.io but when it comes to building it fails to find my test and templates package seen below:

    .
    ├── cmd
    │&#160;&#160; ├── doc
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── git
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── imp
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── log
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── met
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── orc
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── pub
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; ├── rep
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; └── main.go
    │&#160;&#160; └── web
    │&#160;&#160;     ├── fly.toml
    │&#160;&#160;     ├── go.gen
    │&#160;&#160;     ├── go.mod
    │&#160;&#160;     ├── go.sum
    │&#160;&#160;     ├── handlers.go
    │&#160;&#160;     ├── handlers_test.go
    │&#160;&#160;     ├── main.go
    │&#160;&#160;     ├── main_test.go
    │&#160;&#160;     ├── router.go
    │&#160;&#160;     └── router_test.go
    ├── CONTRIBUTING.md
    ├── go.mod
    ├── go.work
    ├── internal
    ├── LICENSE.txt
    ├── main.go
    ├── pctl
    ├── pkg
    │&#160;&#160; ├── **templates**
    │&#160;&#160; │&#160;&#160; ├── base.qtpl
    │&#160;&#160; │&#160;&#160; ├── base.qtpl.go
    │&#160;&#160; │&#160;&#160; ├── go.mod
    │&#160;&#160; │&#160;&#160; ├── go.sum
    │&#160;&#160; │&#160;&#160; ├── whoami.qtpl
    │&#160;&#160; │&#160;&#160; └── whoami.qtpl.go
    │&#160;&#160; └── **test**
    │&#160;&#160;     ├── go.mod
    │&#160;&#160;     └── test.go
    └── README.md

https://paketo.io/docs/reference/go-reference/#package-management-with-go-modules states:
> 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.

Looking at the build log, I see that go mod vendor was indeed run.
> Paketo Buildpack for Go Distribution 2.2.3
> Resolving Go version
> Candidate version sources (in priority order):
> go.mod -> ">= 1.19"
> <unknown> -> ""
>
> 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: &quot;templates&quot;
main.go:8:2: &quot;test&quot;

What I've done:

  1. I've changed the module names to a url (i.e. test -> some.com/test) and used a replace directive in go.mod to point to the modules.
  2. I've ran go mod vendor and seen what gets generated... some.com is there!
  3. I've tried building both locally and remotely with fly launch. Both result in no such file.

I am trying a private repo now.

答案1

得分: 0

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

# vim /etc/hosts
127.0.0.1    <module name>

如果我发现问题是由fly或Buildpack引起的,我会编辑这个答案。问题是当fly运行go mod vendor时,它不会遵循go.mod中的replace指令。

go mod vendor单独运行时会正确地复制所有内容,只要模块的名称是URL格式即可,它不一定是有效的URL。

另一个解决方案是将您的代码托管在有效的URL上,然后完成。可能有一个我忽略的标志,但我还没有找到。我希望任何遇到这个问题的人在此期间都能找到这个答案有帮助。

英文:

Not a great solution but one nonetheless is to edit your host file to have your module pointed to a local file server.

# vim /etc/hosts
127.0.0.1    &lt;module name&gt;

I'll edit this answer if I ever find out if the problem lies with fly or Buildpack. The problem being when fly runs go mod vendor it doesn't respect the replace directive in go.mod.

go mod vendor ran on it's own will copy everything correctly as long as the module's name is in url format. It doesn't have to be a valid url.

The other solution is to simply host your code at a valid url and be done with it. There might be a flag I'm missing but I have not found it yet. I'm hoping anyone that runs into this might find this answer helpful in the meantime.

huangapple
  • 本文由 发表于 2023年1月15日 11:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75122695.html
匿名

发表评论

匿名网友

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

确定