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

hygienic BUILD file modifications (like buildozer) with libCST #9434

Conversation

cosmicexplorer
Copy link
Contributor

TODO: add testing!

Problem

buildozer is a fantastic tool that hygeinically rewrites bazel BUILD files. We would like to do the same, but with a couple caveats (I made these up, so feel free to disagree!):

  1. We want to be able to write BUILD file transformations in Python, making use of target information, not just the parsed BUILD file contents.
  2. We would like to be able to observe and validate changes before they're made, interactively.

Solution

  • Use instagram's libCST to parse BUILD files.
  • Map Addresses from BUILD files to the libCST node that represents the named target.
  • Create a set of @rules which implement about 10% of buildozer commands by interacting with libCST.

Result

(describe how your changes affect the end-user behavior of the system. this section is
optional, and should generally be summarized in the title of the pull request.
)

Example commands when combined with #7356 and #8450:

  • rlwrap ./pants -ldebug --v2 --no-v1 --no-v2-ui --query="owner_of('src/python/pants/util/strutil.py')" --query="type_filter('python_library')" --query='minimize()' --query='noop()' buildozer2 --args='add sources "tarutil.py"'
  • rlwrap ./pants --v2 --no-v1 --no-v2-ui --query="owner_of('src/python/pants/util/strutil.py')" --query="type_filter('python_library')" --query='minimize()' --query='noop()' buildozer2 --args='add sources "tarutil.py"' --args='print name'
  • ./pants binary examples/src/scala/org/pantsbuild/example/build_graph:find-orphaned-targets && ./pants --query="type_filter('python_library')" list --output-format=json src/python/pants/util: | java -jar ./dist/find-orphaned-targets.jar | ./pants --spec-file=/dev/stdin buildozer2 --args="add tags 'orphaned'"

…T!!!

save progress

[ci skip-jvm-tests]  # No JVM changes made.

add basic @goal_rule!!!

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

make a v2 repl!!!

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

add a HashableDict and remove a ton of code

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

fixes after rebase

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

make it work a lot more

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

make parsing BUILD files for targets work!!!

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

attempt to write down buildozer commands!!!

[ci skip-jvm-tests]  # No JVM changes made.

refactor BUILD file object matching

[ci skip-jvm-tests]  # No JVM changes made.

refactor object matching again

[ci skip-jvm-tests]  # No JVM changes made.

the following command works!! (add to list attr)

rlwrap ./pants -ldebug --v2 --no-v1 --no-v2-ui --query="owner_of('src/python/pants/util/strutil.py')" --query="type_filter('python_library')" --query='minimize()' --query='noop()' buildozer2 --args='add sources "tarutil.py"'

[ci skip-jvm-tests]  # No JVM changes made.

getting and modifying data is easy now!!!

this command totally works!!!

rlwrap ./pants --v2 --no-v1 --no-v2-ui --query="owner_of('src/python/pants/util/strutil.py')" --query="type_filter('python_library')" --query='minimize()' --query='noop()' buildozer2 --args='add sources "tarutil.py"' --args='print name'

[ci skip-jvm-tests]  # No JVM changes made.

refactor the AddValuesToList part

[ci skip-jvm-tests]  # No JVM changes made.

add schema.py!

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

everything appears to be working??? including this command:

./pants binary examples/src/scala/org/pantsbuild/example/build_graph:find-orphaned-targets && ./pants --query="type_filter('python_library')" list --output-format=json src/python/pants/util: | java -jar ./dist/find-orphaned-targets.jar | ./pants --spec-file=/dev/stdin buildozer2 --args="add tags 'orphaned'"

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.

additional fixes after rebase

[ci skip-jvm-tests]  # No JVM changes made.

[ci skip-jvm-tests]  # No JVM changes made.
@@ -61,127 +61,3 @@ def root_dir(self):
def registered_aliases(self):
"""Returns a copy of the registered build file aliases this build file parser uses."""
return self._build_configuration.registered_aliases()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this is all dead code. It was dead code before this PR, too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please break this out into a dedicated PR?

This change looks incredible, although the size of the diff makes it a little overwhelming to review. It’d be hugely helpful to break out any possible prework PRs like this so that this PR is smaller.

@cosmicexplorer cosmicexplorer changed the title hygenic BUILD file modifications (like buildozer) with libCST hygeinic BUILD file modifications (like buildozer) with libCST Mar 31, 2020
@cosmicexplorer cosmicexplorer changed the title hygeinic BUILD file modifications (like buildozer) with libCST hygienic BUILD file modifications (like buildozer) with libCST Mar 31, 2020

@frozen_after_init
@dataclass(unsafe_hash=True)
class HashableDict(Generic[_K, _V]):
Copy link
Contributor

Choose a reason for hiding this comment

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

See FrozenDict from Pants.util.frozendict. I think it does what you’re after here.

@Eric-Arellano
Copy link
Contributor

Closing as stale, which we're doing for all changes that haven't been touched in 1+ years to simplify project management.

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.

2 participants