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

Add bazel mod tidy #20483

Closed
wants to merge 1 commit into from
Closed

Add bazel mod tidy #20483

wants to merge 1 commit into from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Dec 9, 2023

Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new bazel mod tidy subcommand automatically updates use_repo calls in the MODULE.bazel file for extensions that use module_ctx.extension_metadata.

@fmeum fmeum force-pushed the bazel-mod-fix branch 10 times, most recently from ebabd9e to 389e53c Compare December 13, 2023 18:32
@fmeum fmeum marked this pull request as ready for review December 13, 2023 19:19
@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 Dec 13, 2023
@fmeum
Copy link
Collaborator Author

fmeum commented Dec 13, 2023

@aherrmann @cgrindel in case you want to give this an early try

The CI failure does appear to be a flake.

@aherrmann
Copy link
Contributor

@fmeum Thanks! I gave it a quick shot on rules_nixpkgs_core and it seems to be working there. (I tested removing a use_repo entry and it regenerated it)

@fmeum fmeum force-pushed the bazel-mod-fix branch 2 times, most recently from f3fc1fe to 8039efa Compare January 27, 2024 23:55
@fmeum fmeum assigned fmeum and unassigned fmeum Jan 27, 2024
@fmeum
Copy link
Collaborator Author

fmeum commented Jan 28, 2024

@meteorcloudy @Wyverald I resolved all conflicts and also fixed a bug in the buildozer module that resulted in a platform-dependent lockfile entry.

.equals(FailureDetails.ExternalDeps.Code.INVALID_EXTENSION_IMPORT)) {
throw new BazelModTidyFunctionException(e, SkyFunctionException.Transience.PERSISTENT);
}
// This is an error bazel mod tidy can fix, so don't fail.
Copy link
Member

Choose a reason for hiding this comment

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

does this actually work? last time I checked, we can't actually recover from errors in any Skyframe node evaluation. So even if we don't throw anything here, the evaluation would end in error (presumably at ModCommand.java:234)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As far as I understand this, we can't recover in a SkyFunction, but here we perform a top-level evaluation (implicitly in keep going mode: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/skyframe/SkyframeExecutor.java;l=2262;drc=0e1dd2afc8dfe69033abbc7852bb23bd0a9237f6). The individual values will still be marked as failed, but we should be able to extract the individual exceptions in this way. There is a test case covering this case, but if I misunderstood how this works it might of course not catch every issue due to the inherent non-determinism of Skyframe evaluation order.

Copy link
Member

Choose a reason for hiding this comment

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

ah! I think the 'implicitly in keep-going mode' bit is crucial. But it still surprises me a bit that the whole evaluation doesn't end up in error (even though we can go through all the failed nodes). Anyhow, this is definitely not blocking the whole PR, especially as you do have a test case

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But it still surprises me a bit that the whole evaluation doesn't end up in error (even though we can go through all the failed nodes).

You are right, that is indeed surprising. Maybe this is because the nodes we actually requested did not fail themselves and we are in "keep going" mode? It would be nice to get confirmation on this.

}

if (modTidyValue.lockfileMode().equals(LockfileMode.UPDATE)) {
// We cannot safely rerun Skyframe evaluation here to pick up the updated module file.
Copy link
Member

Choose a reason for hiding this comment

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

this part seems rather tricky so I'd just like to flag it so I can think about it more. My immediate reaction was that we should just trigger whatever mechanism we have for detecting diffs/dirtiness (there seem to be quite a few of those... DiffAwareness, DirtinessChecker, etc). But some half-hearted digging revealed that it might be even hackier than what you're doing here. So I'd like to come back later with a clearer head and review this part more carefully.

Copy link
Collaborator Author

@fmeum fmeum Jan 30, 2024

Choose a reason for hiding this comment

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

I went with this approach based on @lberki's comments on the design doc. I don't know whether triggering any of the diff awareness logic directly could help work around the various file system caches.

Another argument in favor of the current approach is that it could be fully dropped with the proposed new lockfile format: The lockfile wouldn't change at all if only imports are changed.

Copy link
Member

Choose a reason for hiding this comment

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

Another argument in favor of the current approach is that it could be fully dropped with the proposed new lockfile format: The lockfile wouldn't change at all if only imports are changed.

To confirm, this is because we send out the lockfile event (or whatever it's called...) before we check the validity of use_repo, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure how your question relates to the quote (did you mean to respond on the thread about Skyframe evaluation failing?). The fixup events are emitted before we check the validity of use_repo, which is indeed crucial, as otherwise we would exit the SkyFunction with a failure before we learn of the fixup command. However, this was already necessary before this PR, simply to show the user-facing warning before running into the failure.

Copy link
Member

Choose a reason for hiding this comment

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

good, good -- nevermind the quote, not important :)

@fmeum fmeum force-pushed the bazel-mod-fix branch 3 times, most recently from 1fdcced to d644e21 Compare January 30, 2024 22:35
@fmeum fmeum requested a review from Wyverald January 30, 2024 22:35
@Wyverald Wyverald added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Jan 30, 2024
@Wyverald Wyverald added the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Feb 1, 2024
@meteorcloudy
Copy link
Member

@fmeum Can you please resolve the conflict?

@fmeum
Copy link
Collaborator Author

fmeum commented Feb 5, 2024

Done!

@fmeum
Copy link
Collaborator Author

fmeum commented Feb 6, 2024

I resolved the new conflict.

@fmeum
Copy link
Collaborator Author

fmeum commented Feb 6, 2024

The remaining failure looks like a flake, but a retry didn't fix it.

@copybara-service copybara-service bot closed this in 9f0f232 Feb 7, 2024
@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Feb 7, 2024
@fmeum fmeum deleted the bazel-mod-fix branch February 7, 2024 18:45
@fmeum
Copy link
Collaborator Author

fmeum commented Feb 7, 2024

@bazel-io fork 7.1.0

iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Feb 7, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
@iancha1992 iancha1992 mentioned this pull request Feb 7, 2024
iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Feb 7, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
iancha1992 pushed a commit to iancha1992/bazel that referenced this pull request Feb 7, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 8, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 8, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 8, 2024
Implements https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates `use_repo` calls in the `MODULE.bazel` file for extensions that use `module_ctx.extension_metadata`.

Closes bazelbuild#20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d
github-merge-queue bot pushed a commit that referenced this pull request Feb 12, 2024
Implements
https://docs.google.com/document/d/13LbK_1WhA4la0eH7yISjnMvXs2cKFXD-adKPu0i0RK0/edit

RELNOTES: The new `bazel mod tidy` subcommand automatically updates
`use_repo` calls in the `MODULE.bazel` file for extensions that use
`module_ctx.extension_metadata`.

Closes #20483.

PiperOrigin-RevId: 605021386
Change-Id: Idb1f22c51e126328b9efd6a5a6d6f89d77b9308d

Closes #21241
@iancha1992
Copy link
Member

The changes in this PR have been included in Bazel 7.1.0 RC1. Please test out the release candidate and report any issues as soon as possible. If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=last_rc.
Thanks!

@aherrmann
Copy link
Contributor

Thanks! I repeated my test from above. bazel mod tidy correctly re-created a deleted use_repo item.
I noticed that it also changed the ordering of attributes in a tag invocation

diff --git a/core/MODULE.bazel b/core/MODULE.bazel
index 52953ec..5202e78 100644
--- a/core/MODULE.bazel
+++ b/core/MODULE.bazel
@@ -9,7 +9,7 @@ bazel_dep(name = "bazel_skylib", version = "1.0.3")
 nix_repo = use_extension("//extensions:repository.bzl", "nix_repo")
 nix_repo.github(
     name = "nixpkgs",
-    tag = "22.11",
     sha256 = "ddc3428d9e1a381b7476750ac4dbea7a42885cbbe6e1af44b21d6447c9609a6f",
+    tag = "22.11",
 )
 use_repo(nix_repo, "nixpkgs")

Is that intentional? IIRC this did not happen when I last tested it and the proposal only speaks about use_repo.
I personally am happy with this behavior, but I just wanted to check if this is not an unintended side effect.

@fmeum
Copy link
Collaborator Author

fmeum commented Feb 23, 2024

This is a result of buildifier formatting the module file, which is a side effect of running buildozer on it. Since buildozer by default only reformats if it made any changes, the bazel mod tidy even forces unconditional reformatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants