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

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

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

问题

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

package main

import (
	"regexp"
	"testing"

	"github.com/gruntwork-io/terratest/modules/terraform"
	"github.com/stretchr/testify/assert"
)

func TestS3Creation(t *testing.T) {
	t.Parallel()

	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
		TerraformDir: "./unit-test",
	})

	defer terraform.Destroy(t, terraformOptions)

	terraform.InitAndApply(t, terraformOptions)

	bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")

	assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
}

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

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

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

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

我的环境如下:

macOS Big Sur 11.6.4
CPU: Intel i9
terraform --version                                                                      
Terraform v1.2.3
on darwin_amd64
go version
go version go1.18.3 darwin/amd64

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

根据以下建议:

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

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

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

export GOOS=darwin
export GOARCH=amd64

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

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

英文:

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

package main

import (
	"regexp"
	"testing"

	"github.com/gruntwork-io/terratest/modules/terraform"
	"github.com/stretchr/testify/assert"
)

func TestS3Creation(t *testing.T) {
	t.Parallel()

	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
		TerraformDir: "./unit-test",
	})

	defer terraform.Destroy(t, terraformOptions)

	terraform.InitAndApply(t, terraformOptions)

	bastionSecurityGroup := terraform.Output(t, terraformOptions, "bastion_security_group")

	assert.Regexp(t, regexp.MustCompile(`^sg-*`), bastionSecurityGroup)
}

I initialised it as follows:

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

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

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:

macOS Big Sur 11.6.4
CPU: Intel i9
terraform --version                                                                      
Terraform v1.2.3
on darwin_amd64
go version
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

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

And also exporting the GOOS and GOARCH env variables

export GOOS=darwin
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:

确定