Why can't Heroku build my app with dependencies? - go

I have been working on a Go project and deploying it successfully with Heroku up until I added more packages. My Heroku app is set up to deploy the main branch of my github repo, but it fails to build the app each time I push. This is the build log:
-----> Using buildpack: heroku/go
-----> Go app detected
-----> Fetching stdlib.sh.v8... done
----->
Detected go modules via go.mod
----->
Detected Module Name: my-heroku-app
----->
-----> New Go Version, clearing old cache
-----> Installing go1.17.3
-----> Fetching go1.17.3.linux-amd64.tar.gz... done
-----> Determining packages to install
Detected the following main packages to install:
my-heroku-app
-----> Running: go install -v -tags heroku -mod=vendor my-heroku-app
webapp.go:6:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/go-sql-driver/mysql
webapp.go:7:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/context
webapp.go:8:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/sessions
webapp.go:9:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/golang.org/x/crypto/bcrypt
! Push rejected, failed to compile Go app.
! Push failed
Here is what my import statement looks like
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
"html/template"
"log"
"net/http"
"time"
"unicode"
)
I have tried reading through the guides and documentation about govendor both at Heroku and on their github page, and as far as I can tell, I have done my setup correctly. I am currently using govendor and I have a vendor folder containing a json file in my repo that has the info for the dependencies. Everything works when I run it locally
update: I made a fresh repo with minimal code but as soon as I add a dependency I got the same error. I also tried deploying on Digital Ocean as well but I think they use the open source Heroku buildpack so I got the same error
update 2: I got a pared down version of the app working. The repo has no vendor directory or vendor json whatsoever despite using an external package (build log shows successful install of external package)

Related

Heroku, providing a deb dependency to Go project through APT buildpack does not work out

I have a PDF conversion package in my Go project which relies upon "wkhtmltox" debian package. I install "wkhtmltopdf" (similar to "wkhtmltox") through APT buildpack before running a Go buildpack. The "wkhtmltopdf" has been tested locally, and in this case Go project builds perfectly. But when I do it in Heroku, it does not work out, and fails with the same error as would pop out in case no "wkhtmltox" nor "wkhtmltopdf" was installed. The successfully installed "wkhtmltopdf" :
remote: -----> Installing wkhtmltopdf_0.12.5-1build1_amd64.deb
The error :
remote: ../codon/tmp/cache/go-path/pkg/mod/github.com/adrg/go-wkhtmltopdf#v0.2.2/converter.go:7:10: fatal error: wkhtmltox/pdf.h: No such file or directory
remote: 7 | #include <wkhtmltox/pdf.h>
remote: | ^~~~~~~~~~~~~~~~~
remote: compilation terminated.
Is there someone with similar experience ? Is it possible that for Go the installed package is simply not available without some additional manipulations ?

Why is this Go build failing on Heroku? It builds locally with no issue

Is anyone able to tell me why this build has failed on heroku?
I've tried finding substitute packages and tidying the mod file, even deleting it and redoing it.
here is my build log
Overview Resources Deploy Metrics Activity Access Settings
Activity Feed Build LogID f8e9d5ce-0d8f-4e8e-8b2a-661c4d1f68f7
-----> Building on the Heroku-20 stack
-----> Go app detected
-----> Fetching stdlib.sh.v8... done
----->
Detected go modules via go.mod
----->
Detected Module Name: github.com/xxxxxx/xxxxxx-xxxxxx
----->
!! 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
!!
-----> Using go1.12.17
-----> Determining packages to install
Detected the following main packages to install:
github.com/xxxxxx/xxxxxx-xxxxxx
-----> Running: go install -v -tags heroku github.com/xxxxxx/xxxxxx-companion
github.com/vektah/gqlparser/v2/ast
github.com/agnivade/levenshtein
github.com/mitchellh/mapstructure
github.com/vektah/gqlparser/v2/gqlerror
github.com/99designs/gqlgen/graphql
# github.com/99designs/gqlgen/graphql
../codon/tmp/cache/go-path/pkg/mod/github.com/99designs/gqlgen#v0.13.0/graphql/error.go:21:5: undefined: errors.As
github.com/99designs/gqlgen/graphql/errcode
github.com/vektah/gqlparser/v2/lexer
github.com/hashicorp/golang-lru/simplelru
github.com/vektah/gqlparser/v2/parser
github.com/hashicorp/golang-lru
github.com/gorilla/websocket
github.com/vektah/gqlparser/v2/validator
github.com/99designs/gqlgen/graphql/playground
github.com/xxxxxx/xxxxxx-xxxxxx/graph/model
github.com/xxxxxx/xxxxxx-xxxxxx/database
github.com/99designs/gqlgen/graphql/introspection
github.com/vektah/gqlparser/v2/validator/rules
github.com/go-sql-driver/mysql
github.com/vektah/gqlparser/v2
! Push rejected, failed to compile Go app.
! Push failed
Here is my go.mod file:
module github.com/xxxxxx/xxxxxx-xxxxxx
go 1.15
require (
github.com/99designs/gqlgen v0.13.0
github.com/go-sql-driver/mysql v1.5.0
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/mitchellh/mapstructure v0.0.0-20180220230111-00c29f56e238 // indirect
github.com/stretchr/testify v1.5.1 // indirect
github.com/vektah/gqlparser/v2 v2.1.0
gopkg.in/yaml.v2 v2.2.8 // indirect
)
I'm at a complete loss, I suspect it has to do with an incorrect version, but the build log doesn't give me much to go on. How can I diagnose this kind of issue in the future?
Thank you.
You could add a directive // +heroku goVersion go1.15 in your go.mod file.
module somemodule
// +heroku goVersion go1.15
go 1.15
require (
// ...
)
Then it should look like this.
remote: Detected go modules via go.mod
remote: ----->
remote: Detected Module Name: somemodule
remote: ----->
remote: -----> New Go Version, clearing old cache
remote: -----> Installing go1.15
Documentation: https://github.com/heroku/heroku-buildpack-go#go-module-specifics
Go Module Specifics
You can specify specific package spec(s) via the go.mod file's // +heroku install directive (see below).
// +heroku goVersion <version>: the major version of go you would
like Heroku to use when compiling your code. If not specified this
defaults to the buildpack's [DefaultVersion].
Coming back to mark this one complete, I figured out that by specifying in the go mod file
// +heroku goVersion go1.15
Go buildback supports arbitrary comments specifiying versioning before build.
https://github.com/heroku/heroku-buildpack-go#go-module-specifics
Thank you as well to user #JimB for pointing me in the right direction.

Endorsers Instantiate Chaincode with error "cannot find package"

I created a chaincode and I imported a package to it.
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"golang.org/x/crypto/bcrypt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer")
I can install that chaincode to all endorsers. But when I instantiated it to all endorsers, it faced the error:
endorser failed with error starting container: error starting container: Failed to generate platform-specific docker build: Error returned from build: 1 "/chaincode/input/src/github.com/marbles02/marbles_chaincode.go:85:2: cannot find package "golang.org/x/crypto/bcrypt" in any of:
/usr/local/go/src/golang.org/x/crypto/bcrypt (from $GOROOT)
/chaincode/input/src/golang.org/x/crypto/bcrypt (from $GOPATH)
/go/src/golang.org/x/crypto/bcrypt
I tried to copy golang.org/x/crypto/bcrypt package to the /usr/local/go/src/ in the root directory, but it has the same error.
Try Installing the dependencies in the chaincode directory using below commands:
go mod init will create go.mod and go.sum files.
go mod vendor for creating a vendor directory.
Also if using fabric:v2.X.X then shim and peer packages moved to different libraries.
so change the peer and shim packages to below in the chaincode.go file.
peer --> github.com/hyperledger/fabric-protos-go/peer.
shim --> github.com/hyperledger/fabric-chaincode-go/shim
Make sure to add peerand shim changes before running go mod initcommand OR if you are already have a vendor directory then try go mod tidy and then go mod vendor commands to update the packages.

Unwanted Glide imports

I am working on an App Engine app and am coming across this error running goapp serve:
Failed parsing input: parser: bad import "syscall" in vendor/golang.org/x/net/icmp/message.go
Which is apparently from Glide importing the entire golang.org/x/net package when I only wanted golang.org/x/net/context.
The import in my glide.yaml was:
- package: golang.org/x/net
subpackages:
- context
And the glide.lock file says
- name: golang.org/x/net
version: 07b51741c1d6423d4a6abab1c49940ec09cb1aaf
subpackages:
- context
How would I keep net/icmp out of my build in goapp serve?
I often had extra imports when using glide (a bit like in issue 101)
Just for testing, try re-populate your vendor folder using govendor
govendor list
govendor fetch +m
In my experience, the resulting list of dependencies is cleaner.

Go App On Heroku With Local Packages

I'm trying to put a Go app on Heroku using the Go Buildpack, which is fine when it's something basic, but as soon as I do a local package it does not compile. Here's an example setup:
Structure
+ship
+foo
foo.go
main.go
main.go
package main
import (
"os"
"fmt"
"net/http"
"ship/foo"
)
func main() {
foo.Bar()
port := os.Getenv("PORT")
http.HandleFunc("/", root)
http.ListenAndServe(":" + port, nil)
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Aloha, world!")
}
foo.go
package foo
func Bar() {}
Push
git push heroku master
Initializing repository, done.
Counting objects: 20, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (20/20), 1.53 MiB | 586.00 KiB/s, done.
Total 20 (delta 2), reused 0 (delta 0)
-----> Fetching custom git buildpack... done
-----> Go app detected
-----> Installing go1.3.1... done
-----> Running: godep go install -tags heroku ./...
main.go:7:3: cannot find package "ship/foo" in any of:
/app/tmp/cache/go1.3.1/go/src/pkg/ship/foo (from $GOROOT)
/tmp/build_4b92e51c-3959-4ddb-8eff-90d72da70729/.heroku/g/src/_/Users/Daryl/Go/src/ship/Godeps/_workspace/src/ship/foo (from $GOPATH)
/tmp/build_4b92e51c-3959-4ddb-8eff-90d72da70729/.heroku/g/src/ship/foo
godep: go exit status 1
! Push rejected, failed to compile Go app
Any idea what's going on here and how to go about it?
Just a note for anyone coming across this issue in go 1.6. Godep has changed to use a vendor folder with Heroku instead, so you will need to reset your Godeps to use vendor as per the docs here:
https://github.com/tools/godep#go-15-vendor-experiment
Heroku also has upgrade info here:
https://devcenter.heroku.com/articles/go-support#migrating-from-go1-5-godep-workspace-to-go1-6-with-a-vendor-directory
I have a solution that worked for me, though I don't like it, and hope its not the correct way to do it!
I'm using vendor. Both locally and TravisCI were able to build my app, but TravisCI could not deploy it to Heroku, as Heroku was having troubles finding local packages as well. What I ended up doing is fetching local packages with vendor:
govendor fetch +local
Once I committed again, TravisCI built and deployed to Heroku, and my app worked.
The reason I don't like this solution is that I now have duplicate code! My local subpackages can be found in /, as well as in vendor: /vendor/

Resources