Search code examples
goterraformterratest

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


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?


Solution

  • 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.