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

Minifier drops console.log incorrectly in some cases #8173

Closed
kdy1 opened this issue Oct 23, 2023 · 2 comments · Fixed by #8172
Closed

Minifier drops console.log incorrectly in some cases #8173

kdy1 opened this issue Oct 23, 2023 · 2 comments · Fixed by #8172
Assignees
Labels
Milestone

Comments

@kdy1
Copy link
Member

kdy1 commented Oct 23, 2023

Describe the bug

I found this while working on #8172

Input code

const someFn = (xx, x, y) => [x, y];

const getArray = () => [1, 2, 3];

const goodFunction = async () => {
    const rb = await getArray();
    const rc = await getArray();
    console.log(someFn(1, rb, rc));
}

const badFunction = async () => {
    console.log(someFn(1, await getArray(), await getArray()))
}

goodFunction();
badFunction();

Config

{
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "decorators": true,
            "tsx": false
        },
        "transform": {
            "legacyDecorator": true
        },
        "target": "es2016",
        "minify": {
            "compress": true,
            "mangle": true
        },
        "loose": false,
        "externalHelpers": false
    },
    "minify": true,
    "module": {
        "type": "commonjs"
    },
    "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.94-nightly-20231016.1&code=H4sIAAAAAAAAA0vOzysuUSjOz011y1OwVdCoqNBRAKJKTQVbO4VoECvWmosrGawqPbXEsagosRKkDiJvqKNgpKNgjKQkPz%2FFrTQvuSQzH2RcYnFlXjJUcTWXAhBAlBUlgSTLEzMRhmpoWiMrSMarID8nVS8nP10D4m4NoDOKkoA4WROophbmlqREwk7BNAjdUkwRTU2QHcg%2BBTkNyTYgFwDKZ1XWVwEAAA%3D%3D&config=H4sIAAAAAAAAA31RvQ6CQAzefQpys4M6ODg7uPgQzVHIkftLWxII4d2FCwiIelu%2Ftt9Prztkw1MVa3XLulQkIAIx0gZLOLdeoBlwJW1E1mSiqON2JkcdCCQQD3NCNX70hUeCAizju9EvM0oIPBeB3F7eYgm6vc8CE%2F93FqASZXSKfDmdryuXyhlvinZPr4OLhPzDtwNfWvynaUNgnLOtcGwEyYN9oI2YzrKkn%2FYXT4uyciGvk%2BLqZ8a7j6kGry74itWGxfBz3kku%2BxdGw%2FMD4QEAAA%3D%3D

SWC Info output

No response

Expected behavior

It should not remove calls with side effect

Actual behavior

No response

Version

1.3.94-nightly-20231016.1

Additional context

No response

@kdy1 kdy1 added the C-bug label Oct 23, 2023
@kdy1 kdy1 self-assigned this Oct 23, 2023
@kdy1 kdy1 added this to the Planned milestone Oct 23, 2023
@kdy1
Copy link
Member Author

kdy1 commented Oct 23, 2023

Investigation



   INFO  compress: Running expression simplifier (pass = 1)
    at crates/swc_ecma_minifier/src/compress/mod.rs:187
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

   INFO  compress: expr_simplifier took 127.209µs (pass = 1)
    at crates/swc_ecma_minifier/src/compress/mod.rs:209
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: asyncGeneratorStep#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _next#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an identifier as it's declared, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:971
    in visit_mut_fn_decl with id: _throw#28
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ignore_return_value: Dropping a pure call, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:884
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Optimizing expr as false (in bool context), kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/bools.rs:434
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  Dropping an unary expression, kind: "change"
    at crates/swc_ecma_minifier/src/compress/pure/misc.rs:995
    in visit_mut_fn_decl with id: _async_to_generator#20
    in apply pure optimizer
    in optimize with pass: 1
    in compress ast
    in minify
    in swc::Compiler::apply_transforms
    in process_js_with_custom_pass
    in process_js_file with fm: SourceFile(/Users/kdy1/projects/s/issues/crates/swc/tests/fixture/issues-8xxx/8155/input/1.js)

  DEBUG  ===== Before pure =====
"use strict";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
    try {
        var info = gen[key](arg);
        var value = info.value;
    } catch (error) {
        reject(error);
        return;
    }
    if (info.done) {
        resolve(value);
    } else {
        Promise.resolve(value).then(_next, _throw);
    }
}
function _async_to_generator(fn) {
    return function() {
        var self = this, args = arguments;
        return new Promise(function(resolve, reject) {
            var gen = fn.apply(self, args);
            function _next(value) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
            }
            function _throw(err) {
                asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
            }
            _next(undefined);
        });
    };
}
const someFn = (xx, x, y)=>[
        x,
        y
    ];
const getArray = ()=>[
        1,
        2,
        3
    ];
const goodFunction = function() {
    var _ref = _async_to_generator(function*() {
        const rb = yield getArray();
        const rc = yield getArray();
        console.log(someFn(1, rb, rc));
    });
    return function goodFunction() {
        return _ref.apply(this, arguments);
    };
}();
const badFunction = function() {
    var _ref = _async_to_generator(function*() {
        console.log(someFn(1, (yield getArray()), (yield getArray())));
    });
    return function badFunction() {
        return _ref.apply(this, arguments);
    };
}();
goodFunction();
badFunction();

===== After pure =====
"use strict";
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
    try {
        var info = gen[key](arg);
        var value = info.value;
    } catch (error) {
        return;
    }
    if (info.done) {} else {}
}
function _async_to_generator(fn) {
    return function() {
        var self = this, args = arguments;
        return new Promise(function(resolve, reject) {
            var gen = fn.apply(self, args);
            function _next(value) {}
            function _throw(err) {}
        });
    };
}
const someFn = (xx, x, y)=>[
        x,
        y
    ];
const getArray = ()=>[
        1,
        2,
        3
    ];
const goodFunction = function() {
    var _ref = _async_to_generator(function*() {
        const rb = yield getArray();
        const rc = yield getArray();
        console.log(someFn(1, rb, rc));
    });
    return function goodFunction() {
        return _ref.apply(this, arguments);
    };
}();
const badFunction = function() {
    var _ref = _async_to_generator(function*() {
        console.log(someFn(1, (yield getArray()), (yield getArray())));
    });
    return function badFunction() {
        return _ref.apply(this, arguments);
    };
}();
goodFunction();
badFunction();

kdy1 added a commit that referenced this issue Oct 23, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.95 Oct 24, 2023
@swc-bot
Copy link
Collaborator

swc-bot commented Nov 23, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

2 participants