-
-
Notifications
You must be signed in to change notification settings - Fork 611
fix(gazelle): Do not resolve absolute imports to sibling modules #3106
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(gazelle): Do not resolve absolute imports to sibling modules #3106
Conversation
converting the import to an appropriate absolute import)
Thanks for the PR. Since the current gazelle code would result in un-runnable code, is there a good reason to not default to the correct behaviour and have the flag in case some user wants to do this step-wise? |
Honestly, my main reason is a feeling of "I must be missing something" about it. The sibling import behavior is not accidental: there's code added that only exists for resolving sibling imports, a test that explicitly tests for this behavior, and multiple other tests that implicitly depend on it. And, as far as I know, it was all written much after python2 was unsupported, which is the only reason that I can think of for why this would be desired. That said, I'd be happy to switch the default if you believe that would be ok. |
I'll wait for other to chime in on here. @arrdem, your insight from the Aspect.dev point of view would be valuable here as well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran this branch against our QuantumIA code base with the directive set to true
and false
.
When false
, there were some changes made. I checked them all and they were indeed because the original behavior was incorrect - foo.py
importing math
but Gazelle thinks its the sibling math.py
.
This is super nice and caught some issues for us, thank you!
is there a good reason to not default to the correct behaviour and have the flag in case some user wants to do this step-wise?
I think one argument against is just subverting expectations. People are probably used to the current behavior and adjusting the current behavior would be disruptive.
The current behavior is not necessarily broken in that it always causes bazel build
to fail, but it does mean that targets have more dependencies than strictly required. Those additional deps can cause circular dependencies, especially in file
generation mode.
Thus my vote is to adjust the default to the correct behavior: python_resolve_sibling_imports false
.
I'm +1 for keeping the default and creating a ticket to switch the behaviour when we release 2.0. |
Changes since the initial PR request:
|
Oooo sounds like we're about to fight! Nah I'm kidding of course. I definitely do not feel strongly about adjusting the default to the correct behavior. I'm at like a 3 out of 10. So it'll be pretty easy to convince me otherwise. |
My understanding of the documented policy is that, if this is considered a breaking change but "is not too disruptive" (which I think this would fit), then we want to:
|
Ah sorry... :D I misunderstood/misread/did-not-read properly:
+1 to go with your suggestion. Gazelle does not have the same stability guarantees as rules_python core. This breaking change should not be too disruptive the way I see it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, new tests LGTM.
I verified the most recent commit a00ec81 on our repo. All 👍 🎉
Currently, gazelle allows absolute imports to be resolved to sibling modules: an
import foo
statement will resolve to afoo.py
file in the same folder if such file exists. This seems to be a Python 2 behavior (ie. pre-from __future__ import absolute_import
), and doesn't work on the current rules_python setup.This behavior is explicitly tested in the siblings_import test case. However, recreating the exact same repository layout from this test case and running
bazel test //pkg:unit_test
, the test fails with the import failing.This PR adds a new directive,
gazelle:python_resolve_sibling_imports
, to allow disabling such behavior.The actual changes are in 3 places:
gazelle/python/target.go
, the directive is added toif t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath)
, which is where the import is converted to a full absolute import if it matches a sibling file;gazelle/python/generate.go
, the handling ofconftest.py
was dependent on this behavior (ie. it added a dependency on the moduleconftest
, assuming that it would be resolved to the relative module). That was modified to compute the full absolute module path instead.gazelle/python/resolve.go
, resolve relative imports even when using file generation mode.I also explicitly added
gazelle:python_resolve_sibling_imports true
to any test that breaks if the default value of this directive is changed tofalse
.