Google Go AppEngine在服务/测试时导入并冲突 [英] Google Go AppEngine imports and conflicts when serving / testing

查看:137
本文介绍了Google Go AppEngine在服务/测试时导入并冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我花了两天的好时间试图弄清楚这一点,无论我做什么,我都无法理顺事情。这是发生了什么:




  • 使用Go和Appengine。我试图
    得到适当的单元测试工作时遇到问题。
  • 我已经尝试了很多结构,但这里是我现在的样本:https://github.com/markhayden/SampleIssue

  • 我正在运行 goapp serve goapp test -v ./src/lib1 ,具体取决于我如何设置导入路径。



如果我使用src / lib1作为我的导入路径,然后 goapp serve 。我的应用程序启动并运行正常,但是当我运行测试时,出现以下故障:

  src / lib1 / lib1.go: 5:2:在
/ Users / USERNAME / go_appengine / goroot / src / pkg / src / lib2(from $ GOROOT)中找不到软件包src / lib2
/ Users / markhayden / Projects / go / src / src / lib2(from $ GOPATH)

同样,如果我使用dummy / src / lib1作为我的路径,我的测试很开心并且运行良好,但是在 goapp serve 应用程序I现在得到:

  2014/11/06 20:33:34 go-app-builder:解析输入失败:app文件lib1 .go与从GOPATH导入的相同文件冲突

已经摆弄各种不同的选项,弄清楚如何处理依赖关系并仍然有坚实的测试。也许它是一个appengine / golang的bug?或者我错过了什么?



任何帮助将非常感激。提前致谢!






根据第一条评论反馈更新了所有内容。我可以运行测试(就像我以前能够做的那样),但我仍然无法为应用程序提供服务。这是我运行 goapp serve

  INFO 2014-11 -07 17:24:48,727 devappserver2.py:745]跳过SDK更新检查。 
INFO 2014-11-07 17:24:48,748 api_server.py:172]启动API服务器:http:// localhost:60732
INFO 2014-11-07 17:24:48,751调度程序。 py:185]启动模块默认运行在:http:// localhost:8080
INFO 2014-11-07 17:24:48,754 admin_server.py:118]启动admin服务器:http:// localhost :8000
错误2014-11-07 17:24:49,041 go_runtime.py:171]无法构建Go应用程序:(执行命令:/ Users / markhayden / go_appengine / goroot / bin / go-app-builder - app_base /Users/markhayden/Projects/go/src/github.com/markhayden/SampleIssue -arch 6 -dynamic -goroot / Users / markhayden / go_appengine / goroot -nobuild_files ^^ $ -unsafe -gopath / Users / markhayden / Projects / go -print_extras_hash lib1 / lib1.go lib2 / lib2_test.go main_test.go main.go lib1 / lib1_test.go lib2 / lib2.go)

2014/11/07 09:24:49 go- app-builder:解析输入失败:应用程序文件lib2.go与从GOPATH导入的相同文件冲突

$ GOPATH = / Users / markhayde n / Projects / go
$ GOROOT =没有设置(根据docs,如果你不使用自定义目录,它不需要)



应用程序结构:

  $ GOPATH / src / github.com / markhayden / SampleIssue / 
- yaml
- / lib1
- lib1_test.go
- lib1.go
- / lib2
- lib2_test.go
- lib2.go
- main_test.go
- main.go

在main.go中:

 导入(
fmt
github.com/markhayden/SampleIssue/lib1
net / http



在lib1 / lib1.go中:

 导入(
fmt
github.com/markhayden/SampleIssue/lib2


解决方案

Appengine与从GOPATH导入的相同文件冲突问题: / h2>

Appengine正在导入根目录下的东西(即app.yaml在哪里)。这将导致两个导入,其中一个在appengine扫描目录时导致,另一个导入时由源导入。



您有两种选择:

不要使用完整的导入路径使用appengine。




  • 删除导入的源存储库部分。所以,而不是
    github.com/blah/blah这将是blah / blah。



    注意:有点吮吸,因为它使你的构建和软件appengine具体。通过使用构建约束,您可以使其更好一些。例如 + build!appengine
    + build!appengine 包含/删除构建中的某些文件,具体取决于如果你的目标是appengine。


    将您的模块/依赖关系(子文件夹)移动到一个独立的独立项目中以使其工作使用完整路径导入约定:


    1. 摆脱主项目中的所有目录/依赖项(其中
      你的app.yaml是),这样appengine就无法扫描并找到它们。

    2. 将它们移动到另一个独立的项目(我做了SampleIssueDeps)
      ,没有app.yaml是不是子目录(例如
      / MarkHayden / SampleIssueDeps)。
    3. 然后,通过
      完整路径导入拉出这些依赖关系。例如github.com/MarkHayden/SampleIssueDeps/lib1。

    摘要:对于appengine中的子文件夹包项目不包括导入路径的源存储库部分,或者只使用init()的appengine并将所有其他代码移动到单独的项目中并使用像外部依赖项。

    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:

    • Using Go and Appengine. I am running into issues when trying to get proper unit tests working.
    • I have tried lots of structures but here is a sample of where I am now: https://github.com/markhayden/SampleIssue
    • I am running into dependency issues in either goapp serve or goapp test -v ./src/lib1 depending on how I have my import paths set.

    If I use "src/lib1" for my import path and then goapp serve. My app boots and runs fine, but when I run tests I get the following failure:

    src/lib1/lib1.go:5:2: cannot find package "src/lib2" in any of:
        /Users/USERNAME/go_appengine/goroot/src/pkg/src/lib2 (from $GOROOT)
        /Users/markhayden/Projects/go/src/src/lib2 (from $GOPATH)
    

    Likewise, if I use "dummy/src/lib1" as my path, my tests are happy and run fine but upon goapp serve ing the app I now get:

    2014/11/06 20:33:34 go-app-builder: Failed parsing input: app file lib1.go conflicts with same file imported from GOPATH
    

    Have fiddled with all sorts of different options and can't figure out how to handle dependencies and still have solid testing. Maybe its a appengine / golang bug? Or am I missing something?

    Any help would be very much appreciated. Thanks in advance!


    Updated everything based on first comment feedback. I can run tests (as I was able to do before) but I still can not serve the app. Here is what I get when running goapp serve

    INFO     2014-11-07 17:24:48,727 devappserver2.py:745] Skipping SDK update check.
    INFO     2014-11-07 17:24:48,748 api_server.py:172] Starting API server at: http://localhost:60732
    INFO     2014-11-07 17:24:48,751 dispatcher.py:185] Starting module "default" running at: http://localhost:8080
    INFO     2014-11-07 17:24:48,754 admin_server.py:118] Starting admin server at: http://localhost:8000
    ERROR    2014-11-07 17:24:49,041 go_runtime.py:171] Failed to build Go application: (Executed command: /Users/markhayden/go_appengine/goroot/bin/go-app-builder -app_base /Users/markhayden/Projects/go/src/github.com/markhayden/SampleIssue -arch 6 -dynamic -goroot /Users/markhayden/go_appengine/goroot -nobuild_files ^^$ -unsafe -gopath /Users/markhayden/Projects/go -print_extras_hash lib1/lib1.go lib2/lib2_test.go main_test.go main.go lib1/lib1_test.go lib2/lib2.go)
    
    2014/11/07 09:24:49 go-app-builder: Failed parsing input: app file lib2.go conflicts with same file imported from GOPATH
    

    $GOPATH = /Users/markhayden/Projects/go $GOROOT = not set (according to docs it doesnt need to be if you dont use a custom directory)

    App Structure:

    $GOPATH/src/github.com/markhayden/SampleIssue/
     - app.yaml
     - /lib1
        - lib1_test.go
        - lib1.go
     - /lib2
        - lib2_test.go
        - lib2.go
     - main_test.go
     - main.go
    

    In main.go:

    import (
        "fmt"
        "github.com/markhayden/SampleIssue/lib1"
        "net/http"
    )
    

    In lib1/lib1.go:

    import (
        "fmt"
        "github.com/markhayden/SampleIssue/lib2"
    )
    

    解决方案

    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.

    这篇关于Google Go AppEngine在服务/测试时导入并冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆