LoginSignup
4
1

More than 5 years have passed since last update.

Cloud FunctionsでGoをデプロイをする際のエラーメッセージが「go: finding ・・・」だった時の対処方法

Posted at

発生事象

事象

GCPのCloud FunctionsをCloud Shellから「gcloud functions deploy」コマンドでデプロイしようとしたところ、以下のメッセージが出てデプロイに失敗した。

$ gcloud functions deploy NotUsed --runtime go111 --trigger-http
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: go: finding cloud.google.com/go v0.35.1
go: finding github.com/golang/protobuf v1.2.0
go: finding github.com/BurntSushi/toml v0.3.1
go: finding github.com/google/martian v2.1.0+incompatible
・・・・(中略)・・・・・
go: finding github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e
go: finding github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1
go: finding github.com/google/go-querystring v1.0.0
go: finding go4.org v0.0.0-20180809161055-417644f6feb5
go: finding github.com/shurcooL/sanitized_anchor_name v0.0.0-20170918181015-86672fcb3f95
go: finding google.golang.org/api v0.0.0-20

日時

2019年2月7日

ソース

imported_and_not_used.go

package function

import (
        "net/http"
        "fmt"
        "cloud.google.com/go/firestore"
)

func NotUsed(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello, World!")
}
go.mod
module test

require cloud.google.com/go v0.35.1

発生原因

原因

インポートした「cloud.google.com/go/firestore」がソースの中で使用されておらずビルドに失敗するため。

対策

ソースの中でインポートしたライブラリ(ここではfirestoreを使用するようにする。もしくは不要なインポートを外す。

考察

エラーメッセージとして非常に解りづらくなっております。本質のエラーは「imported and not used」ですが、go: findingを出力してしまっているため、出力できるエラーメッセージ長の上限を超えてしまって本質のエラーが見えなくなってしまっているものと思われます。
go.modが無い場合はエラーメッセージとして「imported and not used」が出力されます。go.modがある場合のみ、エラーメッセージが解りづらくなっています。

事象発生と調査経緯

その1(事象発生前)

go.modが無い状態でデプロイしたところ、以下のようなエラーが発生しgo.modを作成

$ gcloud functions deploy NotUsed --runtime go111 --trigger-http
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: /tmp/sgb/gopath/src/serverlessapp/vendor/function/imported_and_not_used.go:7:2: cannot find package "cloud.google.com/go/firestore" in any of:
        /tmp/sgb/gopath/src/serverlessapp/vendor/cloud.google.com/go/firestore (vendor tree)
        /go/src/cloud.google.com/go/firestore (from $GOROOT)
        /tmp/sgb/gopath/src/cloud.google.com/go/firestore (from $GOPATH)

その2(事象発生)

gcloud functions deployしたところ始めに説明してある事象が発生

その3(go.modの調査)

cloud.google.com/goのバージョンを変更をためしてみたが事象は変わらず。

その4(原因の特定と解決)

エラーメッセージを見ると「message=Build failed」とあったので、ビルドを試してみることにしました。

$ go build imported_and_not_used.go
# command-line-arguments
./imported_and_not_used.go:7:2: imported and not used: "cloud.google.com/go/firestore"

あ、ごめんなさい。そうでした。

きちんと処理を実装して、未使用のインポートをなくしたらデプロイできました。

まとめ

原因はシンプルですが、本質のエラーにたどり着くまでに時間がかかってしまいました。
デプロイする前にgo buildするかmainを書いてテストするようにしましょう。

4
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1