@@ -222,7 +222,9 @@ static BzlmodFlagsAndEnvVars getFlagsAndEnvVars(Environment env) throws Interrup
222
222
try {
223
223
moduleExtensionId =
224
224
ModuleExtensionId .create (
225
- labelConverter .convert (usage .getExtensionBzlFile ()), usage .getExtensionName (), usage .getIsolationKey ());
225
+ labelConverter .convert (usage .getExtensionBzlFile ()),
226
+ usage .getExtensionName (),
227
+ usage .getIsolationKey ());
226
228
} catch (LabelSyntaxException e ) {
227
229
throw ExternalDepsException .withCauseAndMessage (
228
230
Code .BAD_MODULE ,
@@ -252,12 +254,31 @@ private ImmutableBiMap<String, ModuleExtensionId> calculateUniqueNameForUsedExte
252
254
// not start with a tilde.
253
255
RepositoryName repository = id .getBzlFileLabel ().getRepository ();
254
256
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 ());
256
277
if (extensionUniqueNames .putIfAbsent (bestName , id ) == null ) {
257
278
continue ;
258
279
}
259
280
int suffix = 2 ;
260
- while (extensionUniqueNames .putIfAbsent (bestName + suffix , id ) != null ) {
281
+ while (extensionUniqueNames .putIfAbsent (bestName + "~" + suffix , id ) != null ) {
261
282
suffix ++;
262
283
}
263
284
}
0 commit comments