Repository: rollup/rollup
File: src/utils/deconflictChunk.ts (TypeScript)

Code before:
				variable.setRenderNames(
					null,
					chunk.exportMode === 'default' ? chunk.namespaceVariableName : chunk.variableName
				);
			} else if (chunk.exportMode === 'default') {
				variable.setRenderNames(null, chunk.variableName);
			} else {
				variable.setRenderNames(
					chunk.variableName,
					chunk.getVariableExportName(variable) as string | null
				);
			}
		}
	}
}

function deconflictTopLevelVariables(
	usedNames: Set<string>,
	modules: readonly Module[],
	includedNamespaces: ReadonlySet<Module>
): void {
	for (const module of modules) {
		for (const safeVariableName of Object.values(module.info.safeVariableNames ?? {})) {
			usedNames.add(safeVariableName);
		}
	}

	for (const module of modules) {
		module.info.safeVariableNames ||= {};
		for (const variable of module.scope.variables.values()) {
			const cachedSafeVariableName = module.info.safeVariableNames?.[variable.name];
			if (cachedSafeVariableName) {
				variable.setRenderNames(null, cachedSafeVariableName);
				continue;
			}
			if (
				variable.included &&
				// this will only happen for exports in some formats
				!(
					variable.renderBaseName ||
					(variable instanceof ExportDefaultVariable && variable.getOriginalVariable() !== variable)
				)
			) {
				variable.setRenderNames(
					null,
					getSafeName(variable.name, usedNames, variable.forbiddenNames)
				);
				module.info.safeVariableNames[variable.name] = variable.renderName!;
			}
		}
		if (includedNamespaces.has(module)) {

Review comment (suggestion):
> the names in the updated module are no longer deconflicted, resulting in an invalid bundle.

the reason for this was that we did not reflect the changes that are in the `safeVariableNames` property to the `usedNames` variable which is the key component of the deconflicting idea.

and here's the fix for it. 

Code after:
				variable.setRenderNames(null, chunk.variableName);
			} else {
				variable.setRenderNames(
					chunk.variableName,
					chunk.getVariableExportName(variable) as string | null
				);
			}
		}
	}
}

function deconflictTopLevelVariables(
	usedNames: Set<string>,
	modules: readonly Module[],
	includedNamespaces: ReadonlySet<Module>
): void {
	for (const module of modules) {
		module.info.safeVariableNames ||= {};
		for (const variable of module.scope.variables.values()) {
			if (
				variable.included &&
				// this will only happen for exports in some formats
				!(
					variable.renderBaseName ||
					(variable instanceof ExportDefaultVariable && variable.getOriginalVariable() !== variable)
				)
			) {
				// We need to make sure that variables that corresponding to object
				// prototype methods are not accidentally matched.
				const cachedSafeVariableName = Object.getOwnPropertyDescriptor(
					module.info.safeVariableNames,
					variable.name
				)?.value;

				if (cachedSafeVariableName && !usedNames.has(cachedSafeVariableName)) {
					usedNames.add(cachedSafeVariableName);
					variable.setRenderNames(null, cachedSafeVariableName);
					continue;
				}

				variable.setRenderNames(
					null,
					getSafeName(variable.name, usedNames, variable.forbiddenNames)
				);
				module.info.safeVariableNames[variable.name] = variable.renderName!;
			}
		}
		if (includedNamespaces.has(module)) {
			const namespace = module.namespace;
			namespace.setRenderNames(
				null,