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: address module imports that end with .go #487

Merged
merged 0 commits into from
Jun 29, 2022

Conversation

meshuga
Copy link
Contributor

@meshuga meshuga commented Jun 27, 2022

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Version of Golang used when building/testing:

  • 1.11
  • 1.12
  • 1.13
  • 1.14
  • 1.15
  • 1.16
  • 1.17
  • 1.18

How Has This Been Tested?

See TestGetLocalizedPathVendored and TestGetLocalizedPathModule in generator_test.go

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

pkg/generator.go Outdated
@@ -194,7 +194,7 @@ func (g *Generator) getLocalizedPath(ctx context.Context, path string) string {
log := zerolog.Ctx(ctx).With().Str(logging.LogKeyPath, path).Logger()
ctx = log.WithContext(ctx)

if strings.HasSuffix(path, ".go") {
if strings.HasSuffix(path, ".go") && strings.Contains(path, "vendor"+string(filepath.Separator)) {
Copy link
Collaborator

@LandonTClipp LandonTClipp Jun 27, 2022

Choose a reason for hiding this comment

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

Could you explain this a little more? Doesn't this break the caching logic if you have any local .go file that isn't under a vendor directory?

Copy link
Contributor Author

@meshuga meshuga Jun 27, 2022

Choose a reason for hiding this comment

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

Good point! I think we might break the logic in this case. It looks like I'd need to refactor the logic to ensure the path we want to preserve is only a directory and nothing else. I think we can look at the import struct we have to check

result := generator.getLocalizedPath(context.Background(), "vendor/github.com/nats-io/nats.go/js.go")

s.Equal(
"github.com/nats-io/nats.go", result,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you're vendoring a directory don't you want the returned path to be on-disk path instead of the URL?

Copy link
Contributor Author

@meshuga meshuga Jun 27, 2022

Choose a reason for hiding this comment

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

In golang code, you put imports to a vendored directory in same way as from modules, that's why this current logic is in place.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, I've never personally used vendored dependencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I used to. It's just a directory with all project dependencies in a form of a source code. In any case, we ought to check if a path element is a directory or not and if it's a file, remove it from path to build correct import statement.

@LandonTClipp
Copy link
Collaborator

@meshuga thanks for the contribution. I have a couple of comments/questions. Also, I'm adding table-driven tests for getLocalizedPath in #488. Could you rebase off of this once I merge it and add your test there? Thanks!

@tephromancy tephromancy force-pushed the master branch 2 times, most recently from 7bc5fef to 2ca0b83 Compare June 28, 2022 15:00
@LandonTClipp LandonTClipp merged commit de0cade into vektra:master Jun 29, 2022
@meshuga
Copy link
Contributor Author

meshuga commented Jun 30, 2022

@LandonTClipp I think you merged the PR too early, The PR didn't have any changes. I was digging a little bit deeper in the code to understand how imports are generated, taking more time than expected :)

@LandonTClipp
Copy link
Collaborator

That's really odd I didn't intend to merge this. I must have fat fingered something! Sorry about that.

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

Successfully merging this pull request may close these issues.

Packages with . in them are incorrectly imported in mocks
2 participants