Build constraints exclude all Go files (macOS with Go 1.18.3) [duplicate] - go

This question already has an answer here:
golang build constraints exclude all Go files in
(1 answer)
Closed 8 months ago.
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:
build constraints exclude all Go files in
build constraints exclude all Go files
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?

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.

Related

Go module installation error "undefined: any" [duplicate]

This question already has an answer here:
Why do I get "undeclared name: any (requires version go1.18 or later)" when using any instead of interface{}? I am using Go 1.18
(1 answer)
Closed 2 months ago.
I got the following error. Could anybody try the same command? I want to know whether it is a bug in the package or it is related to the version of go that I use or my setup.
$ go get github.com/gonejack/webarchive-to-html
# github.com/alecthomas/kong
../../../go/src/github.com/alecthomas/kong/callbacks.go:105:65: undefined: any
../../../go/src/github.com/alecthomas/kong/callbacks.go:124:15: undefined: any
../../../go/src/github.com/alecthomas/kong/context.go:723:27: undefined: any
../../../go/src/github.com/alecthomas/kong/options.go:59:8: undefined: any
../../../go/src/github.com/alecthomas/kong/options.go:66:18: undefined: any
$ echo $?
2
$ go version
go version go1.16 darwin/amd64
Let's see go.mod file of the package you trying to get. The package is written down based on 1.18 version of golang. So you need at least 1.18 version.
module github.com/gonejack/webarchive-to-html
go 1.18
...
Built-in type any is introduced in go 1.18. any is a simple alias for interface{}.
// builtin.go
type any = interface{}

Unable to compile simple go program using foundationdb

Trying to cross compile on macos arm for linux. My sample project looks like this:
main.go:
package main
import(
"github.com/apple/foundationdb/bindings/go/src/fdb"
)
func main() {
fdb.APIVersion(630)
fdb.MustOpenDatabase("fdb.cluster")
}
go.mod
module fdbtest
go 1.19
require github.com/apple/foundationdb/bindings/go v0.0.0-20221026173525-97cc643cef69
require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
go.sum
github.com/apple/foundationdb/bindings/go v0.0.0-20221026173525-97cc643cef69 h1:vG55CLKOUgyuD15KWMxqRgTPNs8qQfXPtWjYYN5Wai0=
github.com/apple/foundationdb/bindings/go v0.0.0-20221026173525-97cc643cef69/go.mod h1:w63jdZTFCtvdjsUj5yrdKgjxaAD5uXQX6hJ7EaiLFRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
I've installed foundationdb go lang bindings via go get github.com/apple/foundationdb/bindings/go#6.3.25
but when I do env GOOS=linux GOARCH=amd64 go build I get the following errors:
env GOOS=linux GOARCH=amd64 go build
# github.com/apple/foundationdb/bindings/go/src/fdb
../../../go/pkg/mod/github.com/apple/foundationdb/bindings/go#v0.0.0-20221026173525-97cc643cef69/src/fdb/keyselector.go:39:10: undefined: KeyConvertible
../../../go/pkg/mod/github.com/apple/foundationdb/bindings/go#v0.0.0-20221026173525-97cc643cef69/src/fdb/snapshot.go:33:3: undefined: transaction
../../../go/pkg/mod/github.com/apple/foundationdb/bindings/go#v0.0.0-20221026173525-97cc643cef69/src/fdb/generated.go:45:9: undefined: NetworkOptions
<...>
../../../go/pkg/mod/github.com/apple/foundationdb/bindings/go#v0.0.0-20221026173525-97cc643cef69/src/fdb/generated.go:94:9: too many errors
So it seems that it cannot find any of the types from fdb. Yet the KeyConvertible and the NetworkOptions (and others) exist in ../../../go/pkg/mod/github.com/apple/foundationdb/bindings/go#v0.0.0-20221026173525-97cc643cef69/src/fdb/fdb.go
My golang version: go version go1.19.3 darwin/arm64
Newer fdb go bindings (7.1.25, 7.1.0) seem to behave the same...
what am I missing here?

Verify FIPS mode in golang boringssl

how to verify if fips mode is enabled for binary in golang dev boring crypto branch ?
I dont see an easy way apart from internal golang tests
From this file:
https://go.googlesource.com/go/+/dev.boringcrypto/src/crypto/tls/fipsonly/fipsonly.go
// Package fipsonly restricts all TLS configuration to FIPS-approved settings.
//
// The effect is triggered by importing the package anywhere in a program, as in:
//
// import _ "crypto/tls/fipsonly"
//
// This package only exists in the dev.boringcrypto branch of Go.
By including that import statement in your program, it will only compile if you're using the dev.boringcrypto branch.
Here's a test main.go:
package main
import (
"fmt"
_ "crypto/tls/fipsonly"
)
func main() {
fmt.Println("Hello FIPS")
}
Using the dev.boringcrypto branch of Go:
$ go version
go version go1.12.9b4 linux/amd64
$ go run main.go
Hello FIPS
Using the normal release of Go:
$ go version
go version go1.12.9 darwin/amd64
$ go run main.go
main.go:4:2: cannot find package "crypto/tls/fipsonly" in any of:
/Users/ray/.gimme/versions/go1.12.9.darwin.amd64/src/crypto/tls/fipsonly (from $GOROOT)
/Users/ray/go/src/crypto/tls/fipsonly (from $GOPATH)

Adding gokogiri dependency causes `Killed: 9` exit

The environment I am using is go1.8 on MacOS Sierra.
The code:
package main
import (
"fmt"
"io/ioutil"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/xpath"
)
func main() {
fmt.Println("hello world")
b, _ := ioutil.ReadFile("x.xml")
fmt.Println(string(b))
doc, _ := gokogiri.ParseXml(b)
compiled := xpath.Compile("/path/to/node")
ss, _ := doc.Root().Search(compiled)
for _, s := range ss {
fmt.Println(s.Content())
}
}
After I build and run:
$ ./hello-world
Killed: 9
Even the hello world message is not printed. Later on, on investigating the gokogiri README, I saw instructions on installing libxml2. So I did brew install libxml2 and tried, and that also did not fix the issue.
According to similar issue, and also in Golang issue #19734, cgo command broken on darwin after performing c tool-chain (Xcode 8.3) update from Apple.
The solution: upgrade to go1.8.1 or above or add -ldflags=-s to build or test command, e.g. go build -ldflags=-s.

syscall variables undefined

When trying to build the following program on my Mac I get a build error: undefined: syscall.TCPInfo even though that variable is clearly documented http://golang.org/pkg/syscall/#TCPInfo
package main
import "syscall"
func main() {
_ = syscall.TCPInfo{}
}
Here is my go tool version.
$ go version
go version go1.3 darwin/amd64
I thought this might be an issue a lack of OS support so I tried it on http://play.golang.org, but it looks like many documented variables are just randomly missing from the syscall package: http://play.golang.org/p/w3Uk6NaZVy
Am I missing something?
The variable specified inside syscall are OS dependent.
you can add a suffix to the file to specify which os they should be built for:
// +build linux,386 darwin,!cgo
So you can use specific syscall flags for each OS.

Resources