构建约束排除了所有Go文件(使用Go 1.18.3的macOS)。

huangapple go评论124阅读模式
英文:

Build constraints exclude all Go files (macOS with Go 1.18.3)

问题

我是新手,对Go和terratest不太了解。我有以下的terratest代码:

  1. package main
  2. import (
  3. "regexp"
  4. "testing"
  5. "github.com/gruntwork-io/terratest/modules/terraform"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestS3Creation(t *testing.T) {
  9. t.Parallel()
  10. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
  11. TerraformDir: "./unit-test",
  12. })
  13. defer terraform.Destroy(t, terraformOptions)
  14. terraform.InitAndApply(t, terraformOptions)
  15. bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")
  16. assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
  17. }

我按照以下方式进行初始化:

  1. go mod init github.com/myorg/terraform-bastion-linux

当我尝试使用go test -v运行它时,我收到以下错误:

  1. package github.com/myorg/terraform-bastion-linux: build constraints exclude all Go files in /Users/george/terraform-bastion-linux/test

我的环境如下:

  1. macOS Big Sur 11.6.4
  2. CPU: Intel i9
  3. terraform --version
  4. Terraform v1.2.3
  5. on darwin_amd64
  6. go version
  7. go version go1.18.3 darwin/amd64

我没有设置以GO开头的环境变量,例如,env|grep GO没有返回任何结果。

根据以下建议:

我尝试在文件顶部添加以下内容:

  1. //+build darwin,cgo linux
  2. //go:build (darwin && cgo) || linux

并且导出了GOOS和GOARCH环境变量:

  1. export GOOS=darwin
  2. export GOARCH=amd64

但是我仍然收到相同的错误。

如何解决这个问题?
为了成功运行测试,我可以尝试什么?

英文:

I am new to Go and terratest. I have the following terratest

  1. package main
  2. import (
  3. "regexp"
  4. "testing"
  5. "github.com/gruntwork-io/terratest/modules/terraform"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestS3Creation(t *testing.T) {
  9. t.Parallel()
  10. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
  11. TerraformDir: "./unit-test",
  12. })
  13. defer terraform.Destroy(t, terraformOptions)
  14. terraform.InitAndApply(t, terraformOptions)
  15. bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")
  16. assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
  17. }

I initialised it as follows:

  1. go mod init github.com/myorg/terraform-bastion-linux

When trying to run it with go test -v I get the error:

  1. package github.com/myorg/terraform-bastion-linux: build constraints exclude all Go files in /Users/george/terraform-bastion-linux/test

My environment is as follows:

  1. macOS Big Sur 11.6.4
  2. CPU: Intel i9
  3. terraform --version
  4. Terraform v1.2.3
  5. on darwin_amd64
  6. go version
  7. go version go1.18.3 darwin/amd64

I have no env variables set that start with GO, for example, env|grep GO returns nothing as result.

As advised in:

I have tried adding the following on top of the file

  1. //+build darwin,cgo linux
  2. //go:build (darwin && cgo) || linux

And also exporting the GOOS and GOARCH env variables

  1. export GOOS=darwin
  2. export GOARCH=amd64

But I still get the same error.

How to troubleshoot this issue?
What can I try in order to run the test successfully?

答案1

得分: 0

根据JimB的评论,问题在于文件名中包含了"linux"这个词,Go语言将其解释为一个构建约束,只能在Linux上运行,而不能在Darwin上运行。我对Go语言还不熟悉,这种错误看起来非常误导人。我甚至在我的原始问题中没有提到文件名,因为我认为它是无关紧要的。

英文:

As commented by JimB, the problem was that the filename included the word linux, which Go interpreted as a build constraint to only run on Linux and not Darwin. I am new to Go, and it appears to be terribly misleading with such errors. I didn't even include the file name in my original question as I thought it would be irrelevant.

huangapple
  • 本文由 发表于 2022年6月16日 20:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/72646272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定