5
votes

I am trying to deploy to google cloud using deploy command and my code refers local package using the github url. I getting below when deploying using gcloud deploy command. So in this example. My endpoints package refers to local package price using the full git url. What am I missing here?

package endpoints

import (
    "encoding/json"
    "fmt"
    "github.com/piscean/pricing/price"
    "net/http"
)

func LawnPricing(w http.ResponseWriter, r *http.Request) {

    m, err := price.Pricing()

    c, err := json.Marshal(m)

    w.Write(c)
    r.Body.Close()
}

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: /tmp/sgb/gopath/src/serverlessapp/vendor/endpoints/pricing.go:6:2: cannot find package "github.com/piscean/pricing/price" in any of: /tmp/sgb/gopath/src/serverlessapp/vendor/github.com/piscean/pricing/price (vendor tree) /go/src/github.com/piscean/pricing/price (from $GOROOT) /tmp/sgb/gopath/src/github.com/piscean/pricing/price (from $GOPATH) /tmp/sgb/gopath/src/serverlessapp/vendor/endpoints/zipcode.go:5:2: cannot find package "github.com/piscean/pricing/zip" in any of: /tmp/sgb/gopath/src/serverlessapp/vendor/github.com/piscean/pricing/zip (vendor tree) /go/src/github.com/piscean/pricing/zip (from $GOROOT) /tmp/sgb/gopath/src/github.com/piscean/pricing/zip (from $GOPATH)

2
Vendor (put into vendor folder) your dependencies. Tolls like dep and modules support that. - Dmitry Harnitski
As someone already answered, you need a dependency management tool like deb which will setup these dependencies for you. - suraj-kumar-adhikari
I am using VGO with GOMODULES111=ON. It does create Vendor folder. I see all dependencies in vendor folder but not for internal packages. - Paul Katich

2 Answers

2
votes

You should use the dependency package management tool for this called as dep.

Install dep by using the command:

go get -u github.com/golang/dep/cmd/dep

This would create the binary of dep in the GOBIN directory. Navigate to directory where main package is present and execute the command:

For Windows: %GOBIN%\dep.exe init

For Linux: $GOBIN\dep init

This would create Gopkg.toml and Gopkg.lock files along with the vendor folder that would solve your issue.

Reference: https://golang.github.io/dep/docs/introduction.html

1
votes

The cloud function is a managed environment which google provision for us during the deployment of the function. While setting the environment google supplies all the system dependencies but any external dependency should be handled by function itself.

While resolving external dependencies, it looks for Vendor directory , GOROOT and GOPATH to find to package being imported. If package is not found at any of these locations , you get this error.

Solution

  • Supply your package as vendor dependency by manually copying it there.
  • OR create a go module for your package and supply go.mod file while deploying the cloud function. This way go knows where to find the package before building the function.

Reference - https://github.com/GoogleCloudPlatform/golang-samples/issues/1600