google / google/closure-compiler

Mutable exports lead to incorrect code removal

Open
#3,157 8 comments 0 reactions 0 assignees View on GitHub
triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Given these two files ([service repro](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540language_out%2520ES5%250A%252F%252F%2520%2540code_url%2520http%253A%252F%252Fpersistent.info%252Ftmp%252Fbase.js%250A%252F%252F%2520%2540code_url%2520http%253A%252F%252Fpersistent.info%252Ftmp%252Fmain.js%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A)):
```js
// base.js
export function initialize(fn) {
fn();
}

function reassignInitialize(wrapper) {
initialize = function(fn) {
fn();
};
}

// main.js
import * as base from "./base.js";

console.log("Loaded main");

export function initialize() {
console.log("Running initialize");
}

base.initialize(initialize);
```

The expected output is `console.log("Loaded main");console.log("Running initialize")`. However, only `console.log("Loaded main");` appears.

If we drop the `reassignInitialize` function ([service repro](https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540language_out%2520ES5%250A%252F%252F%2520%2540code_url%2520http%253A%252F%252Fpersistent.info%252Ftmp%252Fbase-no-reassignment.js%250A%252F%252F%2520%2540code_url%2520http%253A%252F%252Fpersistent.info%252Ftmp%252Fmain-no-reassignment.js%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A)) then it works as expected.

I'm guessing that something in the mutable export implementation (679ea8fb4e8808ee725d22b8156ec09c96e437b0 - it uses getters) is not playing well with advanced optimizations, but I have not investigated further.

Contributor guide

Open the contributing guide

Research direction

Run the linked Closure Compiler repro with base.js and main.js under ADVANCED_OPTIMIZATIONS, then compare it with the no-reassignment variant. Trace the mutable export implementation introduced by commit 679ea8fb4e8808ee725d22b8156ec09c96e437b0. Done means the original repro preserves both expected console.log calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.