Repository: rollup/rollup
File: src/Module.ts (TypeScript)

Code before:
				resolution: string | Module | ExternalModule | undefined;
			}
		).resolution;

		if (resolution instanceof Module) {
			resolution.includedDynamicImporters.push(this);

			const importedNames = this.options.treeshake
				? node.getDeterministicImportedNames()
				: undefined;

			if (importedNames) {
				resolution.includeExportsByNames(importedNames);
			} else {
				resolution.includeAllExports(true);
			}
		}
	}

	private includeVariable(variable: Variable): void {
		if (variable.included) {
			getAndExtendSideEffectModules(variable, this);
		} else {
			variable.include();
			this.graph.needsTreeshakingPass = true;
			const variableModule = variable.module;
			if (variableModule instanceof Module) {
				if (!variableModule.isExecuted) {
					markModuleAndImpureDependenciesAsExecuted(variableModule);
				}
				if (variableModule !== this) {
					const sideEffectModules = getAndExtendSideEffectModules(variable, this);
					for (const module of sideEffectModules) {
						if (!module.isExecuted) {
							markModuleAndImpureDependenciesAsExecuted(module);
						}
					}
				}
			}
		}
	}

	private includeVariableInModule(variable: Variable): void {
		this.includeVariable(variable);
		const variableModule = variable.module;
		if (variableModule && variableModule !== this) {
			this.includedImports.add(variable);
		}
	}

	private shimMissingExport(name: string): void {

Review comment (bug):
Great debugging work! Just a minor change: We only need to do this if the variable is from a non-external module that is not the current module. So I would suggest to change it as below. I do not think we need to replicate the `module.isExecuted` part as I think if the module is included in another module, that automatically implies that the module is executed (I think...).

```suggestion
		const variableModule = variable.module;
		if (variable.included) {
			if (variableModule instanceof Module && variableModule !== this) {
				getAndExtendSideEffectModules(variable, this);
			}
		} else {
			variable.include();
			this.graph.needsTreeshakingPass = true;
```

Code after:
				resolution: string | Module | ExternalModule | undefined;
			}
		).resolution;

		if (resolution instanceof Module) {
			resolution.includedDynamicImporters.push(this);

			const importedNames = this.options.treeshake
				? node.getDeterministicImportedNames()
				: undefined;

			if (importedNames) {
				resolution.includeExportsByNames(importedNames);
			} else {
				resolution.includeAllExports(true);
			}
		}
	}

	private includeVariable(variable: Variable): void {
		const variableModule = variable.module;
		if (variable.included) {
			if (variableModule instanceof Module && variableModule !== this) {
				getAndExtendSideEffectModules(variable, this);
			}
		} else {
			variable.include();
			this.graph.needsTreeshakingPass = true;
			if (variableModule instanceof Module) {
				if (!variableModule.isExecuted) {
					markModuleAndImpureDependenciesAsExecuted(variableModule);
				}
				if (variableModule !== this) {
					const sideEffectModules = getAndExtendSideEffectModules(variable, this);
					for (const module of sideEffectModules) {
						if (!module.isExecuted) {
							markModuleAndImpureDependenciesAsExecuted(module);
						}
					}
				}
			}
		}
	}

	private includeVariableInModule(variable: Variable): void {
		this.includeVariable(variable);
		const variableModule = variable.module;
		if (variableModule && variableModule !== this) {
			this.includedImports.add(variable);
		}
	}