Re: trying to run a Go server on Heroku, fails to compile with the https://github.com/kr/heroku-buildpack-go buildpack

2,706 views
Skip to first unread message

nvcnvn

unread,
Apr 24, 2013, 8:09:47 AM4/24/13
to golan...@googlegroups.com
If you are on the Linux 64bit. Maybe you can try Heroku null buildpack https://github.com/ryandotsmith/null-buildpack
Complie and then upload...done.

Vào 02:48:47 UTC+7 Thứ tư, ngày 24 tháng tư năm 2013, Octavian Costache đã viết:
Hi everybody,

I'm new to this list so I hope I'm not going to post something that doesn't belong here. :-)

A little bit of context
I'm trying to run a small Go Web server on Heroku. When I try to import anything a module outside of my web server package, it fails to compile on heroku.

Basically my directory structure is:

/src/
/src/okvivi/gofe/
/src/okvivi/models/

In src/okvivi/gofe/gofe.go I have a statement to import "okvivi/models".

My problem
The code compiles fine locally, but when I try to push it to heroku, I get the following error:
  
$ git push heroku master
Counting objects: 14, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (14/14), 1.25 KiB, done.
Total 14 (delta 0), reused 3 (delta 0)

-----> Fetching custom git buildpack... done
-----> Go app detected
-----> Installing Go 1.0.3... done
       Installing Virtualenv... done
       Installing Mercurial... done
       Installing Bazaar... done
-----> Running: go get -tags heroku ./...
package okvivi/models: unrecognized import path "okvivi/models"
 !     Heroku push rejected, failed to compile Go app

To g...@heroku.com:gofe-example.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'g...@heroku.com:gofe-example.git'

I've set this up as a simple skeleton to see the error and I've put it here on github https://github.com/okvivi/go-server-on-heroku.

I've added the error and the steps to reproduce in the README.
Does anybody know if I'm doing something wrong, or whether I should start debugging the buildpack?

Maybe somebody that's set up Go on Heroku successfully can see if I'm doing anything trivially wrong?

Thanks,
Octavian. 

mattn

unread,
Apr 24, 2013, 8:14:02 AM4/24/13
to golan...@googlegroups.com
You shouldn't make structure like GOPATH. you must put web.go into the root of repository. So:

/src/
/src/okvivi/gofe/
/src/okvivi/models/

Should be

/okvivi/gofe/
/okvivi/models/

Octavian Costache

unread,
Apr 24, 2013, 12:22:02 PM4/24/13
to golan...@googlegroups.com
Hey,

I figured out what was happening, so I figured I'd post here for whomever might have this problem again.

After looking closer at what the buildpack does, turns out that .godir is actually the root of the /src directory in your GOPATH. For more complicated source trees, seems like you need to consider that as the root of your repository.

So if the directory structure is like this:
  /okvivi/gofe
  /okvivi/models
then there should be an empty .godir file in the root, signifying that ./* is actually all the code that needs to happen for this build.

I've created an example here https://github.com/okvivi/go-server-on-heroku, that has the source tree in a subdirectory, and uses "git subtree" to push that subdirectory to heroku. 

I've also submitted a pull request to the buildpack to display more debug information on pushing, stuff that I found very useful while debugging this and that would have saved me a few hours of trying to figure this thing out.

Cheers,
Octavian.

Keith Rarick

unread,
Apr 24, 2013, 6:19:24 PM4/24/13
to Octavian Costache, golang-nuts
Hi Octavian, did you try running through the instructions linked
in the buildpack's readme? It shows what to put in .godir and
how the source code in the repository relates to the rest of your
Go workspace:

http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html

I also just added a second package to the example app at
https://github.com/kr/go-heroku-example, so it's more clear that
you can put more than one package in a single repo.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Octavian Costache

unread,
Apr 24, 2013, 6:46:51 PM4/24/13
to Keith Rarick, golang-nuts
I have read both the readme and the blog post quite carefully.

Until I read the /bin/compile source code I didn't find out what I really wanted to know: that .godir is actually the location of all the source code that will be copied into $GOPATH/src/ when the app will compile. 

The impression you get from http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku.html is that .godir is the place where the actual .go source files are, even when they are in a package (which works if you organize all your code into one single package, but breaks down when you need to organize code into multiple packages).

See example here https://github.com/okvivi/go-server-on-heroku/tree/master/src - there are two packages, and the binary is only under one, so .godir has to be empty to get everything under /okvivi/* to be copied in the right place.

--
Octavian Costache · @okvivi

Keith Rarick

unread,
Apr 24, 2013, 7:52:42 PM4/24/13
to Octavian Costache, golang-nuts
I strongly recommend against making an empty .godir.
It might get the job done right now, but the buildpack isn't
designed to work that way and an empty .godir might very
well break in the future.

You can have as many packages as you like if you follow
the example in https://github.com/kr/go-heroku-example.

If the introductory blog post is misleading, I'll try to come
up with something better. (There's an older introduction at
https://gist.github.com/kr/299535bbf56bf3016cba. Would
you say its description of .godir is any better? Are there
elements of that guide that we should consider bringing
back to life?)

Octavian Costache

unread,
Apr 24, 2013, 9:54:05 PM4/24/13
to Keith Rarick, golang-nuts
I feel like we're having communication problems where I am talking about packages, and you're talking about binaries.

The example at https://github.com/kr/go-heroku-example is still misleading. 

First, it has two binaries which compile fine, but only because all the code necessary for each binary is in one place only, and there's no code split into multiple packages that each of those binaries need.

Second, the moment you try to split that code into packages it doesn't compile locally anymore because the code is not organized according to Go standards of having to have the code under a GOPATH/src directory where the Go compiler knows how to look for libraries and packages.

Third, code organization in the go-heroku-example repository there has no concept of packages all together - web.go is in the root of the repository - which is not reflective of real world development where once you have a reasonable amount of code you start needing to split it across packages so you can share code among binaries.


I've tried to replicate your example to better make my point of where I see the problem. 

I've put it all here https://github.com/okvivi/go-server-on-heroku and I'm going to try to explain the directory structure there. If you clone it and try to push it to heroku to build you'll see that it fails.

My company has two binaries and shared code among them.

    foobar.com/frontend/  <-- there's a binary in here, the web one
    foobar.com/shared/    <-- NO BINARY in here, just a package with shared code
    foobar.com/worker/    <-- there's a second binary in here

This directory structure no longer compiles, because the moment I specify a ".godir" that's anything other than the root of my directory structure, the go compiler can no longer find the code under foobar.com/shared anymore.

I hope these steps to reproduce make more sense.

Octavian Costache

unread,
Apr 24, 2013, 10:39:18 PM4/24/13
to Keith Rarick, golang-nuts
I forgot to add a reference to the document about organizing Go code after which I am guiding my code organization, here http://golang.org/doc/code.html

Unless I'm missing something it feels to me like currently the heroku buildpack assumptions about code being in the root of the .git repository go against GoLang code organization, import paths and package names conventions.

Keith Rarick

unread,
Apr 25, 2013, 10:46:13 PM4/25/13
to Octavian Costache, golang-nuts
On Wed, Apr 24, 2013 at 6:54 PM, Octavian Costache
<octavian...@gmail.com> wrote:
> I feel like we're having communication problems where I am talking about
> packages, and you're talking about binaries.

I've added an example package that's not a command, so you can
see how it works. Sorry for not doing that before.

It's defined in
https://github.com/kr/go-heroku-example/tree/master/message
and used in
https://github.com/kr/go-heroku-example/blob/master/worker/main.go#L5

Octavian Costache

unread,
Apr 26, 2013, 10:31:38 AM4/26/13
to Keith Rarick, golang-nuts
Hi Keith,

I think I now finally understand the conventions that the build pack assumes. 
The blog post and the readme are definitely confusing.

Maybe you could add this in the README:

The directory structure this buildpack assumes looks something like this:

           +------ .godir is this ---------+      
           |                               |
           v                               v
$GOPATH/src/github.com/kr/go-heroku-example/.git   <-- git repository root is here
                                           |
                                           +binary.go
                                           |
                                           +library/code.go
                                           |
                                           +worker/other_binary.go



Octavian Costache

unread,
Jun 4, 2013, 7:31:55 PM6/4/13
to adamo...@gmail.com, golang-nuts, Keith Rarick
Happy this helped Adam! 
I will merge your pull request so that other people could use it as inspiration next time they stumble upon this thread. :-)

The solution I ended up for ourselves was to use "git subtree" which is a way to push a subdirectory of our repository to heroku as if it would be it's own git repo.

That way, the file structure we ended up with is something like this:

              +- .git root here
              |
              v
/work/git-repo/go/src/foobar/server/binary.go
^                ^   |      |
|                |   |      +worker/worker_binary.go
+----$GOPATH-----+   |      |
                     |      +shared/code.go
                     |
                     +third_party.com/library/code/...
                     |
                     +.godir  <- empty file here.

and then do something like this:

$ git subtree push --prefix go/src heroku master

I use this kind of strange structure because our repository is home to all binaries and code that we have, all in one place.

Sorry Keith for using your buildpack in unorthodox ways. :-)

Hope this helps (you and other future devs).
Octavian.



On Tue, Jun 4, 2013 at 4:31 PM, <adamo...@gmail.com> wrote:
           +------ .godir is this ---------+      

Thanks for this post. That was the moment that clicked for me.

I'll also admit I had a hard time understanding the buildpack convention, particularly because it doesn't quite follow a standard go workspace as defined here:

I created a standard go workspace and symlinked the second-level packages such that the go buildpack can find them. I also created a pull request to merge into your project: https://github.com/okvivi/go-server-on-heroku/pull/1

Hope that helps,
Adam

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/6tyCz7Tc8Ow/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

crme...@gmail.com

unread,
Dec 9, 2014, 5:27:24 PM12/9/14
to golan...@googlegroups.com
I'm guessing this thread is out of date and there is no need to use .godir any more? There's no reference to it in the post at https://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku, and there is no such file in the repo at https://github.com/kr/go-heroku-example. Is that correct?

I'm trying to follow the mentioned tutorial without success (as described at https://stackoverflow.com/questions/27389271/run-go-app-on-heroku), and am trying to figure out which piece might be missing.

Thanks
Reply all
Reply to author
Forward
0 new messages