diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java index 2147bd2ea0672b..26a57516da9ea9 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java @@ -320,12 +320,17 @@ private RepositoryDirectoryValue.Builder fetchInternal( new RepoRecordedInput.EnvVar(envKey), clientEnvironment.get(envKey)); } - for (Table.Cell repoMappings : - repoMappingRecorder.recordedEntries().cellSet()) { - recordedInputValues.put( - new RepoRecordedInput.RecordedRepoMapping( - repoMappings.getRowKey(), repoMappings.getColumnKey()), - repoMappings.getValue().getName()); + // For repos defined in Bzlmod, record any used repo mappings in the marker file. + // Repos defined in WORKSPACE are impossible to verify given the chunked loading (we'd have to + // record which chunk the repo mapping was used in, and ain't nobody got time for that). + if (!isWorkspaceRepo(rule)) { + for (Table.Cell repoMappings : + repoMappingRecorder.recordedEntries().cellSet()) { + recordedInputValues.put( + new RepoRecordedInput.RecordedRepoMapping( + repoMappings.getRowKey(), repoMappings.getColumnKey()), + repoMappings.getValue().getName()); + } } env.getListener().post(resolved); diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepoRecordedInput.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepoRecordedInput.java index 9c8a6369109119..605bd65a7ce3c5 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepoRecordedInput.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepoRecordedInput.java @@ -559,7 +559,12 @@ public String toStringInternal() { @Override public SkyKey getSkyKey(BlazeDirectories directories) { - return RepositoryMappingValue.key(sourceRepo); + // Since we only record repo mapping entries for repos defined in Bzlmod, we can request the + // WORKSPACE-less version of the main repo mapping (as no repos defined in Bzlmod can see + // stuff from WORKSPACE). + return sourceRepo.isMain() + ? RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS + : RepositoryMappingValue.key(sourceRepo); } @Override diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java index 63578d8b1efdc0..4d8c46f8d5faa9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java @@ -15,7 +15,6 @@ package com.google.devtools.build.lib.rules.repository; -import static com.google.devtools.build.lib.cmdline.LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; @@ -394,12 +393,9 @@ private boolean shouldUseVendorRepos(Environment env, RepositoryFunction handler } private boolean shouldExcludeRepoFromVendoring(RepositoryFunction handler, Rule rule) { - return handler.isLocal(rule) || handler.isConfigure(rule) || isWorkspaceRepo(rule); - } - - private boolean isWorkspaceRepo(Rule rule) { - // All workspace repos are under //external, while bzlmod repo rules are not - return rule.getPackage().getPackageIdentifier().equals(EXTERNAL_PACKAGE_IDENTIFIER); + return handler.isLocal(rule) + || handler.isConfigure(rule) + || RepositoryFunction.isWorkspaceRepo(rule); } @Nullable diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java index 1533eaca58c46e..591211ce441f77 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.repository; import static com.google.common.base.Preconditions.checkState; +import static com.google.devtools.build.lib.cmdline.LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; @@ -137,6 +138,11 @@ public static void setupRepoRoot(Path repoRoot) throws RepositoryFunctionExcepti } } + public static boolean isWorkspaceRepo(Rule rule) { + // All workspace repos are under //external, while bzlmod repo rules are not + return rule.getPackage().getPackageIdentifier().equals(EXTERNAL_PACKAGE_IDENTIFIER); + } + protected void setupRepoRootBeforeFetching(Path repoRoot) throws RepositoryFunctionException { setupRepoRoot(repoRoot); } diff --git a/src/test/shell/bazel/starlark_repository_test.sh b/src/test/shell/bazel/starlark_repository_test.sh index 54da676930a190..aabc46e0465c41 100755 --- a/src/test/shell/bazel/starlark_repository_test.sh +++ b/src/test/shell/bazel/starlark_repository_test.sh @@ -2660,10 +2660,12 @@ bazel_dep(name="bar") local_path_override(module_name="bar", path="bar") EOF touch BUILD + echo 'load("@r//:r.bzl", "pi"); print(pi)' > WORKSPACE.bzlmod cat > r.bzl < WORKSPACE.bzlmod cat > r.bzl <