jerry
jerry

Reputation: 839

build constraints exclude all Go files in

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)
}

Upvotes: 79

Views: 161246

Answers (16)

mgasp
mgasp

Reputation: 31

Following the hint given by @Lundis, in debian/ubuntu system i fixed this by installing build-essential package:

sudo apt-get install build-essential

Upvotes: 1

DubDub
DubDub

Reputation: 1387

If you're getting this with the standard library, you may be trying to use functionality that's not in your version of go currently.

My case is I was using go1.22.0 and tried to use the iter package here: https://pkg.go.dev/iter

If you check the versions, the earliest it was available is go1.23.0 (and the prereleases), so it's not available in my release.

Why this error in particular rather than couldn't find this package? Well the package does infact exist in the release, but specifically isn't intended to be exposed, see: https://pkg.go.dev/go/build#hdr-Build_Constraints for more details.

And that becomes evident looking at the package in my release: https://cs.opensource.google/go/go/+/refs/tags/go1.22.0:src/iter/iter.go;l=5

Upvotes: 0

GRASBOCK
GRASBOCK

Reputation: 737

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.

Upvotes: 0

AJR
AJR

Reputation: 1681

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

Upvotes: 0

Martin Meli
Martin Meli

Reputation: 575

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

Upvotes: 4

fzdy1914
fzdy1914

Reputation: 61

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

Upvotes: 6

Lundis
Lundis

Reputation: 490

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.

Upvotes: 13

user2233706
user2233706

Reputation: 7225

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.

Upvotes: 21

MousyBusiness
MousyBusiness

Reputation: 308

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

Upvotes: 1

DevOops
DevOops

Reputation: 955

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

Upvotes: 1

Andreas Gnyp
Andreas Gnyp

Reputation: 1850

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.

Upvotes: 70

zangw
zangw

Reputation: 48566

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

Upvotes: 12

kvaps
kvaps

Reputation: 2987

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

Upvotes: 3

cbg.hongminh
cbg.hongminh

Reputation: 11

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

Upvotes: -2

Sheetal Kaul
Sheetal Kaul

Reputation: 3277

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

Upvotes: -1

RK26
RK26

Reputation: 383

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

Upvotes: 4

Related Questions