blob: 2ec34b65b10ca76ecbb72bd6e0a3b8627901a5cd [file]
// Copyright 2026 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.skyframe;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.compress.CompressionService;
import com.google.devtools.build.lib.compress.CompressionServiceImpl;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.skyframe.serialization.FingerprintValueService;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodecs;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.skyframe.serialization.SharedValueDeserializationContext.LookupAbandonedException;
import com.google.devtools.build.lib.skyframe.serialization.SkyframeDependencyException;
import com.google.devtools.build.lib.skyframe.serialization.SkyframeLookupContinuation;
import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.state.EnvironmentForUtilities;
import com.google.protobuf.ByteString;
import java.util.concurrent.ExecutionException;
import net.starlark.java.eval.Module;
import net.starlark.java.eval.StarlarkSemantics;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for BzlLoadFunction. */
@RunWith(JUnit4.class)
public class BzlLoadThreadOwnerTest extends BuildViewTestCase {
private static final CompressionService COMPRESSION_SERVICE = new CompressionServiceImpl();
private final ObjectCodecs objectCodecs = new ObjectCodecs();
private final FingerprintValueService fingerprintValueService =
FingerprintValueService.createForTesting();
@Test
public void owner_detectsDirectChange() throws Exception {
scratch.file("test/BUILD");
scratch.file(
"test/lib.bzl",
"""
def f():
return 1
""");
BzlLoadThreadOwner oldOwner = getOwner("//test:lib.bzl");
ByteString oldOwnerSerialized = objectCodecs.serializeMemoized(oldOwner);
scratch.overwriteFile(
"test/lib.bzl",
"""
def f():
return 2
""");
invalidatePackages();
BzlLoadThreadOwner newOwner = getOwner("//test:lib.bzl");
assertThat(oldOwner.key()).isEqualTo(newOwner.key());
assertThat(oldOwner).isNotEqualTo(newOwner);
ExecutionException exception =
assertThrows(ExecutionException.class, () -> deserializeWithSkyframe(oldOwnerSerialized));
assertThat(exception)
.hasCauseThat()
.hasMessageThat()
.contains("Cannot retrieve a zombie module");
}
@Test
public void owner_detectsTransitiveChange() throws Exception {
scratch.file("test/BUILD");
scratch.file("test/dep.bzl", "x = 1");
scratch.file(
"test/lib.bzl",
"""
load("dep.bzl", "x")
def f():
return x
""");
BzlLoadThreadOwner oldOwner = getOwner("//test:lib.bzl");
ByteString oldOwnerSerialized = objectCodecs.serializeMemoized(oldOwner);
scratch.overwriteFile("test/dep.bzl", "x = 2");
invalidatePackages();
BzlLoadThreadOwner newOwner = getOwner("//test:lib.bzl");
assertThat(oldOwner.key()).isEqualTo(newOwner.key());
assertThat(oldOwner).isNotEqualTo(newOwner);
ExecutionException exception =
assertThrows(ExecutionException.class, () -> deserializeWithSkyframe(oldOwnerSerialized));
assertThat(exception)
.hasCauseThat()
.hasMessageThat()
.contains("Cannot retrieve a zombie module");
}
@Test
public void owner_ignoresUnrelatedChange() throws Exception {
scratch.file("test/BUILD", "load('//test:unrelated.bzl', 'x')");
scratch.file("test/unrelated.bzl", "x = 1");
scratch.file(
"test/lib.bzl",
"""
def f():
return 42
""");
getTarget("//test:BUILD");
BzlLoadThreadOwner oldOwner = getOwner("//test:lib.bzl");
ByteString oldOwnerSerialized = objectCodecs.serializeMemoized(oldOwner);
scratch.overwriteFile("test/unrelated.bzl", "x = 2");
invalidatePackages();
getTarget("//test:BUILD");
BzlLoadThreadOwner newOwner = getOwner("//test:lib.bzl");
assertThat(oldOwner).isEqualTo(newOwner);
BzlLoadThreadOwner oldOwnerDeserialized = deserializeWithSkyframe(oldOwnerSerialized);
assertThat(oldOwnerDeserialized).isEqualTo(oldOwner);
}
/**
* Regression test for b/565384708.
*
* <p>When a {@link BzlLoadValue} is dropped from Skyframe and later recomputed, the new {@link
* Module} is a distinct instance. Owners built from the old and new modules must still compare
* equal if the semantics and transitive digest match, otherwise Starlark values tokenized by the
* old module compare unequal to deserialized copies tokenized by the new one.
*/
@Test
public void owner_equalForDifferentModuleInstancesWithSameSemanticsAndDigest() {
BzlLoadValue.Key key = getKey("//test:lib.bzl");
byte[] digest = {0, 1, 2, 3};
Module module1 = createModule(key, StarlarkSemantics.DEFAULT, digest);
Module module2 = createModule(key, StarlarkSemantics.DEFAULT, digest);
assertThat(module1).isNotSameInstanceAs(module2);
BzlLoadThreadOwner owner1 = BzlLoadThreadOwner.of(key, module1);
BzlLoadThreadOwner owner2 = BzlLoadThreadOwner.of(key, module2);
assertThat(owner1).isEqualTo(owner2);
assertThat(owner1.hashCode()).isEqualTo(owner2.hashCode());
}
@Test
public void owner_unequalForDifferentDigest() {
BzlLoadValue.Key key = getKey("//test:lib.bzl");
Module module1 = createModule(key, StarlarkSemantics.DEFAULT, new byte[] {0, 1, 2, 3});
Module module2 = createModule(key, StarlarkSemantics.DEFAULT, new byte[] {0xB, 0xA, 0xD});
assertThat(BzlLoadThreadOwner.of(key, module1))
.isNotEqualTo(BzlLoadThreadOwner.of(key, module2));
}
@Test
public void owner_unequalForDifferentSemantics() {
BzlLoadValue.Key key = getKey("//test:lib.bzl");
byte[] digest = {0, 1, 2, 3};
Module module1 = createModule(key, StarlarkSemantics.DEFAULT, digest);
Module module2 =
createModule(
key,
StarlarkSemantics.builder()
.setBool(BuildLanguageOptions.INCOMPATIBLE_NO_ATTR_LICENSE, false)
.build(),
digest);
assertThat(BzlLoadThreadOwner.of(key, module1))
.isNotEqualTo(BzlLoadThreadOwner.of(key, module2));
}
@Test
public void owner_unequalForDifferentKey() {
byte[] digest = {0, 1, 2, 3};
BzlLoadValue.Key key1 = getKey("//test:lib.bzl");
BzlLoadValue.Key key2 = getKey("//test:other.bzl");
assertThat(BzlLoadThreadOwner.of(key1, createModule(key1, StarlarkSemantics.DEFAULT, digest)))
.isNotEqualTo(
BzlLoadThreadOwner.of(key2, createModule(key2, StarlarkSemantics.DEFAULT, digest)));
}
private static Module createModule(
BzlLoadValue.Key key, StarlarkSemantics semantics, byte[] transitiveDigest) {
return Module.withPredeclaredAndData(
semantics,
ImmutableMap.of(),
BazelModuleContext.create(
key,
RepositoryMapping.EMPTY,
/* moduleRepoName= */ null,
key.getLabel().toPathFragment().toString(),
ImmutableList.of(),
transitiveDigest,
ImmutableMap.of(),
ImmutableList.of()));
}
private static BzlLoadValue.Key getKey(String label) {
return BzlLoadValue.keyForBuild(Label.parseCanonicalUnchecked(label));
}
private BzlLoadValue getBzlLoadValue(BzlLoadValue.Key key) throws InterruptedException {
EvaluationResult<BzlLoadValue> result =
SkyframeExecutorTestUtils.evaluate(
getSkyframeExecutor(), key, /* keepGoing= */ false, reporter);
if (result.hasError()) {
fail(result.getError(key).getException().getMessage());
}
return result.get(key);
}
private BzlLoadThreadOwner getOwner(String label) throws InterruptedException {
BzlLoadValue.Key key = getKey(label);
Module module = getBzlLoadValue(key).getModule();
// Ensure that the file has been processed by checking its Module for the label field.
assertThat(Label.parseCanonicalUnchecked(label))
.isEqualTo(BazelModuleContext.of(module).label());
return BzlLoadThreadOwner.of(key, module);
}
private BzlLoadThreadOwner deserializeWithSkyframe(ByteString serialized)
throws ExecutionException,
InterruptedException,
SerializationException,
SkyframeDependencyException,
LookupAbandonedException {
// Deserialization always returns a future because there is a Skyframe lookup. The future is
// always done because there are no shared values to wait on.
SkyframeLookupContinuation continuation =
(SkyframeLookupContinuation)
Futures.getDone(
(ListenableFuture<?>)
objectCodecs.deserializeWithSkyframe(
COMPRESSION_SERVICE, fingerprintValueService, serialized));
ListenableFuture<?> resultFuture =
continuation.process(
new EnvironmentForUtilities(
// The only Skyframe lookup our deserializer needs is for one BzlLoadValue.
key -> {
try {
return getBzlLoadValue((BzlLoadValue.Key) key);
} catch (InterruptedException e) {
throw new AssertionError(e);
}
}));
return (BzlLoadThreadOwner) Futures.getDone(resultFuture);
}
}