问题描述
所以我花了两天的大部分时间试图解决这个问题,无论我做什么,我都无法解决问题.这是发生了什么:
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:
- 使用 Go 和 Appengine.我在尝试时遇到问题进行适当的单元测试.
- 我尝试了很多结构,但这里是我现在所处位置的示例:https://github.com/markhayden/SampleIssue
- 我在
goapp serve
或goapp test -v ./src/lib1
中遇到依赖问题,这取决于我如何设置导入路径.
- Using Go and Appengine. I am running into issues when trying toget 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
orgoapp test -v ./src/lib1
depending on how I have my import paths set.
如果我使用 "src/lib1"
作为我的导入路径,然后使用 goapp serve
.我的应用程序启动并运行良好,但是当我运行测试时出现以下故障:
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)
同样,如果我使用 "dummy/src/lib1"
作为我的路径,我的测试很高兴并且运行良好,但是在 goapp serve
运行应用程序后,我现在得到:
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
已经摆弄了各种不同的选项,但无法弄清楚如何处理依赖项并且仍然进行可靠的测试.也许它是一个 appengine/golang 错误?还是我遗漏了什么?
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!
根据第一条评论反馈更新了所有内容.我可以运行测试(正如我以前能够做的那样),但我仍然无法为该应用程序提供服务.这是我在运行 goapp serve
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 = 未设置(根据文档,如果您不使用自定义目录,则不需要设置)
$GOPATH = /Users/markhayden/Projects/go
$GOROOT = not set (according to docs it doesnt need to be if you dont use a custom directory)
应用结构:
$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
在 main.go 中:
In main.go:
import (
"fmt"
"github.com/markhayden/SampleIssue/lib1"
"net/http"
)
在 lib1/lib1.go 中:
In lib1/lib1.go:
import (
"fmt"
"github.com/markhayden/SampleIssue/lib2"
)
推荐答案
Appengine与从 GOPATH 导入的相同文件冲突"问题:
Appengine 正在导入根目录下的内容(即 app.yaml 所在的位置).这将导致两次导入,一次由 appengine 在扫描目录时进行,另一次由您的源在显式导入时进行.
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.
你有两个选择:
不要在 appengine 中使用完整的导入路径(对于子文件夹包).
删除导入的源存储库部分.所以代替github.com/blah/blah"就是blah/blah".
Remove the source repository part of import. So instead of"github.com/blah/blah" it would be "blah/blah".
注意:这有点糟糕,因为它使您的构建和软件应用引擎变得特定.您可以通过使用 构建约束 使这更好一点 - 也许 -.例如+build !appengine
或+build !appengine
从构建中包含/删除某些文件,具体取决于您是否针对 appengine.
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.
将您的模块/依赖项(子文件夹)移动到一个单独且独立的项目中,使其与完整路径导入约定一起使用:
- 删除主项目中的所有目录/依赖项(其中您的 app.yaml 是),因此 appengine 无法扫描并找到它们.
- 将它们移动到另一个独立项目(我做了 SampleIssueDeps)没有 app.yaml 不是 子目录(例如/MarkHayden/SampleIssueDeps).
- 然后通过以下方式拉取这些依赖项全路径导入.例如github.com/MarkHayden/SampleIssueDeps/lib1.
- Get rid of all directories / dependencies in the main project (whereyour app.yaml is), so that appengine can't scan and find them.
- Move them to another independent project (I did SampleIssueDeps)with no app.yaml that is not a sub-directory (e.g./MarkHayden/SampleIssueDeps).
- Then pull those dependencies viafull path import. e.g. github.com/MarkHayden/SampleIssueDeps/lib1.
总结:对于 appengine 项目中的子文件夹包,不要包含导入路径的源存储库"部分,或者只使用 appengine 到 init() 并移动所有其他代码分离项目并使用类似的外部依赖项.
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 导入和冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!