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

Mangling sometimes reuses and attempts to re-declare symbols used in imports #7634

Closed
thebanjomatic opened this issue Jul 6, 2023 · 1 comment · Fixed by #7639 or #7707
Closed
Assignees
Labels
Milestone

Comments

@thebanjomatic
Copy link

thebanjomatic commented Jul 6, 2023

Describe the bug

I encountered this problem while trying to minify a file from lodash. This ultimately fails my build with the following error:

Module parse failed: Identifier 'o' has already been declared (1:194)

Input code

Original Reproducer:

// https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
import Symbol from './_Symbol.js';
import arrayMap from './_arrayMap.js';
import isArray from './isArray.js';
import isSymbol from './isSymbol.js';

/** Used as references for various `Number` constants. */
var INFINITY = 1 / 0;

/** Used to convert symbols to primitives and strings. */
var symbolProto = Symbol ? Symbol.prototype : undefined,
    symbolToString = symbolProto ? symbolProto.toString : undefined;

/**
 * The base implementation of `_.toString` which doesn't convert nullish
 * values to empty strings.
 *
 * @private
 * @param {*} value The value to process.
 * @returns {string} Returns the string.
 */
function baseToString(value) {
  // Exit early for strings to avoid a performance hit in some environments.
  if (typeof value == 'string') {
    return value;
  }
  if (isArray(value)) {
    // Recursively convert values (susceptible to call stack limits).
    return arrayMap(value, baseToString) + '';
  }
  if (isSymbol(value)) {
    return symbolToString ? symbolToString.call(value) : '';
  }
  var result = (value + '');
  return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
}

export default baseToString;

Minimal Reproducer:

import Foo from './foo.js';

export const Bar = Foo;

function someRecursiveFunction(value) {
  return value.map(someRecursiveFunction);
}

export default someRecursiveFunction;

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": {
        "toplevel": false,
        "keep_classnames": false,
        "keep_fnames": false,
        "keep_private_props": false,
        "ie8": false,
        "safari10": false
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.3.66&code=H4sIAAAAAAAAA11UTW%2FaQBS8I%2FEf5hQDTUx6JUKkh1biUFQl9JBTWMxz2dbetXbXbhDiv%2FfthynAyft23rx54zGybrRxeD3UW12hNLpGlk%2Ff4zn%2FbbOn4UBGjDBGHL6L5j%2Bqr1zjpP3i62dYOt%2BCbkb2hQQbDqaTCX5a2kFYGCrJkCrIotQGnTBStxabVVtvyWxQaGWdUM7mmEyHA77HcvVtuVqu3zDHZ0zxeM3ptO%2FpiJXYMNb6UmNkLZ3seIxQO1hnpPp1wRmhP4xm7Lz3bJEe8sbX3aEhzNCqHZVS0e5%2BOAD%2FYudavwZKbr6kWlyecteDLlh68Uw2wXpP2ApLYCsrqkk54aRW0CU27%2Bf2Df7uZbHHTpNVmTuvq9qqknYfmDpRtRQ2p7pxh%2FPC%2FjIAntmRTjhKB2FEjePkFBuDkPgUvNP8emIvng251iiLY6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er:
https://play.swc.rs/?version=1.3.66&code=H4sIAAAAAAAAA22NMQ7CMBAEe0v%2Bw3ZJGvMAREORB%2FADy5wlo9gXnX0REuLvBIuCIu3s7mzKK0vDzIwonDG4U2R2jzqcrbGGnj0OXGrD1Qsu32qPopbQEhdUznSjoFLTRvOPjptflCa8rAGEmkpBRy77dTycTLv2%2FXd6p%2Bh1acf%2BvfsBkO%2BxOrsAAAA%3D&config=H4sIAAAAAAAAA41VwW7bMAy99ysCn3doB3QY9gG77RsExaIcZbJoiJSboMi%2Fj1bsNG1oY5cg5tMjRfKRen%2Fa7Zojtc2v3bv8lY%2FBZoJ8%2BxYLnRPbk1gaaHtLbQ4DN98W9EgT5G0kqKbLFWnY5g64suj788vrzGgiIsHCmG19SMGf72O22A8ZiO5sYhWXpYfE9Jk%2FYxnfJoBzubfvESPYtIEYSyYkhg6y5rjFGO1AYEabFS%2FTTW0OhFqICSwMzgwZBxVPLnDAJDEfUQfWmRYdKFDI0HIYQaNJLKElkvSUfCrsYF%2B6rvb5CxtGG4tlJSacakvktorXAwZi40vSSngFV2pwBefifmUGbzJwyemRd8SQVnryF0AqEC1Rsj1ofusJL3paY%2FtNZkheJMtnBRd9a1km6KSoJgSvVHaqDGQOWjczuNLCVNlWu84Mr5SPggMD3otWFNf0Frg9aEH5PAB6BZD%2BWq%2Bp6gqY2xSu4NNAbMC%2FJUvWBTaf6C0f1lE693uMGwF64AO6jQPSCsZ1OMuWOA3reEkORBrg1COFKvC4BGQAGE2s%2B%2FJBGzIe4tF0Efcfa2I%2BcLnt4d6mrs773b5kHCKMENdk%2FB8jsonK3cZJ1ctkP0wv%2FNREaWXwwsvzp2dDUnlafmtSTY%2Bu3CVUNXl9Tn40H4eWl%2BMWpAn0Z2HWQl3%2BAebXfY3kBgAA

Expected behavior

The function should have a unique name and should not collide with the imports.

Actual behavior

The function exported as the default export has the same name as one of the imports which is invalid esm code and fails to parse both in the browser and in nodejs.

Version

1.3.68

Additional context

No response

@swc-bot
Copy link
Collaborator

swc-bot commented Aug 17, 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 Aug 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.