build constraints exclude all Go files in
Asked Answered
M

14

69

When I import "golang.org/x/net/route"

It tells me: "build constraints exclude all Go files in go/src/golang.org/x/net/routego"

I am using VSCode on Windows/Linux.

I searched online and didn't see a solution to a similar problem.

package main

import (
    "golang.org/x/net/route"
)

{
    rib, _ := route.FetchRIB(0, route.RIBTypeRoute, 0)
    messages, err := route.ParseRIB(route.RIBTypeRoute, rib)
}
Melessa answered 26/3, 2019 at 1:4 Comment(1)
what fixed my issue was adding a main.go file (for some reason it was missing from my project dir)Thou
S
58

I had this problem in Goland (with source code on WSL2 and Goland on win).

I was able to solve it with

go clean -modcache

Subsequent build put everything in order again.

Supra answered 29/8, 2022 at 15:44 Comment(3)
This solved my problem. And I understand that go does intelligence around what files to build, if you set the GO_OS or name a file like xyz_<OS>.go, which can lead to the unfortunate fact of having 0 files to build with even though there are source files in the pwd... However, I'm not sure what happened such that removing cached modules fixes this? What happened?Music
I'm rather new to Go, so I drew from my "general experience" and speculated, after some reading here, that the cache was "mixed up". Meaning, I was installing packages using Golang AND the console. If there is some configuration going on--as you mentioned--the cache would "contradict" itself. Once again, just a thought! And I'd be very happy to amend my answer with a proper explanation, if we find something reproducible.Supra
This got me mostly there. Running go mod tidy completed the task.Coadjutress
B
19

My issue was that I had a stray import "C" in my code and I was compiling with CGO_ENABLED=0. This problem was hard to find because IntelliJ collapses all the imports into one statement.

Blowbyblow answered 9/1, 2023 at 20:13 Comment(2)
temporary enable cgo with: env CGO_ENABLED=1 go run mainLamonica
Also I needed to set CC=<path to gcc>Webfooted
B
12

We met the same error under Goland, and it could be solved in this way

If you want to set GOOS = linux under Mac, just add this line in the header of file // +build linux,amd64,go1.15,!cgo which means we declare that this file is for the target system that has the following requirements: Linux, the AMD64 architecture, the 1.15 Go version, and no CGO support.


Update 08/04/2022

After go 1.17. The go command now understands //go:build lines and prefers them over // +build lines. The new syntax uses boolean expressions, just like Go, and should be less error-prone. Here are some more details

// go:build (darwin && cgo) || linux
// +build darwin,cgo linux
Berga answered 9/4, 2021 at 11:52 Comment(0)
J
10

If you have a dependency requiring cgo (which includes x/net by default), you will get this error if your system does not have a C compiler. Can be easily fixed by installing gcc.

Josselyn answered 23/3, 2023 at 13:51 Comment(1)
Yeah, that's what it turned out to be for me. I was working in a new codebase (and I'm new to Go) and didn't have GCC on my box. I installed GCC and it built. The error message is misleading.Henke
J
5

I fixed the issue by simply doing Invalidate Caches under File Tab.

Judson answered 19/4, 2023 at 3:35 Comment(0)
W
4

try to remove folder x in go/src/golang.org/

Wildermuth answered 12/10, 2020 at 5:33 Comment(0)
Z
3

In my case I just by an accident replaced original code by test file, it was containing the following comments:

//go:build unittest
// +build unittest
Zacharia answered 7/2, 2022 at 20:40 Comment(1)
upvoted! This seems like it would be a commonly overlooked issue. I had the inversion of this issue. I.E. my test file didn't have the build constraints indicating this is a test file - so my test file wasn't allowed to import our core test files. The complication in my case was my test file was importing a package which requires cgo to be enabled - so I wasted a bunch of time ensuring that cgo had all necessary prequisites enabled.Florescence
P
3

In my case, after trying the go clean -modcache solution, I also had to restart Goland before it stopped complaining about the build constraints.

Psychopathy answered 14/6, 2023 at 10:16 Comment(1)
For me restarting IntelliJ worked. Thanks Martin!!Sedate
C
1

This error seems to present itself rather ambiguously.

In my case, my project uses CGO, and this error manifests when CGO_ENABLED=0 in the environment, which is solved by unsetting this env var, or setting CGO_ENABLED=1

Crackbrained answered 6/10, 2022 at 11:53 Comment(0)
H
1

In my case it appears the go get failed and the folder referenced in GOPATH was empty. Check your library folder which is complaining here. here

Hitt answered 13/11, 2022 at 1:21 Comment(0)
R
0

I had the same problem trying to import "golang.org/x/sync". I did not realise that (unlike the Go library "sync" package) there are only sub-packages in x/sync.

I should have been importing "golang.org/x/sync/errgroup".

Richma answered 7/10, 2023 at 4:56 Comment(0)
S
0

In my case I was building a docker image.
Changing from alpine FROM golang:1.22-alpine to FROM golang:1.22 helps.

Was not a problem for me, because I was using multistage-builds anyways.

Suborder answered 24/2 at 11:5 Comment(0)
B
-1

"build constraints exclude all Go files in go/src/golang.org/x/net/routego" In Intellij :

  1. Navigate to the folder in the project explorer
  2. Right click -> Mark folder as not excluded.
Bulldozer answered 4/10, 2021 at 3:2 Comment(0)
V
-2

If you get error build constraints exclude all Go files in ... with go get command, please specify the path as follows

Error:

$ go get {HOST}/platform/be-photo

Success:

$ go get {HOST}/platform/be-photo/external/client
Vyatka answered 20/10, 2021 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.