Skip to content

Add a dry-run option to bazel mod tidy#25540

Open
numine777 wants to merge 3 commits intobazelbuild:masterfrom
numine777:feature/add-mody-tidy-nowrite
Open

Add a dry-run option to bazel mod tidy#25540
numine777 wants to merge 3 commits intobazelbuild:masterfrom
numine777:feature/add-mody-tidy-nowrite

Conversation

@numine777
Copy link

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!

* Allow bazel mod tidy to take a `write` option that determines whether
fixes are made to files in place
  -> Defaults to true
@github-actions github-actions bot added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Mar 12, 2025
private static BlazeCommandResult reportAndCreateTidyDryRunResult(
CommandEnvironment env, BazelModTidyValue modTidyValue) {
if (modTidyValue.fixups().isEmpty()) {
return BlazeCommandResult.success();
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Author

Choose a reason for hiding this comment

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

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Author

Choose a reason for hiding this comment

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

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, see #25246 (comment). I think that we do need to capture stdout.

Copy link
Author

Choose a reason for hiding this comment

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

Implemented! Happy to hear constructive feedback

.setWorkingDir(env.getWorkspace())
.addArg(modTidyValue.buildozer().getPathString())
.addArg("-f")
.addArg("-")
Copy link
Collaborator

Choose a reason for hiding this comment

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

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));
Copy link
Collaborator

Choose a reason for hiding this comment

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

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"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

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";
Copy link
Collaborator

Choose a reason for hiding this comment

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

joining has an overload that accepts a suffix.

JIRA Tracking ID:

Description:

Test Notes:

Release Notes:
@dws
Copy link
Contributor

dws commented Jun 13, 2025

+1 to get this landed

@fmeum
Copy link
Collaborator

fmeum commented Jun 21, 2025

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

@numine777
Copy link
Author

@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

@fmeum
Copy link
Collaborator

fmeum commented Jun 21, 2025

Don't worry, thanks for kicking this off!

@meteorcloudy meteorcloudy added awaiting-user-response Awaiting a response from the author and removed awaiting-review PR is awaiting review from an assigned reviewer labels Sep 9, 2025
@iancha1992
Copy link
Member

@bazel-io fork 7.7.0

@dws
Copy link
Contributor

dws commented Oct 26, 2025

Any update on getting this landed?

@fmeum
Copy link
Collaborator

fmeum commented Oct 26, 2025

This needs bazelbuild/buildtools#1373, which has been rolled back

@github-actions
Copy link

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.

@github-actions github-actions bot added the stale Issues or PRs that are stale (no activity for 30 days) label Feb 18, 2026
@archshift
Copy link

This would be great to have!

@iancha1992 iancha1992 added not stale Issues or PRs that are inactive but not considered stale and removed stale Issues or PRs that are stale (no activity for 30 days) labels Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-user-response Awaiting a response from the author not stale Issues or PRs that are inactive but not considered stale team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants