Igor
Igor

Reputation: 21

Can't push simple golang project to heroku

My project "testheeroku7777" consist of two files. main.go:

package main

import "fmt"

func main() {

fmt.Println("Hello world!")

}

Procfile:

web: testheeroku7777

When I push:

testheeroku7777> git push heroku master

It gives an error:

Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 645.21 KiB | 5.92 MiB/s, done.
Total 8 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> App not compatible with buildpack: https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/go.tgz
remote:        More info:     https://devcenter.heroku.com/articles/buildpacks#detection-failure
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !       Push rejected to testheeroku7777.
remote: To https://git.heroku.com/testheeroku7777.git
! [remote rejected] master -> master (pre-receive hook declined) 
error: failed to push some refs to 'https://git.heroku.com/testheeroku7777.git'

I've read provided links but I have no idea about this error.

Upvotes: 2

Views: 1697

Answers (2)

amunnelly
amunnelly

Reputation: 1196

This issue has to do with Go’s new dependency management system. Heroku is looking for a dependency management system in your project, and can’t find one. That’s why it’s throwing an error. Even though yours is a simple project, Heroku must have a dependency-management system that it can recognise.

Using Go modules is the officially-sanctioned method of dependency-management. But – and it’s a big but – a surprising quirk of Go modules is that Go modules don’t work for projects that are within the GOPATH.

When Go started, the developers made a big thing of having all projects in one place, the GOPATH. All the documentation was explicit on this – this is where your go code has to stored, it won’t run from anywhere else. But now, if you’re writing code that needs Go modules – and any code that’s going on heroku needs Go modules – the directories for that code must be outside GOPATH. I don’t know why they don’t make that more explicit, but that seems to be the way it is.

So, to get your code working, what you need to do is:

  1. Move your project to somewhere outside GOPATH – the desktop, say, in the name of originality.

  2. Create a remote git repository with the same name as your current project.

  3. Then create a go module file by typing, at the terminal command prompt in your local directory, go mod init github.com/myGithubUserName/myProjectName

  4. Next, create a local git repository by typing git init in the terminal.

  5. Identify the remote repository you created earlier by typing git remote add origin http//www.github.com/myGithubUserName/myProjectName.git in the terminal.

  6. Write some excellent code.

  7. Type git add . && git commit -m “first commit” at the prompt to commit your code.

  8. Type git push -u origin master to update the remote repository.

After all that, and only after all that, are you ready to type git push heroku master. The only consolation is that all should run smoothly now.

There are earlier dependency-management systems as mentioned by other posters, like govendor and dep, but Go modules are the officially-sanctioned method and I guess we’re all as well to adapt to it now as later. The most popular tutorial about Go modules seems to be this one: https://roberto.selbach.ca/intro-to-go-modules/. A search will find a few others too.

Upvotes: 2

apg
apg

Reputation: 2649

Heroku doesn't have the Go runtime installed in the base image, and therefore requires that you utilize the go buildpack to build your app. However, this also requires that you use a vendoring tool such as dep, or govendor, which means this very basic app, by itself, unfortunately won't work.

Upvotes: 0

Related Questions