Skip to content

Commit 76be3a2

Browse files
authored
Add reverted 'isolate' changes (#18928)
1 parent 287e179 commit 76be3a2

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunction.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ static BzlmodFlagsAndEnvVars getFlagsAndEnvVars(Environment env) throws Interrup
222222
try {
223223
moduleExtensionId =
224224
ModuleExtensionId.create(
225-
labelConverter.convert(usage.getExtensionBzlFile()), usage.getExtensionName(), usage.getIsolationKey());
225+
labelConverter.convert(usage.getExtensionBzlFile()),
226+
usage.getExtensionName(),
227+
usage.getIsolationKey());
226228
} catch (LabelSyntaxException e) {
227229
throw ExternalDepsException.withCauseAndMessage(
228230
Code.BAD_MODULE,
@@ -252,12 +254,31 @@ private ImmutableBiMap<String, ModuleExtensionId> calculateUniqueNameForUsedExte
252254
// not start with a tilde.
253255
RepositoryName repository = id.getBzlFileLabel().getRepository();
254256
String nonEmptyRepoPart = repository.isMain() ? "_main" : repository.getName();
255-
String bestName = nonEmptyRepoPart + "~" + id.getExtensionName();
257+
// When using a namespace, prefix the extension name with "_" to distinguish the prefix from
258+
// those generated by non-namespaced extension usages. Extension names are identified by their
259+
// Starlark identifier, which in the case of an exported symbol cannot start with "_".
260+
// We also include whether the isolated usage is a dev usage as well as its index in the
261+
// MODULE.bazel file to ensure that canonical repository names don't change depending on
262+
// whether dev dependencies are ignored. This removes potential for confusion and also
263+
// prevents unnecessary refetches when --ignore_dev_dependency is toggled.
264+
String bestName =
265+
id.getIsolationKey()
266+
.map(
267+
namespace ->
268+
String.format(
269+
"%s~_%s~%s~%s~%s%d",
270+
nonEmptyRepoPart,
271+
id.getExtensionName(),
272+
namespace.getModule().getName(),
273+
namespace.getModule().getVersion(),
274+
namespace.isDevUsage() ? "dev" : "",
275+
namespace.getIsolatedUsageIndex()))
276+
.orElse(nonEmptyRepoPart + "~" + id.getExtensionName());
256277
if (extensionUniqueNames.putIfAbsent(bestName, id) == null) {
257278
continue;
258279
}
259280
int suffix = 2;
260-
while (extensionUniqueNames.putIfAbsent(bestName + suffix, id) != null) {
281+
while (extensionUniqueNames.putIfAbsent(bestName + "~" + suffix, id) != null) {
261282
suffix++;
262283
}
263284
}

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public void createValue_moduleExtensions() throws Exception {
292292
maven, "rules_jvm_external~1.0~maven",
293293
pip, "rules_python~2.0~pip",
294294
myext, "dep~2.0~myext",
295-
myext2, "dep~2.0~myext2");
295+
myext2, "dep~2.0~myext~2");
296296

297297
assertThat(value.getFullRepoMapping(ModuleKey.ROOT))
298298
.isEqualTo(
@@ -323,7 +323,7 @@ public void createValue_moduleExtensions() throws Exception {
323323
"oneext",
324324
"dep~2.0~myext~myext",
325325
"twoext",
326-
"dep~2.0~myext2~myext"));
326+
"dep~2.0~myext~2~myext"));
327327
}
328328

329329
@Test

0 commit comments

Comments
 (0)