背景: 用 Python + Golang + Vue.js 做一个开源项目,在用 Beego 工具 bee 搭建项目结构,生成文档(bee generate docs or bee run -downdoc=true -gendoc=true) 时报错:Failed to generate the docs.
bee 的 commit 版本为
commit 6a86284cec9a17f9aae6fe82ecf3c436aafed68d (HEAD -> develop, origin/develop, origin/HEAD)
Merge: 6f1ff54 0f9b9ea
Author: Faissal Elamraoui <amr.faissal@gmail.com>
Date: Thu Apr 11 07:34:49 2019 +0200
Merge pull request #586 from cjereme/fix/infosec-output-security
Fixes #536
需要在终端设置好: GOROOT 和 GOPATH,按照官方的 DEMO 来还是报错 Failed to generate the docs. 项目包管理方式选择 Go Modules,上网搜了一下,貌似没啥解决办法。只能翻看源码了,大概的源码在 bee/generate/swaggergen/g_docs.go。
大概的原因就是 bee 工具不支持 Go Modules, 生成项目的时候会自动穿创建 src 目录,可是在使用 Go Modules 时已经不依赖于 src。
下面是我的项目目录结构
image.png
import 的方式都是直接 import "fishhub/fish/xxx",然而在生成文档时, analyseControllerPkg(bee/generate/swaggergen/g_docs.go::analyseControllerPkg) 函数会检查文件是否在,代码如下:
pkgRealpath := ""
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
} else {
wgopath := gopaths
for _, wg := range wgopath {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
}
}
if pkgRealpath != "" {
if _, ok := pkgCache[pkgpath]; ok {
return
}
pkgCache[pkgpath] = struct{}{}
} else {
beeLogger.Log.Fatalf("Package '%s' does not exist in the GOPATH or vendor path", pkgpath)
}
很明显,会在 GOPATH 或者 VendorPATH 里拼接路径,然后检测是否存在此文件,然后 Go Modules 包管理的项目是不依赖 GOPATH,因此结合 pkgpath 肯定会检测文件失败。
解决方案
- 不使用
GO Modules管理项目。 - 打个补丁:
我的项目为例:绝对路径是:/Users/user/xxx/fishhub
此时需要在终端设置export GOPATH=/Users/user/xxx, 原因是会通过pkgpath判断文件是否存在,添加的代码如下:
if pkgRealpath == "" {
for _, wg := range gopaths {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
}
}
即检测文件是否存在的代码改为:
pkgRealpath := ""
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
} else {
wgopath := gopaths
for _, wg := range wgopath {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, "src", pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
}
}
if pkgRealpath == "" {
for _, wg := range gopaths {
wg, _ = filepath.EvalSymlinks(filepath.Join(wg, pkgpath))
if utils.FileExists(wg) {
pkgRealpath = wg
break
}
}
}
if pkgRealpath != "" {
if _, ok := pkgCache[pkgpath]; ok {
return
}
pkgCache[pkgpath] = struct{}{}
} else {
beeLogger.Log.Fatalf("Package '%s' does not exist in the GOPATH or vendor path", pkgpath)
}
然后 go install 重新生成 bee 可执行文件就可以了。











网友评论