Go App On Heroku With Local Packages - heroku

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/

Related

Why can't Heroku build my app with dependencies?

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)

Errors running the first Go project on the local machine

I've installed the latest version of Go on my local machine, downloaded the source code from https://github.com/rrrkren/topshot-sales, and placed the project code in my GOPATH.
When I run it go run go/main.go in my command prompt, I get these errors
go\main.go:8:2: no required module provides package github.com/onflow/flow-go-sdk/client: go.mod file not found in current directory or any parent directory; see 'go help modules'
go\main.go:6:2: no required module provides package github.com/rrrkren/topshot-sales/topshot: go.mod file not found in current directory or any parent directory; see 'go help modules'
go\main.go:9:2: no required module provides package google.golang.org/grpc: go.mod file not found in current directory or any parent directory; see 'go help modules'
Even though the go.mod file is located in the current directory. I would like to be able to download this project and keep it on my local machine, so I can edit the source code whenever I want. How can I do that?
This is what worked for me.
Step 1. Delete any previous Go version if you are doubtful about having it configured properly.
Step 2. Install new Go version. Download the binary release (Go) from here Go Binary Download
Note: In order to delete and install the new Go version you can use these steps - Go Deletion and Installation, they worked for me. Also the version I am currently using is 1.16.3.
Step 3. After completing step 1 and step 2, run the following command in a terminal:
go env -w GO111MODULE=auto
This should do it.
You should not need a GOPATH environment variable with Go 1.16.
Only:
GO111MODULE=on (won't be needed in Go 1.17 or 1.18)
GOPROXY=https://proxy.golang.org,direct
GOROOT=C:\path\to\go
(GOROOT unless you have installed Go in its default folder: %USERPROFILE%\go)
I tried:
D:\git> git clone https://github.com/rrrkren/topshot-sales
Cloning into 'topshot-sales'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 25 (delta 9), reused 21 (delta 6), pack-reused 0
Receiving objects: 100% (25/25), 16.95 KiB | 5.65 MiB/s, done.
Resolving deltas: 100% (9/9), done.
D:\git> cd topshot-sales
D:\git\topshot-sales> go run main.go
go: downloading github.com/onflow/flow-go-sdk v0.10.0
...
go: downloading gopkg.in/yaml.v2 v2.2.5
panic: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing dial tcp 35.193.214.129:9000: i/o timeout"
goroutine 1 [running]:
main.handleErr(...)
D:/git/topshot-sales/main.go:14
main.main()
D:/git/topshot-sales/main.go:23 +0x805
exit status 2
No error about go.mod, only a runtime execution error.
If you're on macOS, run the following command first and then retry:
xcode-select --install
Reference: Why am I getting an “invalid active developer path” when attempting to use Git after upgrading to macOS Monterey?

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.

Run Go app on Heroku

I'm trying to follow the tutorial at https://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku. Initially everything seems to work:
ceiroa-ltm:my_heroku_app ceiroa$ heroku create -b https://github.com/kr/heroku-buildpack-go.git
Creating glacial-badlands-6720... done, stack is cedar-14
BUILDPACK_URL=https://github.com/kr/heroku-buildpack-go.git
https://glacial-badlands-6720.herokuapp.com/ | https://git.heroku.com/glacial-badlands-6720.git
Git remote heroku added
ceiroa-ltm:my_heroku_app ceiroa$ git push heroku master
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (10/10), 925 bytes | 0 bytes/s, done.
Total 10 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Fetching custom git buildpack... done
remote: -----> Go app detected
remote: -----> Installing go1.3.3... done
remote: -----> Running: godep go install -tags heroku ./...
remote: -----> Discovering process types
remote:
remote: -----> Compressing... done, 1.5MB
remote: -----> Launching... done, v4
remote: https://glacial-badlands-6720.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/glacial-badlands-6720.git
* [new branch] master -> master
However, no dynos are spinned:
ceiroa-ltm:my_heroku_app ceiroa$ heroku ps
ceiroa-ltm:my_heroku_app ceiroa$
And the app gives an error:
What am I doing wrong?
I tried a bunch of things that didn't work:
downgrade go version (to match the one defined in https://github.com/kr/go-heroku-example/blob/master/Godeps/Godeps.json)
move from default Eclipse Go structure to one without src, bin, or pkg inside project
use hyphens instead of underscores (for project name and in Procfile and Godeps.json)
define full identifier in Godeps.json
At the end and started from scratch again, creating the project from the command line instead of from Eclipse, and this worked.
From the things above, I can rule out #1 and #4 as fixes, as I'm back to using the same config I initially had.
Another thing that was throwing me off was references to ".godir" in different websites and posts. I don't know what this file used to be used for, but it doesn't seem necessary any more.
I'm going to point to Eclipse and its Go plugin as the culprits, for now.
Heroku now officially supports Go and has a Getting Started guide.

heroku push rejected, failed to compile Python app

I have created a python application using "Flask" a python framework. I used the following documentation https://devcenter.heroku.com/articles/python
When I run this:
git push heroku master
I'm getting the following error after pushing to heroku.
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (8/8), 1.62 KiB, done.
Total 8 (delta 0), reused 0 (delta 0)
-----> Python app detected
-----> No runtime.txt provided; assuming python-2.7.4.
-----> Preparing Python runtime (python-2.7.4)
-----> Installing Distribute (0.6.36)
-----> Installing Pip (1.3.1)
-----> Installing dependencies using Pip (1.3.1)
Downloading/unpacking BeautifulSoup==3.2.1 (from -r requirements.txt (line 1))
Downloading BeautifulSoup-3.2.1.tar.gz
Running setup.py egg_info for package BeautifulSoup
Downloading/unpacking CDApplet==1.0 (from -r requirements.txt (line 2))
Could not find any downloads that satisfy the requirement CDApplet==1.0 (from -r requirements.txt (line 2))
No distributions at all found for CDApplet==1.0 (from -r requirements.txt (line 2))
Storing complete log in /app/.pip/pip.log
! Push rejected, failed to compile Python app
To git#heroku.com:frozen-brushlands-5131.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com:frozen-brushlands-5131.git'
Any help is appreciated.
Looks like there is not a Python package called CDApplet. When your Heroku app tries to install CDApplet it fails and gives you this error.
I tried it locally and could not find a Python package called CDApplet either.
I encountered the same error, try removing the concerned dependency (or add a correct name).
After that, do:
git add .
git commit -m "commit-message"
git push heroku master
You should be ready to go now.
i have same problem and fixed with
git add *
git commit -m "initial commit"
git push heroku master
i hope this'll help you.
I also ran into this problem, luckily, I found a fix. This are the steps I took.
Step 1
You need to upgrade the heroku app via command line interface
`$` heroku stack:set heroku-18 -a <app name>
Note that this will not affect previous apps that was built so you may need to rebuild the app. Remember to substitute "app name" with the name of the app
`$` git commit --allow-empty -m "Upgrading to heroku-18"
After the previous line, try pushing it again with:
`$` git push heroku master
Step 2
If this doesn't work remove the runtime.txt file containing the python version and remember to add and commit changes to git. Push once again and it should work, It worked for me!

Resources