|
66 | 66 | import java.util.LinkedHashSet;
|
67 | 67 | import java.util.List;
|
68 | 68 | import java.util.Map;
|
| 69 | +import java.util.Optional; |
69 | 70 | import java.util.concurrent.atomic.AtomicBoolean;
|
70 | 71 | import java.util.function.Consumer;
|
71 | 72 | import javax.annotation.Nullable;
|
@@ -762,6 +763,11 @@ private BzlLoadValue computeInternalWithCompiledBzl(
|
762 | 763 | if (repoMapping == null) {
|
763 | 764 | return null;
|
764 | 765 | }
|
| 766 | + Optional<RepositoryMapping> mainRepoMapping = |
| 767 | + getMainRepositoryMapping(key, builtins.starlarkSemantics, env); |
| 768 | + if (mainRepoMapping == null) { |
| 769 | + return null; |
| 770 | + } |
765 | 771 | Label.RepoMappingRecorder repoMappingRecorder = new Label.RepoMappingRecorder();
|
766 | 772 | ImmutableList<Pair<String, Location>> programLoads = getLoadsFromProgram(prog);
|
767 | 773 | ImmutableList<Label> loadLabels =
|
@@ -840,6 +846,7 @@ private BzlLoadValue computeInternalWithCompiledBzl(
|
840 | 846 | BazelModuleContext.create(
|
841 | 847 | label,
|
842 | 848 | repoMapping,
|
| 849 | + mainRepoMapping.orElse(null), |
843 | 850 | prog.getFilename(),
|
844 | 851 | ImmutableList.copyOf(loadMap.values()),
|
845 | 852 | transitiveDigest);
|
@@ -974,6 +981,34 @@ private static RepositoryMapping getRepositoryMapping(
|
974 | 981 | return repositoryMappingValue.getRepositoryMapping();
|
975 | 982 | }
|
976 | 983 |
|
| 984 | + @Nullable |
| 985 | + private static Optional<RepositoryMapping> getMainRepositoryMapping( |
| 986 | + BzlLoadValue.Key key, StarlarkSemantics starlarkSemantics, Environment env) |
| 987 | + throws InterruptedException { |
| 988 | + if (!starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD)) { |
| 989 | + return Optional.empty(); |
| 990 | + } |
| 991 | + RepositoryMappingValue.Key repoMappingKey; |
| 992 | + // When adding cases for other key types such as WORKSPACE or Bzlmod, make sure to track the |
| 993 | + // usages of the repo mapping in persistent caches, such as repository marker files and the |
| 994 | + // MODULE.bazel.lock file. |
| 995 | + if (key instanceof BzlLoadValue.KeyForBuild) { |
| 996 | + repoMappingKey = RepositoryMappingValue.key(RepositoryName.MAIN); |
| 997 | + } else if (key instanceof BzlLoadValue.KeyForBuiltins) { |
| 998 | + // Using the full main repo mapping here results in a cycle as it depends on WORKSPACE, but |
| 999 | + // builtins are injected into WORKSPACE. Fixing this fully would require adding a new key type |
| 1000 | + // for builtins (transitively) loaded from WORKSPACE. |
| 1001 | + repoMappingKey = RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS; |
| 1002 | + } else { |
| 1003 | + return Optional.empty(); |
| 1004 | + } |
| 1005 | + var mainRepositoryMappingValue = (RepositoryMappingValue) env.getValue(repoMappingKey); |
| 1006 | + if (mainRepositoryMappingValue == null) { |
| 1007 | + return null; |
| 1008 | + } |
| 1009 | + return Optional.of(mainRepositoryMappingValue.getRepositoryMapping()); |
| 1010 | + } |
| 1011 | + |
977 | 1012 | /**
|
978 | 1013 | * Validates a label appearing in a {@code load()} statement, throwing {@link
|
979 | 1014 | * LabelSyntaxException} on failure.
|
|
0 commit comments