Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minifying return await removes await which produces a different program #5157

Closed
jsoldi opened this issue Oct 28, 2021 · 4 comments · Fixed by #5158
Closed

Minifying return await removes await which produces a different program #5157

jsoldi opened this issue Oct 28, 2021 · 4 comments · Fixed by #5158
Labels

Comments

@jsoldi
Copy link

jsoldi commented Oct 28, 2021

Uglify version 3.14.2

JavaScript input

window.fail = async function() { 
    throw new Error("Fail") 
}; 

window.test = async function() { 
    try { 
        return await fail(); 
    } 
    catch(e) { 
        console.log("Failed: " + e.message); 
        return 0; 
    } 
}

The uglifyjs CLI command executed or minify() options used.

Just the default options. This is my whole code:

import UglifyJS from 'uglify-js'
var res = UglifyJS.minify('window.fail = async function() { throw new Error("Fail") }; window.test = async function() { try { return await fail(); } catch(e) { console.log("Failed: " + e.message); return 0; } }');
console.log(res.code);

JavaScript output (beautified) or error produced.

window.fail = async function() {
    throw new Error("Fail")
}, 
window.test = async function() {
    try {
        return fail()
    } catch (n) {
        return console.log("Failed " + n.message), 0
    }
};

Uglify removes the await in the test function, which changes the program. The original test function logs Failed Fail and returns 0 without actually throwing, while the minified version throws Fail.

@kzc
Copy link
Contributor

kzc commented Oct 29, 2021

Test reformatted to run under NodeJS:

$ cat a.js
async function fail() { 
    throw new Error("PASS");
} 
async function test() { 
    try { 
        return await fail(); 
    } catch (e) { 
        console.log(e.message);
    } 
}
test();
$ cat a.js | node
PASS
$ cat a.js | uglify-js -bc | node
[stdin]:2
    throw new Error("PASS");
          ^

Error: PASS
    at fail ([stdin]:2:11)
    at test ([stdin]:7:16)
    at [stdin]:13:1

@kzc
Copy link
Contributor

kzc commented Oct 29, 2021

Another variation of the bug:

$ cat a2.js 
function fail() { 
    return new Promise((s, f) => f(new Error("PASS")));
} 
async function test() { 
    try { 
        return await fail(); 
    } catch (e) { 
        console.log(e.message);
    } 
}
test();
$ cat a2.js | node
PASS
$ cat a2.js | uglify-js -bc | node
[stdin]:2
    return new Promise((s, f) => f(new Error("PASS")));
                                   ^

Error: PASS
    at [stdin]:2:36
    at new Promise (<anonymous>)
    at fail ([stdin]:2:12)
    at test ([stdin]:7:16)
    at [stdin]:13:1

@alexlamsl alexlamsl added the bug label Oct 29, 2021
@alexlamsl
Copy link
Collaborator

Thanks @jsoldi for the report and @kzc for narrowing it down − investigating.

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Oct 29, 2021
alexlamsl added a commit that referenced this issue Oct 29, 2021
@alexlamsl
Copy link
Collaborator

Patch released in uglify-js@3.14.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants