Go import error: invalid version unknow revision - go

Code, file structure on left and error
package main
import (
"fmt"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/client"
log "github.com/sirupsen/logrus"
)
func main() {
fmt.Println("Starting Application...")
fmt.Println("Terminating Application...")
}
This is the error I get when I run: go run main.go
github.com/cilium/cilium#1.7.2 requires github.com/optiopay/kafka#v0.0.0-2080809090225-01ce283b732b: ivalid version: unknown revision 01ce283b732b
I am using go mod file for my dependencies and my go version is latest 1.14
I also checked my file structure and I already have cilium#v1.7.2 under pkg/mod/github.com/cilium
I tried adding github.com/optiopay/kafka under my import statement in my code but i get the same exact error still.
I have no idea how to fix this, I was googling but I could fin no definite or clear answer which worked. Any help appreicated.

You are seeing this error because replace directives in go.mod are ignored by go mod/go get and the go.mod file of Cilium v1.7.2 contains many replace directives.
To workaround this issue, replicate the replace directives of the version of Cilium that you are trying to import into your own go.mod file. An example for Cilium v1.7 can be found in this repository.

seems optiopay/kafka is your private repo. You can configure ssh with git. and set your url in git config to use ssh instead of https.
Steps:
configure ssh (if not already done)
update git config ->
git config --system url."git#github.com:".insteadOf "https://github.com/"

Error - the version that go is trying to pull for that specific module that imports it is either outdated or invalid.
Solution - For example in my case i was getting this error below:
go: downloading github.com/myk4040okothogodo/tutorial/gen/go/proto/books v0.0.0-00010101000000-000000000000
github.com/myk4040okothogodo/tutorial/books imports
github.com/myk4040okothogodo/tutorial/books/server imports
github.com/myk4040okothogodo/tutorial/gen/go/proto/books: github.com/myk4040okothogodo/tutorial/gen/go/proto/books#v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000
as you can see above my module "github.com/myk4040okothogodo/tutorial/books/server" imports another module "github.com/myk4040okothogodo/tutorial/gen/go/proto/books:" this import throws the error above, so i go to my go.mod file and make the following changes:
1 module github.com/myk4040okothogodo/tutorial/books/server
2
3 go 1.18
4
5 replace github.com/myk4040okothogodo/tutorial/db => ../../db
6
7 replace github.com/myk4040okothogodo/tutorial/gen/go/proto/books => ../../gen/go/proto/books
8
9 require (
10 github.com/arangodb/go-driver v1.3.2
11 github.com/myk4040okothogodo/tutorial/db v0.0.0-00010101000000-000000000000
12 github.com/myk4040okothogodo/tutorial/gen/go/proto/books latest
13 google.golang.org/grpc v1.47.0
14 )
check above in line 12 where i put "latest" instead of the version number i.e "v0.0.0...."
I save the file and then i run "go mod tidy"
The compiler then changes the "latest" designation to an up to date version,i.e it will look as below after running the mod tidy command.
1 module github.com/myk4040okothogodo/tutorial/books/server
2
3 go 1.18
4
5 replace github.com/myk4040okothogodo/tutorial/db => ../../db
6
7 replace github.com/myk4040okothogodo/tutorial/gen/go/proto/books => ../../gen/go/proto/books
8
9 require (
10 github.com/arangodb/go-driver v1.3.2
11 github.com/myk4040okothogodo/tutorial/db v0.0.0-00010101000000-000000000000
12 github.com/myk4040okothogodo/tutorial/gen/go/proto/books v0.0.0-20220601171028-60237b9c9583
13 google.golang.org/grpc v1.47.0
14 )
PS: check the package where i make the changes, dont change the wrong imports

Related

Golang: generating core failed: unable to load github.com

I installed golang and started with this tutorial: https://www.howtographql.com/graphql-go/1-getting-started/
When I run:
go run github.com/99designs/gqlgen generate
I get:
reloading module info
generating core failed: unable to load github.com/my-user/hackernews/graph/model - make sure you're using an import path to a package that exists
exit status 1
What is wrong with my setup?
This is my gopath
/Users/my-pc-user/go
TLDR
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .
Explanation
According to a quick start guide, you should create a package with generated code that actually is already imported by your server:
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"your_module_name/graph"
"your_module_name/graph/generated"
)
Because of the fact that your_module_name/graph/generated has no *.go files you cannot start a server and if you try you will get an error like:
graph/schema.resolvers.go:10:2: no required module provides package your_module_name/graph/generated; to add it:
To generate that package you will need to execute go run github.com/99designs/gqlgen generate, but there is another problem: gqlgen generates code that uses another package that is still doesn't exist yet, i.e. your_module_name/graph/model.
Additional step adding build constraints is required to not drop indirect dependency while generation process. That's why there is the first step with underscore import.
And if you put any *.go file to that directory with package directive — everything should works now:
$ cd your_project_path/
$ print '// +build tools\npackage tools\nimport _ "github.com/99designs/gqlgen"' | gofmt > ./tools.go
$ echo 'package model' | gofmt > ./graph/model/doc.go
$ go get .

use k8s deepcopy-gen generate code failed

I am going to use k8s code-generator to generator deepcopy file and my project under GOPATH/src, but i seems not work and got a problem about GOROOT.
deepcopy-gen command is deepcopy-gen -i k8s_customize_controller/pkg/apis -p k8s_customize_controller/pkg/client -v 10
output blow:
[root#centos72-k8s code-generator]# deepcopy-gen -i k8s_customize_controller/pkg/apis -p k8s_customize_controller/pkg/client -v 10
I0122 02:51:04.609157 17278 parse.go:383] importPackage k8s_customize_controller/pkg/apis
I0122 02:51:04.609359 17278 parse.go:330] addDir k8s_customize_controller/pkg/apis
I0122 02:51:04.730397 17278 parse.go:404] unable to import "k8s_customize_controller/pkg/apis": package k8s_customize_controller/pkg/apis is not in GOROOT (/usr/local/go/src/k8s_customize_controller/pkg/apis)
I0122 02:51:04.730701 17278 main.go:82] Completed successfully.
unable to import "k8s_customize_controller/pkg/apis": package k8s_customize_controller/pkg/apis is not in GOROOT
it seems this problem about GOROOT?
how to resolve this problem?
I had similar issue with an error like this:
Generating deepcopy funcs
F1018 10:51:28.259741 74132 main.go:80] Error: Failed making a parser: unable to add directory "github.com/[my-git-account]/[repo-name]/pkg/apis/v1": No files for pkg "github.com/[my-git-account]/[repo-name]/pkg/apis/v1"
The problem was that I recently moved my github golang projects folders out of the $GOPATH/src folder (which is in my case is ~/go/src), because it worked well on vanilla Ubuntu and WSL Ubuntu, but it has challenges to update packages on MacOS - I moved all my project from the folder ~/go/src/github.com/[my-git-account]
(where the code-generator expected them) to the folder ~/dev/[my-git-account].
Solution I use to fix the error above - to create a symbolic link on my current github projects folder to the $GOPATH/src/github.com:
ln -s ~/dev/[my-git-account] $GOPATH/src/github.com
This way there is a folder $GOPATH/src/github.com/[my-git-account] (provided by the sym-link) with golang projects, where code-generator can find them.
Faced one drawback of this trick - in the IDE navigation to the method can move to the linked source (within SDK package by the link - not to the source code, opened in the IDE).

VSCode import problem with windows API call

When importing golang.org/x/sys/windows in VSCode, I can only choose SIGDescribe, nothing else.
Hovering over the import, following errors appear.
error while importing golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows
could not import golang.org/x/sys/windows (no required module provides package "golang.org/x/sys/windows")compilerBrokenImport
The manual command go get golang.org/x/sys/windows gives the following error message
Command 'gopls.go_get_package' failed: Error: err: exit status 1: stderr: package golang.org/x/sys/windows: build constraints exclude all Go files in /home/username/go/pkg/mod/golang.org/x/sys#v0.0.0-20210630005230-0f9fa26af87c/windows .
I already re-installed Golang and updated GoTools in VSCode, no changes.
Goal: The following code below should work.
package main
import "golang.org/x/sys/windows"
func main() {
user32DLL := windows.NewLazyDLL("user32.dll")
}
OS: Ubuntu 21.04
GO Version: 1.16.6
Editor: VSCode 1.58.1
Make a folder somewhere something. Then make a file something/main.go:
package main
import "golang.org/x/sys/windows"
func main() {
println(windows.EVENTLOG_ERROR_TYPE == 1)
}
Then build:
go mod init something
go mod tidy
go build

Google cloud BigQuery undefined ValueList

I'm working with the BigQuery API on Google Cloud Platform and I've completed the Golang client library installation.
When I try to compile, I see the following errors:
[root#server ~]$ go install github.com/user/program
# github.com/user/program/handler
go/src/github.com/user/program/handler/file1.go:228: undefined:
"cloud.google.com/go/bigquery".ValueList
go/src/github.com/user/program/handler/file1.go:259: undefined:
"cloud.google.com/go/bigquery".ValueList
The offending line for 228 is:
227 for {
228 var values bigquery.ValueList
229 err := it.Next(&values)
At the top of the file I import the package, as normally
import "cloud.google.com/go/bigquery"
and have taken care to install the client library for Golang
https://cloud.google.com/bigquery/docs/reference/libraries#client-libraries-install-go
I can see that the cloud.google.com package is contained within my GOPATH and I see the ValueList type decleration inside of a file in there. It is therefore puzzling that I'm getting this error.
What's more: I can compile this fine on my Windows machine with no errors. It's only when I try to do the same thing on my CentOS VM that I run into this. Both are running go version go1.6.3.
Thank you.
#Spikey,
Hi, please make sure you've installed correct client.
Here's why:
After installing go get -u cloud.google.com/go/bigquery I see that they do not have an exported ValueList type(!). Only valuelist, i.e. unexported. Check line 39 of ~go/src/google.golang.org/cloud/bigquery/value.go
However, they do mention ValueList as an exported type in their docs(!), for example:
line 52 of ~go/src/google.golang.org/cloud/bigquery/doc.go
https://godoc.org/cloud.google.com/go/bigquery
line 164 of ~/go/src/google.golang.org/cloud/README.md
Also, ValueList is an exported type if looking directly at: https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/bigquery/value.go
So there's a confusion about installing a correct Google client.
Hope this helps!

Stringer tool complains about wrong archive header

I am trying to use go generate/stringer (golang.org/x/tools/cmd/stringer) to generate String() methods on enums. I have problems, which I believe, are because of slightly different format of .a packages on different systems. I have this file:
package main
import (
"math/rand"
)
//go:generate stringer -type=Foo
type Foo int;
const (
FooPrime Foo = iota
FooBis
)
func main() {
//Just use rand anywhere, otherwise we get a compiler error
rand.Seed(1)
}
Now if I run go generate example.go on my machine everything is all right: foo_string.go is created. However, on a test machine I get:
stringer: checking package: example.go:4:2: could not import math/rand (reading export data: /usr/lib64/go/pkg/linux_amd64/math/rand.a: go archive is missing __.PKGDEF)
Now, after some digging in the code I think that I get this error, because on my machine rand.a has the following header:
!<arch>
__.PKGDEF 0 0 0 644 2051
`
while on test machine it has the following header:
!<arch>
__.PKGDEF/ 0 399 399 100644 2051
`
I think that the crucial difference is slash after PKGDEFF. gcimporter refuses to process .a file, if it doesn't have __.PKGDEF header.
To check this, I edited manually gcimporter/exportdata.go and changed one of the line from this:
if name != "__.PKGDEF"
to this:
if name != "__.PKGDEF" && name != "__.PKGDEF\"
After this change (and compiling and installing everything) I was able to run go generate on example.go.
My questions are: why do I get this problem and how do I get rid of it (other then manually editing external library)?
What I can see from the spec for openSUSE's packaging they are disabling reinstallation of the standard library at updates. __.PKGDEF is a Go specific informational section, and some linker OpenSUSE has used has simply produced incompatible output.
There's nothing you can do except install a healthy Go from the official source.

Resources