Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle case sensitive import collisions

Tags:

go

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

like image 955
jim Avatar asked Mar 10 '23 06:03

jim


1 Answers

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.

like image 55
Nevermore Avatar answered Mar 14 '23 19:03

Nevermore