How to handle case sensitive import collisions
Asked Answered
C

3

8

I am using a third party library in GoLang that has previously had import paths in different case. Initially a letter was lower case then the author changed it to uppercase.

Some plugin authors updated their libraries and others didn't. In the meantime the original library author reverted the case change.

Now I find myself in a state where my application won't build due to to case import collisions.

How can one go about fixing this?

Many thanks

Chak answered 25/4, 2017 at 18:59 Comment(1)
Edit your source files to match the current state of things? That's what I've always done. Curious to see if there's a better answer.Epidote
N
7

You could vendor the dependencies, and then go into the vendor/ directory and manually change (try greping or seding the dependency), the dependencies.

For an introduction to vendoring, try here, https://blog.gopheracademy.com/advent-2015/vendor-folder/

The original repo can still live in your GOPATH, but the 'corrected' version can go in the vendor folder, in which the compiler will look first when linking dependencies.

There are many tools for vendoring, I use govendor

Edit

As mkopriva mentions in the comments, you can refactor import names using the gofmt tool:

gofmt -w -r '"path/to/PackageName" -> "path/to/packagename"' ./

gofmt -w -r 'PackageName.x -> packagename.x' ./

The lowercase single character identifier is a wildcard.

from the docs

The rewrite rule specified with the -r flag must be a string of the form:

pattern -> replacement

Both pattern and replacement must be valid Go expressions. In the pattern, single-character lowercase identifiers serve as wildcards matching arbitrary sub-expressions; those expressions will be substituted for the same identifiers in the replacement.

Nessa answered 25/4, 2017 at 19:18 Comment(1)
As an alternative to grep one can also use the gofmt tool. E.g. for changing the imports gofmt -w -r '"path/to/PackageName" -> "path/to/packagename"' ./ and for changing the selector expressions on that package gofmt -w -r 'PackageName.x -> packagename.x' ./Superposition
T
1

Just if anyone wonders why this error might occur in your project: Make sure all imports use either lowercase or uppercase path, but not mixed. So like this:

one.go -> "github.com/name/app/login"
another.go -> "github.com/name/app/login"

And not like this:

one.go -> "github.com/name/app/Login"
another.go -> "github.com/name/app/login"
Tiaratibbetts answered 12/7, 2021 at 21:48 Comment(0)
N
-1

On Windows, first rename folder names involved in the import path to match the package name defined in .go file.

If issue still persists, try renaming the folder to something else and then rename back to correct one to overwrite any Cache issue. This method worked for me.

Natal answered 1/9 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.