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