Add a dry-run option to bazel mod tidy#25540
Add a dry-run option to bazel mod tidy#25540numine777 wants to merge 3 commits intobazelbuild:masterfrom
Conversation
* Allow bazel mod tidy to take a `write` option that determines whether fixes are made to files in place -> Defaults to true
| private static BlazeCommandResult reportAndCreateTidyDryRunResult( | ||
| CommandEnvironment env, BazelModTidyValue modTidyValue) { | ||
| if (modTidyValue.fixups().isEmpty()) { | ||
| return BlazeCommandResult.success(); |
There was a problem hiding this comment.
The second part of my comment from #25246 (comment) still applies. As is, this might pass even though bazel mod tidy would have made changes due to formatting. I think that you need to invoke buildozer in each case and use it as the source of truth for whether it changed a file.
There was a problem hiding this comment.
Do you mean to say here that we may end up with false positives in cases where MODULE.bazel file(s) need to be formatted rather than actually having errors? Or are you pointing out that this implementation might return a success case even though bazel mod tidy would have made formatting only changes?
There was a problem hiding this comment.
The latter. If all use_repo arguments are corrected but the module file is badly formatted, this would return success but performing a non-dry run would change the module file.
There was a problem hiding this comment.
Is that something that we feel a need to include in dry-run? For instance, it's fairly straightforward to use buildifier to capture formatting errors, but nothing except this tool detects problems in use_repo calls. If we do want to include formatting, is the proposed flow that we call buildozer with stdout and pipe that output to a function to determine formatting needs?
There was a problem hiding this comment.
Yes, see #25246 (comment). I think that we do need to capture stdout.
There was a problem hiding this comment.
Implemented! Happy to hear constructive feedback
| .setWorkingDir(env.getWorkspace()) | ||
| .addArg(modTidyValue.buildozer().getPathString()) | ||
| .addArg("-f") | ||
| .addArg("-") |
There was a problem hiding this comment.
Could we share the common part of the construction of the builder between the branches?
| .get(); | ||
| .get() | ||
| .getStdout(); | ||
| return reportAndCreateTidyDryRunResult(env, modTidyValue, new String(out)); |
There was a problem hiding this comment.
Instead of comparing stdout for differences, which requires reading the module files, could you check stderr for fixed <path> lines? https://github.com/bazelbuild/buildtools/blob/733b4a20350d044edbef66af669b1793cf8347e3/edit/buildozer.go#L1502
| fixup.usage().getExtensionBzlFile() + "%" + fixup.usage().getExtensionName(); | ||
| return fixup.usage().getProxies().stream() | ||
| .map(p -> String.format(" %s: %s", p.getLocation().toString(), extensionId)) | ||
| .collect(joining("\n")); |
There was a problem hiding this comment.
Instead of collecting here and then collecting again in the outer stream, use flatMap.
| return fixup.usage().getProxies().stream() | ||
| .map(p -> String.format(" %s: %s", p.getLocation().toString(), extensionId)) | ||
| .collect(joining("\n")); | ||
| }).collect(joining("\n"))) + "\n"; |
There was a problem hiding this comment.
joining has an overload that accepts a suffix.
|
+1 to get this landed |
|
I sent bazelbuild/buildtools#1373 to make it easier to implement this feature. Let's wait for that to land as well as a release, then our job here will become much easier. Based on that change, I was able to simplify the current PR: https://github.com/bazelbuild/bazel/compare/master...fmeum:bazel:fork/numine777/feature/add-mody-tidy-nowrite?expand=1 |
|
@fmeum Thanks much! I apologize for letting this get stale. My team pivoted back to using WORKSPACE for the time being, so the demand hasn't been as strong on our end |
|
Don't worry, thanks for kicking this off! |
|
@bazel-io fork 7.7.0 |
|
Any update on getting this landed? |
|
This needs bazelbuild/buildtools#1373, which has been rolled back |
|
Thank you for contributing to the Bazel repository! This pull request has been marked as stale since it has not had any activity in the last 30 days. It will be closed in the next 30 days unless any other activity occurs. If you think this PR is still relevant and should stay open, please post any comment here and the PR will no longer be marked as stale. |
|
This would be great to have! |
This PR addresses issue #24263
We needed this change for our CI as we are migrating to bzlmod. This PR now addresses the master branch to make the patch as useful as possible.
Please let me know if this PR needs any additional updates to documentation or testing before it is viable. I merely saw that the issue was tagged as a good first issue and thought I would jump in!