Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to handle duplicate imports of one package? #132

Open
fasaxc opened this issue Aug 4, 2023 · 3 comments
Open

Way to handle duplicate imports of one package? #132

fasaxc opened this issue Aug 4, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@fasaxc
Copy link

fasaxc commented Aug 4, 2023

I have some files that import the same library package multiple times with different names:

import log "github.com/sirupsen/logrus"
import "github.com/sirupsen/logrus"

I was hoping to write a gopatch to clean these up but I can't seem to do it. This patch on its own

@@
var logrus identifier
@@
-import logrus "github.com/sirupsen/logrus"
+import logrusBar "github.com/sirupsen/logrus"
-logrus
+logrusBar

only matches one of the two imports and I can't seem to control which one it matches.

@lverma14
Copy link
Collaborator

Hi! Thanks for reaching out.
You should be able to clean the imports by using 2 patches
Could you try using the below patch? The first patch gets rid of the unnamed import and the second gets rids of leftover named import instances.

# get rids of the unnamed imports 
@@
var x identifier
@@
-import "github.com/sirupsen/logrus"
+import logrusBar "github.com/sirupsen/logrus"

-logrus.x
+logrusBar.x

# gets rid of the named imports instances
@@
var log, x identifier
@@
-import log "github.com/sirupsen/logrus"
+import logrusBar "github.com/sirupsen/logrus"

-log.x
+logrusBar.x

Example: test_file : link
After running gopatch: link

Please let me know, if this doesn't work, thanks!

@r-hang
Copy link

r-hang commented Aug 22, 2023

@lverma14, could a single patch UX for this be turned into a feature request ticket or is a two-patch approach suitable in similar situations?

@abhinav
Copy link
Contributor

abhinav commented Aug 22, 2023

So a detail that may not be super obvious:

@@
var logrus identifier
@@
 import logrus "github.com/sirupsen/logrus"

That matches any logrus import—named or unnamed. It records the knowledge of whether it's named or unnamed in that logrus identifier. (See also https://github.com/uber-go/gopatch/blob/main/docs/PatchesInDepth.md#best-practices-for-imports.)

Unfortunately, there's no way to control which import it matches if there are multiple.
Plus there's no way to "run" the patch multiple times for each import.

There are a couple possible feature requests to consider here:

  • A means of specifying that something should only match a named import. You can say a patch only matches an unnamed import by specifying an unnamed import.
  • Run a patch repeatedly until it makes no changes. This was considered as a patch option (e.g. @@ loop in the patch open block) or as a flag gopatch --repeat. The idea is that when a patch matches and modifies a file, it should be applied there repeatedly until it no longer makes any changes. This will help for cases like the above where you want to convert all imports of something to the same standard form.

@lverma14 lverma14 reopened this Aug 22, 2023
@lverma14 lverma14 added the enhancement New feature or request label Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

4 participants