Deploying go1.11 on App Engine Standard Build failure: Your app is not on your GOPATH

2.3k views Asked by At

I have attempted to deploy my Go app to App Engine. I have the following build errors:

Starting Step #1 - "builder"
Step #1 - "builder": Pulling image: gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": go111_1_11_2_20181111_RC00: Pulling from gae-runtimes/go111_app_builder
Step #1 - "builder": Digest: sha256:51fb36bfa16e7013356867c3a3972986084df93e56258fc258579a5799f0436e
Step #1 - "builder": Status: Downloaded newer image for gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00
Step #1 - "builder": 2018/11/24 18:13:29 Your app is not on your GOPATH, this build may fail.
Step #1 - "builder": 2018/11/24 18:13:29 Building from Go source in /tmp/staging477638319/srv, with main package at ./...
Step #1 - "builder": 2018/11/24 18:13:29 Building /tmp/staging477638319/srv, saving to /tmp/staging477638319/usr/local/bin/start
Step #1 - "builder": 2018/11/24 18:13:30 Wrote build output to /builder/outputs/output
Step #1 - "builder": 2018/11/24 18:13:30 Failed to build app: Your app is not on your GOPATH, please move it there and try again.
Step #1 - "builder": building app with command '[go build -o /tmp/staging477638319/usr/local/bin/start ./...]', env '[PATH=/go/bin:/usr/local/go/bin:/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=d253e517b16c HOME=/builder/home BUILDER_OUTPUT=/builder/outputs DEBIAN_FRONTEND=noninteractive GOROOT=/usr/local/go/ GOPATH=/go GOPATH=/tmp/staging477638319/srv/gopath]': err=exit status 1, out=srv/main.go:7:2: cannot find package "cloud.google.com/go/firestore" in any of:
Step #1 - "builder": /usr/local/go/src/cloud.google.com/go/firestore (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/cloud.google.com/go/firestore (from $GOPATH)
Step #1 - "builder": srv/main.go:8:2: cannot find package "github.com/gin-gonic/gin" in any of:
Step #1 - "builder": /usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/github.com/gin-gonic/gin (from $GOPATH)
Step #1 - "builder": srv/main.go:9:2: cannot find package "google.golang.org/api/option" in any of:
Step #1 - "builder": /usr/local/go/src/google.golang.org/api/option (from $GOROOT)
Step #1 - "builder": /tmp/staging477638319/srv/gopath/src/google.golang.org/api/option (from $GOPATH)
Finished Step #1 - "builder"
ERROR
ERROR: build step 1 "gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_RC00" failed: exit status 1

My app.yaml file looks like:

runtime: go111
handlers:
- url: /api/user
  script: auto

- url: /favicon.ico
  static_files: build/favicon.ico
  upload: build/favicon.ico

- url: /
  static_files: build/index.html
  upload: build/index.html

- url: /
  static_dir: build

My main.go file at the root looks like:

package main

import (
    "context"
    "net/http"

    "cloud.google.com/go/firestore"
    "github.com/gin-gonic/gin"
    "google.golang.org/api/option"
)

const firestoreAccountFile = "firebase.json"
const firestoreProjectID = "golang-gae-firestore-template"

type formData struct {
    Name  string `json:"name" binding:"required"`
    Email string `json:"email" binding:"required"`
}

func main() {
    // Gin init
    r := gin.Default()
    // Serve from static build directory
    r.StaticFS("/", http.Dir("./build"))
    // routes
    r.POST("/api/user", userHandler)
    // run application on port 8080
    r.Run(":8080")
}

func writeLogIfError(c *gin.Context, err error) {
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    }
}

func getNewFirestoreClient(ctx context.Context) (*firestore.Client, error) {
    return firestore.NewClient(ctx, firestoreProjectID, option.WithServiceAccountFile(firestoreAccountFile))
}

func userHandler(c *gin.Context) {
    ctx := context.Background()

    client, err := getNewFirestoreClient(ctx)
    writeLogIfError(c, err)
    defer client.Close()
    // Get form data
    var form formData
    c.BindJSON(&form)

    // [START add user to firestore]
    _, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
        "name":  form.Name,
        "email": form.Email,
    })
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }
    // [END add user to firestore]
    c.JSON(http.StatusOK, gin.H{"status": "user added to db"})
}

I cannot build successfully without getting these build errors. I've attempted to follow the documentation for Go on App Engine exactly and am confused if I am to structure my application any different than suggested. Any thoughts on how to resolve this GOPATH error?

2

There are 2 answers

2
daviddavis On

Update: I was unable to successfully deploy with GOPATH, and instead successfully was able to deploy with go.mod after I included the env variable: export GO111MODULE=on for the modules to work. Documentation here: https://github.com/golang/go/wiki/Modules.

0
Eugene On

The way it worked for me was to put my app into GOPATH/src/ folder - to reside there along third party dependencies.

https://cloud.google.com/appengine/docs/standard/go111/specifying-dependencies

says:

If your app is in a directory on your GOPATH environment variable, App Engine analyzes, copies, and uploads your imports from GOPATH

-which was confusing, i.e. I first tried to put my app into GOPATH folder and it didn't work.

So given your GOPATH folder is C:\go your project should be structured like that:

- C
  - go
     - pkg
     - src
         - cloud.google.com
         - github.com
         - google.golang.org
         - your-app