Google Go AppEngine imports and conflicts when serving / testing

前端 未结 3 1110
清歌不尽
清歌不尽 2020-12-08 23:05

So I have spent the better part of two days trying to figure this one out and no matter what I do I can\'t get things straightened out. Here is what is going on:

    <
相关标签:
3条回答
  • 2020-12-08 23:25

    Appengine "conflicts with same file imported from GOPATH" issue:

    Appengine is importing things underneath the root directory (i.e. where the app.yaml is). This will cause two imports, one by appengine when it scans the directories, and a second by your source when it is explicitly imported.

    You have two choices:

    Don't use the full import path (for sub-folder packages) with appengine.

    • Remove the source repository part of import. So instead of "github.com/blah/blah" it would be "blah/blah".

      Note: This kinda sucks as it makes your build and software appengine specific. You could make this a little better -maybe- by using build constraints. e.g. +build !appengine or +build !appengine to include/remove certain files from the build depending on if you are targeting appengine.

    Move your modules/dependencies (sub-folders) to a separate and independent project to make it work with the full path import convention:

    1. Get rid of all directories / dependencies in the main project (where your app.yaml is), so that appengine can't scan and find them.
    2. Move them to another independent project (I did SampleIssueDeps) with no app.yaml that is not a sub-directory (e.g. /MarkHayden/SampleIssueDeps).
    3. Then pull those dependencies via full path import. e.g. github.com/MarkHayden/SampleIssueDeps/lib1.

    Summary: For sub-folder packages in an appengine project don't include the "source repository" part of the import path OR only use appengine to init() and move all of your other code to separate projects and use like external dependencies.

    0 讨论(0)
  • 2020-12-08 23:36

    I had a lot of trouble following various answers and understanding how to solve the problem.

    But after a lot of research, I believe I understand both cause and solution:

    Google app-builder tooling does some path-munging and is causing this. They are aware of the bug but no ETA to fix it.

    Problem summary: any .go files inside or below the directory holding main.go / app.yaml will be double imported…

    In summary, just make sure that ALL of our files/packages are siblings and not decedents of the directory holding those two files...

    0 讨论(0)
  • 2020-12-08 23:41

    I came up with another option that isn't discussed here and in my opinion is much easier to deal with (and keep your app less appengine specific). Lets say you have the repo at github.com/blah/blah and right now the root folder of the repo defines your app engine server.

    First, move the app.yaml and other app engine specific files (NOT .go files) into github.com/blah/blah/appengine/app.yaml.

    Next, wherever you run your init function for app engine, rename it to something like func Run() { ... }, and then in github.com/blah/blah/whatever.go write something like this:

    package appengine
    
    import "github.com/blah/blah"
    
    func init() {
        blah.Run()
    }
    

    From my experience this has resolved the issue and made things much easier. I'll update this if I run into any major issues that make this a bad solution.

    0 讨论(0)
提交回复
热议问题