首頁 > 後端開發 > Golang > 主體

Go Buildpack 無法找到本機模組。我缺什麼?

WBOY
發布: 2024-02-13 11:21:07
轉載
530 人瀏覽過

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

php小編香蕉在使用Go Buildpack時遇到了一個問題:「無法找到本地模組,我缺少什麼?」。 Go Buildpack 是用於在Cloud Foundry平台上建立和運行Go應用程式的工具。這個問題通常是由於缺少Go依賴函式庫或配置不正確所導致的。解決這個問題的方法是檢查Go專案的依賴關係,並確保正確設定了GOPATH和GO111MODULE等環境變數。

問題內容

我正在嘗試在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    <module name>
登入後複製

如果我發現問題是出在 fly 還是 buildpack 上,我會編輯這個答案。問題是當 fly 運行 go modvendor 時,它不遵守 go.mod 中的替換指令。

只要模組名稱採用 url 格式,自行執行的 go mod供應商就會正確複製所有內容。它不必是有效的網址。

另一個解決方案是簡單地將程式碼託管在有效的 url 上並完成它。我可能缺少一面旗幟,但我還沒有找到它。我希望遇到此問題的任何人都可能會發現此答案同時有所幫助。

以上是Go Buildpack 無法找到本機模組。我缺什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!