猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

go.mod に書かれた Go のバージョンが Heroku に認識されない

f:id:yoshiki_utakata:20200901093957p:plain

現象と解決方法

通常 go.mod には

module xxxx

go 1.14

require (
    ...
)

のように、 go のバージョンが指定されているが、Herokuにデプロイするとこの go のバージョンと違ったものがデプロイされる。

-----> 
       Detected go modules via go.mod
-----> 
 !!    The go.mod file for this project does not specify a Go version
 !!    
 !!    Defaulting to go1.12.17
 !!    
 !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-modules#build-configuration
 !!    

調べてみると以下のような issue があり、

github.com

このように書き直したら go 1.14 でビルドされた

module review-ranking

// +heroku goVersion go1.14
go 1.14

require (
    ...
)

なぜ go 1.12 でビルドされるのか

https://devcenter.heroku.com/articles/go-support によると、執筆時に対応している Go のバージョンは https://github.com/heroku/heroku-buildpack-go/blob/97649d30c6e2a638036f09d2c4bac934f79f0c72/data.json#L3 の通りで、デフォルト(指定なし)だと go 1.12 が使われる。

補足 issue で話されていること

Defaults to v1.11.x version, despite "go 1.12" in go.mod file #301

-> go.mod で go 1.12 と書いているにもかかわらず、デフォルトの 1.11.x のバージョンが使われる。

msiebuhr commented on 8 Mar 2019

ビルドしたらこんなメッセージが表示されました。

 !!    The go.mod file for this project does not specify a Go version
 !!    
 !!    Defaulting to go1.11.5
 !!    
 !!    For more details see: https://devcenter.heroku.com/articles/go-apps-with-modules#build-configuration
 !!    

go.mod には go 1.12 と書いてあります。

go.mod にバージョンが書いてあるのに、また別の場所で go のバージョンを指定しなければならないのでしょうか。

freeformz commented on 9 Mar 2019

go.mod に go X.Y が書けるようになる前から、 Heroku は go.mod をサポートしています。そのため、現状、goのバージョンを指定するには以下のように書く必要があります。

// +heroku goVersion go1.12

jmorrell commented on 9 Mar 2019

近いうちにその記法もHerokuでサポートする予定です。((現在もまだサポートされていないっぽいが...)))

(以下略)