【问题标题】:Type Incompatibility With Vendor Folder类型与供应商文件夹不兼容
【发布时间】:2019-08-15 06:31:32
【问题描述】:

我正在通过 Jenkins 部署一个 Go 应用程序并运行一些测试。

即使我从我的 GOPATH 中删除了所有第三方库,我的所有测试都在本地通过,因为我已经通过 godep save 填充了我的 vendor 文件夹。

但是,当 Jenkins 运行我的测试时,它报告 GitHub 版本和供应商版本之间的类型不兼容:

mypackage/MyFile_test.go:65:22: cannot use MY_VARIABLE
(type "github.com/gocql/gocql".UUID) as type 
"myproject/vendor/github.com/gocql/gocql".UUID in assignment

我尝试使用Dep(Go 团队的官方供应商经理)而不是godep,但没有解决问题。

我是否需要告诉我的测试使用“myproject/vendor/github.com/gocql/gocql”而不是“github.com/gocql/gocql”? (更新:显然这是非法的,会报错must be imported as github.com/gocql/gocql。)

我该如何解决这个问题?

更新:

  • 我在本地机器和 Jenkins 服务器上都使用 Go 1.12.1。
  • 我没有使用任何形式的go modules

这是我的 Jenkins 流水线代码的 Go 部分。会不会和这个问题有关?

steps {                                           
    // Create our project directory.
    sh 'cd ${GOPATH}/src'
    sh 'mkdir -p ${GOPATH}/src/myproject'

    // Copy all files in our Jenkins workspace to our project directory.                
    sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/myproject'

    // Copy all files in our "vendor" folder to our "src" folder.
    sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'

    // Build the app.
    sh 'go build'               

    // Remove cached test results.
    sh 'go clean -cache'

    // Run Unit Tests.
    sh 'go test ./... -v -short'    
}

【问题讨论】:

  • 你使用的是什么版本的 go? Jenkins 和您本地开发的版本是否相同?
  • @Varcorb 我在本地和 Jenkins 服务器上都使用 Go 1.12.1,但我认为这与这个问题没有任何关系,因为它看起来更像是 Go 处理 @987654329 @ 库与非vendor 库不同。鉴于/vendor/ 是它们名字的唯一区别,Go 不应该足够聪明地知道它们是相同的吗?
  • @Varcorb 我假设这是因为如果 Jenkins 在 GOPATH 中找不到第三方库,它只会查看 vendor 文件夹。这意味着它应该对vendor 和非vendor 库一视同仁,不是吗?
  • 我问的原因是因为 1.12.1 有go modules 功能,可能不是在寻找GOPATHGO111MODULE 环境变量是否设置为 on?。
  • @Varcorb 我没有使用go modules,因为我认为它还没有被广泛使用。我使用的一些第三方库与它不完全兼容,应该没问题,因为我没有在Jenkins中配置Go使用go modules

标签: go jenkins dependency-management vendor


【解决方案1】:

正如怀疑的那样,问题出在我的 Jenkins 配置上(因为在本地一切正常)。

原来每条sh 行都代表一个新的 shell 终端,所以我需要做的就是将所有内容放入一个 sh 部分,如下所示:

steps {
    // Create our expected project directory inside our GOPATH's src folder.
    // Move our project codes (and its vendor folder) to that folder.
    // Build and run tests.
    sh '''                    
        mkdir -p ${GOPATH}/src/myproject
        mv ${WORKSPACE}/* ${GOPATH}/src/myproject
        cd ${GOPATH}/src/myproject
        go build
        go clean -cache
        go test ./... -v -short
       '''
}

非常感谢所有帮助过的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2019-04-05
    • 2013-10-30
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多