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

Handle from foo import * wildcard imports in Rust dep inference parser #19249

Merged
merged 6 commits into from
Jun 6, 2023

Conversation

huonw
Copy link
Contributor

@huonw huonw commented Jun 5, 2023

This fixes #19248 by implementing explicit handling for * wildcard imports in the Rust-based Python dependency inference parser.

The commits are somewhat individually reviewable:

  1. switching how the insert_import arguments are consumed from the syntax tree, for from ... import ... statements in particular:
    • before: name and module_name correspond to import $name, "$name", from $module_name import $name
    • after: base and imported correspond to import $base, "$base", from $base import $imported
    • This is in particular flipping the last one, and changes whether the from ... part is the optional arg (before) or not (after).
    • The motivation is that there's more functional similarities between the from ... part and a plain import ... statement, than between the import ... part and a plain import ... statement, despite the superficial similarities of it! (from foo import bar is a little like import foo as __secret; bar = __secret.bar.)
  2. Add some tests that fail
  3. Fix the bug
  4. (and others) some tweaks, including defence-in-depth against similar problems happening in future

@huonw huonw added needs-cherrypick category:internal CI, fixes for not-yet-released features, etc. labels Jun 5, 2023
@huonw huonw added this to the 2.17.x milestone Jun 5, 2023
@huonw huonw marked this pull request as ready for review June 5, 2023 06:51
@huonw huonw requested a review from thejcannon June 5, 2023 07:37
@thejcannon
Copy link
Member

You're going to get bit by #18961 if you haven't already.

I was thinking of computing a hash of the files during the build step and using that. Otherwise we need to manually bump the (not-yet-added) version.

Thoughts?

Copy link
Member

@thejcannon thejcannon left a comment

Choose a reason for hiding this comment

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

I'll take a close look today or tomorrow.

Thanks for this!

src/rust/engine/dep_inference/src/python/mod.rs Outdated Show resolved Hide resolved
@huonw
Copy link
Contributor Author

huonw commented Jun 5, 2023

I've pushed a few changes, including a defensive one to hopefully make problems like this more obvious in future: make sure there's always some sort of import inserted every time a from .. import .. statement is visited, because inserting nothing is "obviously" wrong. In particular, it's particularly dangerous that inference failed silently here, and I only identified it because tests failed when running 2.17.0rc0 in our CI, rather than getting noisy dep inference failure warnings when running simple commands locally.

You're going to get bit by #18961 if you haven't already.

Thanks, I got bit by that for testing #19173 😅

(On that note, I have a suspicion that --no-local-cache might not work for this? Even with that flag I think I was getting cached results)

I was thinking of computing a hash of the files during the build step and using that. Otherwise we need to manually bump the (not-yet-added) version.

Thoughts?

Either seems fine to me, both have trade-offs:

  • manual version: 'precise' (e.g. doesn't change for no-functionality-change edits, like reformatting or adding comments), but potential to forget to change
  • hash: potentially awkward to correctly hash the right things (all relevant files and all dependency versions, including down to tree sitter (relates to Expose the hash of a given target #8445)), and changes spuriously more often

On balance, I guess hashing is probably better for now: this runs fast enough that (for most users) rerunning when upgrading pants versions is probably unnoticeable compared to all the other work that happens?

@huonw huonw requested a review from thejcannon June 5, 2023 23:48
@huonw huonw merged commit ea97fa8 into pantsbuild:main Jun 6, 2023
@huonw huonw deleted the bugfix/19248-globs branch June 6, 2023 01:19
github-actions bot pushed a commit that referenced this pull request Jun 6, 2023
…ser (#19249)

This fixes #19248 by implementing explicit handling for `*` wildcard
imports in the Rust-based Python dependency inference parser.

The commits are somewhat individually reviewable:

1. switching how the `insert_import` arguments are consumed from the
syntax tree, for `from ... import ...` statements in particular:
   - before: `name` and `module_name` correspond to `import $name`,
     `"$name"`, `from $module_name import $name`
   - after: `base` and `imported` correspond to `import $base`, `"$base"`,
     `from $base import $imported`
   - This is in particular flipping the last one, and changes whether the
     `from ...` part is the optional arg (before) or not (after).
   - The motivation is that there's more functional similarities between
     the `from ...` part and a plain `import ...` statement, than between the
     `import ...` part and a plain `import ...` statement, despite the
     superficial similarities of it! (`from foo import bar` is a little like
     `import foo as __secret; bar = __secret.bar`.)
2. Add some tests that fail
3. Fix the bug
4. (and others) some tweaks, including defence-in-depth against similar
    problems happening in future
huonw pushed a commit that referenced this pull request Jun 6, 2023
…ser (Cherry-pick of #19249) (#19255)

This fixes #19248 by implementing explicit handling for `*` wildcard
imports in the Rust-based Python dependency inference parser.

The commits are somewhat individually reviewable:

1. switching how the `insert_import` arguments are consumed from the
syntax tree, for `from ... import ...` statements in particular:
   - before: `name` and `module_name` correspond to `import $name`,
     `"$name"`, `from $module_name import $name`
   - after: `base` and `imported` correspond to `import $base`, `"$base"`,
     `from $base import $imported`
   - This is in particular flipping the last one, and changes whether the
     `from ...` part is the optional arg (before) or not (after).
   - The motivation is that there's more functional similarities between
     the `from ...` part and a plain `import ...` statement, than between the
     `import ...` part and a plain `import ...` statement, despite the
     superficial similarities of it! (`from foo import bar` is a little like
     `import foo as __secret; bar = __secret.bar`.)
2. Add some tests that fail
3. Fix the bug
4. (and others) some tweaks, including defence-in-depth against similar
    problems happening in future
@benjyw
Copy link
Contributor

benjyw commented Jun 8, 2023

@huonw For future reference, this should likely have been labeled category:bugfix

@benjyw benjyw added category:bugfix Bug fixes for released features and removed category:internal CI, fixes for not-yet-released features, etc. labels Jun 8, 2023
@thejcannon
Copy link
Member

I think there's a debate on that, right? Because it fixes a bug that was never released 🤔

@benjyw
Copy link
Contributor

benjyw commented Jun 8, 2023

Fair point.

@kaos
Copy link
Member

kaos commented Jun 8, 2023

I think there's a debate on that, right? Because it fixes a bug that was never released 🤔

Oh, I missed that. Would be good to have that on the PR in case it closes a bug while being labeled internal to avoid future confusion. :)

huonw added a commit that referenced this pull request Jun 14, 2023
This updates `changelog.py` to write the (non-internal) changes to the
relevant release notes file directly, rather than requiring a human to
copy-paste it in. For now, the file is just mutated, without committing.
The internal changes are still printed for the human to deal with.

For instance, `pants run src/python/pants_release/changelog.py --
--prior 2.18.0.dev1 --new 2.18.0.dev2` adds a new section to
`src/python/pants/notes/2.18.x.md`:

```diff
diff --git a/src/python/pants/notes/2.18.x.md b/src/python/pants/notes/2.18.x.md
index e648a4525c..d6668a24b1 100644
--- a/src/python/pants/notes/2.18.x.md
+++ b/src/python/pants/notes/2.18.x.md
@@ -1,5 +1,35 @@
 # 2.18.x Release Series
 
+## 2.18.0.dev2 (Jun 14, 2023)
+
+### New Features
+
+* Include complete platforms for FaaS environments for more reliable building ([#19253](#19253))
+
+* Add experimental support for Rustfmt ([#18842](#18842))
+
+* Helm deployment chart field ([#19234](#19234))
+
+### Plugin API Changes
+
+* Replace `include_special_cased_deps` flag with `should_traverse_deps_predicate` ([#19272](#19272))
+
+### Bug Fixes
+
+* Raise an error if isort can't read a config file ([#19294](#19294))
+
+* Improve handling of additional files in Helm unit tests ([#19263](#19263))
+
+* Add taplo to the release ([#19258](#19258))
+
+* Handle `from foo import *` wildcard imports in Rust dep inference parser ([#19249](#19249))
+
+* Support usage of `scala_artifact` addresses in `scalac_plugin` targets ([#19205](#19205))
+
+### Performance
+
+* `scandir` returns `Stat`s relative to its directory. ([#19246](#19246))
+
 ## 2.18.0.dev1 (Jun 02, 2023)
 
 ### New Features
```

This also moves it into the new `pants_release` root, adds some basic
tests, and has it fetch the relevant branch directly, rather than
prompting the user to do so. It also pulls out a `git.py` support module
with helpers for exec-ing `git` as an external process.

The commits are individually reviewable.

This is a step towards automating more of the "start release" process,
per #19279. After this
core refactoring of the functionality, I think the next step is to
combine it with the CONTRIBUTORS update and version bumping, and then
after that string it all together on CI so that starting a release is
fully automated.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:bugfix Bug fixes for released features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

from ..foo import * glob imports are silently ignored by Rust parser
5 participants