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

Generate error-prone patches #21640

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rsalvador
Copy link
Contributor

@rsalvador rsalvador commented Mar 12, 2024

The current error-prone integration doesn't generate patches: #5729 (comment)

This PR enables error-prone patch generation and relies on making some existing error-prone members public: google/error-prone#4318

Error-prone requires the caller to specify the location of the generated error-prone.patch files: https://errorprone.info/docs/patching, callers can handle this by for example dynamically generating a javacopts with a different -XepPatchLocation for each java target rule. Another option is to use the IN_PLACE option: -XepPatchLocation:IN_PLACE

@rsalvador rsalvador requested a review from a team as a code owner March 12, 2024 08:28
@github-actions github-actions bot added team-Rules-Java Issues for Java rules awaiting-review PR is awaiting review from an assigned reviewer labels Mar 12, 2024
@sgowroji sgowroji added awaiting-user-response Awaiting a response from the author and removed awaiting-review PR is awaiting review from an assigned reviewer labels Mar 18, 2024
@rsalvador
Copy link
Contributor Author

rsalvador commented Mar 18, 2024

Hi @sgowroji, I see you changed the status to awaiting-user-response, but I don't see any question directed to me.
Note that the build failures are because this PR needs changes in error-prone: google/error-prone#4318

Copy link

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

In an amazing coincidence, I just worked on this same thing a couple of days back and ended up with a very similar approach! This code is definitely cleaner than what I did. I just had a comment on one thing I did differently

@@ -121,6 +127,9 @@ public void postFlow(Env<AttrContext> env) {
elapsed.start();
try {
errorProneAnalyzer.finished(new TaskEvent(Kind.ANALYZE, env.toplevel, env.enclClass.sym));
if (refactoringTask != null) {
refactoringTask.finished(new TaskEvent(Kind.GENERATE, env.toplevel, env.enclClass.sym));

Choose a reason for hiding this comment

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

With this call here, the refactoring task won't actually run after the GENERATE phase is completed. Assuming that it is important to run the task at that time, an alternative is to register the task as a TaskListener with the compiler. To do this, around line 115 where the RefactoringTask is created, you can do:

MultiTaskListener.instance(context).add(refactoringTask);

Then you won't need this call. Not sure if it's important or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @msridhar, I implemented your suggestion.

@sgowroji sgowroji added awaiting-review PR is awaiting review from an assigned reviewer and removed awaiting-user-response Awaiting a response from the author labels Mar 19, 2024
@sgowroji
Copy link
Member

Hi @rsalvador, Thanks for sharing details on above failures.

@msridhar
Copy link

@rsalvador have you explored how this change interacts with sandboxing for your use case? With weaker sandboxes, I found that the original source files do get modified with the IN_PLACE strategy, since symlinks are used. I haven't tested with a stronger sandbox, but I suspect the patching might just fail with IN_PLACE if the input files are viewed as read-only. In such cases patch files could be used, but then they might need to be set as an additional output of the action. Just wondering how this is working out for you all.

@rsalvador
Copy link
Contributor Author

Hi @sgowroji, the error-prone PR (google/error-prone#4318) has been in a "1 workflow awaiting approval. This workflow requires approval from a maintainer" state since it was opened last week and it has not had any activity or checks run. Do you know how to get the error-prone PR moving forward?

@rsalvador
Copy link
Contributor Author

@rsalvador have you explored how this change interacts with sandboxing for your use case? With weaker sandboxes, I found that the original source files do get modified with the IN_PLACE strategy, since symlinks are used. I haven't tested with a stronger sandbox, but I suspect the patching might just fail with IN_PLACE if the input files are viewed as read-only. In such cases patch files could be used, but then they might need to be set as an additional output of the action. Just wondering how this is working out for you all.

For local builds, we tried both IN_PLACE and dynamically generating a javacopts with a different -XepPatchLocation for each java target and both worked well, didn't try stronger sandboxing. We used it to perform a mass fixing of wildcard imports by running a full local build with patching enabled and plan to use it for similar use cases only for now.

@sgowroji
Copy link
Member

Hi @sgowroji, the error-prone PR (google/error-prone#4318) has been in a "1 workflow awaiting approval. This workflow requires approval from a maintainer" state since it was opened last week and it has not had any activity or checks run. Do you know how to get the error-prone PR moving forward?

@rsalvador we don't Triage or manage above mentioned repository.

raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Mar 28, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.java_bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=cme/clients/flipr-java-client/flipr-client tools/bazel build //cme/clients/flipr-java-client/flipr-client:src_main
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Mar 28, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Mar 29, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Mar 30, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Mar 30, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Apr 23, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Apr 23, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
raviagarwal7 pushed a commit to uber-common/bazel that referenced this pull request Apr 23, 2024
Summary:
This diff contains the changes in bazelbuild#21640, ported to the uber/java/7.0.2.001 branch.  We also commit a modified version of the Error Prone check_api jar.  The modifications in that jar are those changes in google/error-prone#4318 applied to Error Prone 2.22.0 (the version used in our branch).  Probably, rather than committing the modified jar, we could upload the modified jar to artifactory and change the version we depend on here.

I have tested that these changes work locally on a devpod.  `IN_PLACE` patching will most likely not work under stronger sandboxes, as it relies on modifying the source files in the sandbox (which, by default on a devpod, are just symlinks to the original source files).

Also, there seem to be some weird interactions with the Bazel cache, such that when I tried to run the same autopatch twice (after undoing the initial changes), I had to blow away my local `~/.bazelcache` directory to make it work.

Test Plan:
Tested locally by copying over the built java_tools jars and running:

```
EP_PATCH_CHECK=MissingOverride EP_PATCH_CHECK_PATH_ONLY=a/b/c tools/bazel build //a/b/c:src
```

It would be good to write an integration test for this at some point, since it hasn't been upstreamed.

Reviewers: cjk

Subscribers: ravirajj, ravi

Revert Plan: n/a

JIRA Issues: JAVADEVX-8326

Differential Revision: https://code.uberinternal.com/D13309409
@cushon
Copy link
Contributor

cushon commented May 3, 2024

Just adding support for -XepPatchLocation: doesn't seem ideal for Bazel, because as mentioned earlier IN_PLACE doesn't work with sandboxing, and setting a specific location doesn't make Bazel aware of those outputs, so it requires something like running the action locally and writing them outside the output tree.

One alternative would be to add support for this to Bazel, so it could handle setting up the additional output for the action and passing that path through to JavaBuilder.

At Google we're generating Error Prone patches by using an aspect to create an additional action to run Error Prone for refactoring (instead of re-using the same invocation that happens in JavaBuilder), and having the aspect handle collecting all of the transitive refactoring outputs.

@rsalvador
Copy link
Contributor Author

Just adding support for -XepPatchLocation: doesn't seem ideal for Bazel, because as mentioned earlier IN_PLACE doesn't work with sandboxing, and setting a specific location doesn't make Bazel aware of those outputs, so it requires something like running the action locally and writing them outside the output tree.

One alternative would be to add support for this to Bazel, so it could handle setting up the additional output for the action and passing that path through to JavaBuilder.

Another option could be to use a wrapper rule for java_library that declares the output.

Agreed that the way this PR allows patching is not ideal, but modifying the native bazel rules seemed too involved, maybe it will be better to do once the java rules are refactored to Starlark?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review PR is awaiting review from an assigned reviewer team-Rules-Java Issues for Java rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants