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

fix: Force import alias when package ends in /v[0-9]+$ #409

Merged
merged 2 commits into from
Feb 1, 2023

Conversation

bmoylan
Copy link
Contributor

@bmoylan bmoylan commented Feb 1, 2023

This is a more targetted fix for #405 than #406 reverted in #408.

Rather than operate on the generated files, we predict invalid package names and force an import alias. I've added a second test type to demonstrate how jennifer deconflicts aliases.


This change is Reviewable

@bmoylan bmoylan marked this pull request as ready for review February 1, 2023 17:22
@bmoylan bmoylan requested a review from nmiyake February 1, 2023 17:24
Copy link
Contributor

@nmiyake nmiyake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great!

The other scenario that seems to be special-cased in the Go code is packages that start with go-, but I don't think that's a concern/case that we need to handle for Conjure since I'm not sure that packages are allowed to have a hyphen in the name (and even if they do, we probably have some safety from a practical perspective in that such package names should likely be rare).

@@ -129,3 +134,7 @@ func newGoFile(filePath string, file *jen.File) *OutputFile {
file: file,
}
}

func packageSuffixRequiresAlias(importPath string) bool {
return regexp.MustCompile(`/v[0-9]+$`).MatchString(importPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks! The implementation the Go team uses for this is at https://github.com/palantir/go-ptimports/blob/93cb0022ccc2ca0a21b3ac3396b85349567cc40a/vendor/golang.org/x/tools/internal/imports/fix.go#L1145-L1153, but I think this regexp matching is more readable and fine.

@bulldozer-bot bulldozer-bot bot merged commit 201c9c7 into develop Feb 1, 2023
@bulldozer-bot bulldozer-bot bot deleted the bm/versioned-packages branch February 1, 2023 17:45
@svc-autorelease
Copy link
Collaborator

Released 6.38.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants