$GOPATH/go.mod exists but should not in AWS Elastic Beanstalk
Asked Answered
T

4

10

I'm trying to deploy a golang app based on gin framework using bitbucket pipeline and AWS Elastic Beanstalk. I create my package application.zip with all the files needed to deploy the app inside an EB, when I push it, both by pipeline or by manual menu in AWS console I got these:

17:21:49 make.1 | go: finding github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:49 make.1 | go: downloading github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:49 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:50 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | + GOOS=linux
  17:21:50 make.1 | + GOARCH=amd64
  17:21:50 make.1 | + go build -o bin/application '-ldflags=-s -w'
  17:21:50 make.1 | go: cannot find main module; see 'go help modules'
  17:21:50 make.1 | exited with code 1
  17:21:50 system | sending SIGTERM to all processes

I see I forgot the go.mod file, indeed adding it then I got these message and everything works great:

  17:27:59 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:27:59 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | + GOOS=linux
  17:27:59 make.1 | + GOARCH=amd64
  17:27:59 make.1 | + go build -o bin/application '-ldflags=-s -w'
  17:27:59 make.1 | go: downloading golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:00 make.1 | go: extracting golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:00 make.1 | go: finding golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:01 make.1 | exited with code 0
  17:28:01 system | sending SIGTERM to all processes

But then while working I made some changes in my code, and I commit again, thinking that now everything is fine but I got these logs line:

  Executing: HOME=/tmp /opt/elasticbeanstalk/lib/ruby/bin/ruby /opt/elasticbeanstalk/lib/ruby/bin/foreman start --procfile /tmp/d20191202-5748-15rj5l8/eb-buildtask-0 --root /var/app/staging --env /var/elasticbeanstalk/staging/elasticbeanstalk.env
  17:32:00 make.1 | started with pid 5761
  17:32:00 make.1 | + go get github.com/gin-gonic/gin
  17:32:00 make.1 | $GOPATH/go.mod exists but should not
  17:32:00 make.1 | exited with code 1
  17:32:00 system | sending SIGTERM to all processes

And now I'm lost about $GOPATH/go.mod exists but should not I'm using last Go vesion 1.13.4, and AWS uses 1.13.2.

MORE INFO

I'll try to be more precise about the project, sorry if the logs is not enough. I have a simple application with gorm + gin, it's basically a REST API server. the structure of my project is like this:

/go/src/company.com/project

Inside I have these files and folders:

application.go
go.mod
Controller/dashboardController.go
Model/user.go
Service/utility.go
BuildFile
Procfile

Last two files are used to let the EB build the project once is uploaded in the S3. Of course locally everything works just fine, as well as the first time I upload the complete project with go.mod included, see the second bunch of logs. But when I amend a file, and I pack the application again and send it to EB, both via bitbucket piple or aws console, it doesn't matter, I got the error.

SOLUTION

After a lot of headhache I decide to log into ElasticBeanstalk via ssh, and I found that if I add to my build.sh script the line below than all the problems go away:

sudo rm /var/app/current/go.*

My build.sh file at the end is like this:

#!/usr/bin/env bash
# Stops the process if something fails
set -xe
touch /var/app/current/go.bak
sudo rm /var/app/current/go.*

# get all of the dependencies needed
go get "github.com/gin-gonic/gin"
go get "github.com/jinzhu/gorm"
go get "github.com/jinzhu/gorm/dialects/postgres"
go get "github.com/appleboy/gin-jwt"

# create the application binary that eb uses
GOOS=linux GOARCH=amd64 go build -o bin/application -ldflags="-s -w"
Tintinnabulation answered 2/12, 2019 at 17:42 Comment(2)
$GOPATH/go.mod should not exist, in fact there should be no files in the top level of GOPATH. What are you trying to do exactly? Please provide a minimal reproducible example, not just some of the log output.Driver
I had such message wnen I had RUN go mod download before WORKDIR /app in Dockerfile. Moving WORKDIR /app fixed that error.Dowdy
S
41

When GOPATH is set go get installs the requested module to the path provided in GOPATH but if you use .mod file, it uses the working directory.

Your case, you have both GOPATH set and have the .mod file.

You can unset GOPATH like:

unset GOPATH

It should solve your problem. Or, you can delete .mod file too.

Sable answered 28/5, 2020 at 10:31 Comment(0)
R
2

May be this can help you.

It looks like you have GOPATH=. and you are copying the module into that same place. The go get has the effect of creating ./src/golang.org/x/vgo, and then the COPY creates ./go.mod, so that it looks like the GOPATH's downloaded source lives inside the module. That in turn makes it look like src/golang.org/x/vgo is part of the project you are working on, and vgo gets very confused by its own (unsupported) use of vendoring.

Original post reference: here

Additional source : here

Rowdyism answered 17/4, 2020 at 10:47 Comment(1)
True, this also happens if the defined GOPATH is the same as the project folder and you have nor defined the bin and the src directoriesLamrert
V
2

You have to DELETE the go.mod file which is in the GOPATH directory location

Vocalize answered 25/8, 2021 at 10:8 Comment(0)
J
0

I had a similar problem and what I did was:

go env -u GOPATH

It should solve your problem if you had ever set the GOPATH variable like this go env -w GOPATH=some_path.

Japhetic answered 21/7, 2021 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.