-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fix edge cases in lockfile handling #24754
Closed
+160
−32
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2024 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
fmeum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import java.util.Optional; | ||
import net.starlark.java.eval.Starlark; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** Tests for {@link BazelLockFileModule}. */ | ||
@RunWith(JUnit4.class) | ||
public class BazelLockFileModuleTest { | ||
|
||
private ModuleExtensionId extensionId; | ||
private LockFileModuleExtension nonReproducibleResult; | ||
private LockFileModuleExtension reproducibleResult; | ||
private ModuleExtensionEvalFactors evalFactors; | ||
private ModuleExtensionEvalFactors otherEvalFactors; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
extensionId = | ||
ModuleExtensionId.create( | ||
Label.parseCanonicalUnchecked("//:ext.bzl"), "ext", Optional.empty()); | ||
nonReproducibleResult = | ||
LockFileModuleExtension.builder() | ||
.setBzlTransitiveDigest(new byte[] {1, 2, 3}) | ||
.setUsagesDigest(new byte[] {4, 5, 6}) | ||
.setRecordedFileInputs(ImmutableMap.of()) | ||
.setRecordedDirentsInputs(ImmutableMap.of()) | ||
.setEnvVariables(ImmutableMap.of()) | ||
.setGeneratedRepoSpecs(ImmutableMap.of()) | ||
.build(); | ||
reproducibleResult = | ||
LockFileModuleExtension.builder() | ||
.setBzlTransitiveDigest(new byte[] {1, 2, 3}) | ||
.setUsagesDigest(new byte[] {4, 5, 6}) | ||
.setRecordedFileInputs(ImmutableMap.of()) | ||
.setRecordedDirentsInputs(ImmutableMap.of()) | ||
.setEnvVariables(ImmutableMap.of()) | ||
.setGeneratedRepoSpecs(ImmutableMap.of()) | ||
.setModuleExtensionMetadata( | ||
Optional.of( | ||
ModuleExtensionMetadata.create( | ||
Starlark.NONE, Starlark.NONE, /* reproducible= */ true))) | ||
.build(); | ||
evalFactors = ModuleExtensionEvalFactors.create("linux", "x86_64"); | ||
otherEvalFactors = ModuleExtensionEvalFactors.create("linux", "aarch64"); | ||
} | ||
|
||
@Test | ||
public void combineModuleExtensionsReproducibleFactorAdded() { | ||
var oldExtensionInfos = | ||
ImmutableMap.of(extensionId, ImmutableMap.of(evalFactors, nonReproducibleResult)); | ||
var newExtensionInfos = | ||
ImmutableMap.of( | ||
extensionId, | ||
new LockFileModuleExtension.WithFactors(otherEvalFactors, reproducibleResult)); | ||
|
||
assertThat( | ||
BazelLockFileModule.combineModuleExtensions( | ||
oldExtensionInfos, newExtensionInfos, id -> true)) | ||
.isEqualTo(oldExtensionInfos); | ||
} | ||
|
||
@Test | ||
public void combineModuleExtensionsFactorBecomesReproducible() { | ||
var oldExtensionInfos = | ||
ImmutableMap.of(extensionId, ImmutableMap.of(evalFactors, nonReproducibleResult)); | ||
var newExtensionInfos = | ||
ImmutableMap.of( | ||
extensionId, new LockFileModuleExtension.WithFactors(evalFactors, reproducibleResult)); | ||
|
||
assertThat( | ||
BazelLockFileModule.combineModuleExtensions( | ||
oldExtensionInfos, newExtensionInfos, id -> true)) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void combineModuleExtensionsFactorBecomesNonReproducible() { | ||
var oldExtensionInfos = | ||
ImmutableMap.of(extensionId, ImmutableMap.of(evalFactors, reproducibleResult)); | ||
var newExtensionInfos = | ||
ImmutableMap.of( | ||
extensionId, | ||
new LockFileModuleExtension.WithFactors(evalFactors, nonReproducibleResult)); | ||
|
||
assertThat( | ||
BazelLockFileModule.combineModuleExtensions( | ||
oldExtensionInfos, newExtensionInfos, id -> true)) | ||
.isEqualTo( | ||
ImmutableMap.of(extensionId, ImmutableMap.of(evalFactors, nonReproducibleResult))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for the third point in the description: module extension IDs with an isolation key and those for repo rules would conflict after serialization into a string (for the lockfile), which results in a crash when parsing them back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems dangerous to me. Changing the separator to
+
here would mean that (IIUC) we can't have it in the .bzl file label, so essentially using any repo rule from a non-main repo would fail (because that would result in an extension name like@@foo++foo_ext+foo_repo//:bar.bzl+my_repo_rule
). In fact I'm surprised all tests passed -- maybe we neveruse_repo_rule
from non-main repos in our tests?(and yes, I understand that this also means
%
was never allowed to show up in the .bzl file label, but this was also true of aspects etc.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, why exactly would this fail? The synthetic extension name as passed in by module file evaluation is "prettified" to just
_repo_rules
for the purpose of generating the extension unique prefix: https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunction.java;l=186?q=_repo_rules&ss=bazel%2Fbazel&start=41.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the repos generated by an innate extension have the form of
module+_repo_rulesN+repo_name
. But the name of the extension corresponding to the repo name prefixmodule+_repo_rulesN
is currently<bzl_file_label>%<rule_name>
(code). This PR is changing that line to split on+
, which should result in errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm now using a space instead of a plus and am also only splitting on the last occurrence of the separator.
I don't think this was an issue in practice though as the bzl file label is the one specified in a module file and thus uses apparent rather than canonical repo names (I don't think we ever intentionally supported the latter and even prohibited them at some point).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I see! That's indeed why everything was actually okay.
Yeah, as long as the separator is not a valid Starlark identifier character and we split by the last occurrence only, we should be all good in all cases. This LGTM now, thanks!