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

"Duplicate declaration" error when running "lingui extract" with NODE_ENV=production #433

Closed
agwells opened this issue Jan 14, 2019 · 23 comments

Comments

@agwells
Copy link
Contributor

agwells commented Jan 14, 2019

Describe the bug
I've recently upgraded my LinguiJS package versions from 2.7.0 to 2.7.2. Since the upgrade, my build step to extract language strings crashes with an error message about a duplicate declaration of Trans. After some experimentation, I discovered that this only happens when the NODE_ENV environment variable is production. If I set NODE_ENV to development or test, it works fine.

(I haven't had time yet to try to replicate this error on a newly set-up project, so it's possible it may be related to other quirks in my project.)

To Reproduce

  1. Have a "Create React App" application with a file that imports {Trans} like so:
import { Trans } from "@lingui/macro"

export default function App() {
   return <Trans>This should be translated!</Trans>
}
  1. Set up a script in package.json like so:
"scripts": {
  "extract": "NODE_ENV=production lingui extract --overwrite"
}
  1. Run npm run extract
  2. The npm run script crashes, with the error message TypeError: /path/to/project/src/file.js: Duplicate declaration "Trans"

Expected behavior
lingui extract should complete successfully, with output like this:

Catalog statistics:
┌─────────────┬─────────────┬─────────┐
│ Language    │ Total count │ Missing │
├─────────────┼─────────────┼─────────┤
│ en (source) │     293     │    -    │
│ en-x-intl   │     293     │   293   │
└─────────────┴─────────────┴─────────┘

(use "npm run add-locale <locale>" to add more locales)
(use "npm run extract" to update catalogs with new messages)
(use "npm run compile" to compile catalogs for production)

Additional context
As mentioned above, this error only happens when running lingui with NODE_ENV=production. When I run it with NODE_ENV=development, it finishes successfully. That will probably be my workaround for the time being, because I imagine the NODE_ENV probably shouldn't have any effect on the CLI scripts anyhow.

Here's the full error stack from npm (with some file names changed):

/path/to/project/node_modules/@lingui/cli/api/compat.js:48
        throw e;
        ^

TypeError: /path/to/project/src/file.js: Duplicate declaration "Trans"
> 1 | import { Trans } from '@lingui/macro';
     |          ^
    at File.buildCodeFrameError (/path/to/project/node_modules/@babel/core/lib/transformation/file/file.js:261:12)
    at Scope.checkBlockScopedCollisions (/path/to/project/node_modules/@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/path/to/project/node_modules/@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/path/to/project/node_modules/@babel/traverse/lib/scope/index.js:452:14)
    at Object.Declaration (/path/to/project/node_modules/@babel/traverse/lib/scope/index.js:125:12)
    at NodePath._call (/path/to/project/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/path/to/project/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/path/to/project/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/path/to/project/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/path/to/project/node_modules/@babel/traverse/lib/context.js:85:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project@0.1.0 extract: `NODE_ENV=production lingui extract --overwrite`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the project@0.1.0 extract script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
  • jsLingui version: 2.7.2
  • Your Babel config: Create React App
  • Babel version:
└─┬ react-scripts@2.1.3
  ├── babel-core@7.0.0-bridge.0 
  └─┬ jest@23.6.0
    └─┬ jest-cli@23.6.0
      ├─┬ jest-config@23.6.0
      │ └─┬ babel-core@6.26.3 
      │   └─┬ babel-register@6.26.0
      │     └── babel-core@6.26.3 
      └─┬ jest-runtime@23.6.0
        └── babel-core@6.26.3 
@tricoder42
Copy link
Contributor

Hey @agwells,
this is interesting, I'll take a look.

In the meantime: You don't have to run lingui extract with NODE_ENV=production. The script just extracts messages to external catalog. You should only build your app with NODE_ENV=production, but stripping out this env var from lingui extract should be just fine.

@agwells
Copy link
Contributor Author

agwells commented Jan 23, 2019

Thanks for the confirmation about NODE_ENV. That's what I figured, because lingui extract is just statically analyzing the source code, so it would be weird if it had separate development and production modes. :)

It was just due to the sequence of steps in our build pipeline, that we were running lingui extract with NODE_ENV=production. It's at the beginning of a stage that compiles the app in "production" mode, so I had declared NODE_ENV=production near the top of the step. Fortunately, it was easy to change things so the lingui step uses NODE_ENV=development, and then we switch back to production.

@tricoder42 tricoder42 added this to the 3.0 Macros milestone Jan 24, 2019
@tricoder42
Copy link
Contributor

Ah, cool! I'm glad you figured out a workaround. I'll keep it open just in case I find a root cause.

@adamgruber
Copy link
Contributor

I just started seeing this same issue because we run lingui extract as part of the build script. Not sure if this is an issue with jsLingui or a change in Babel.

@stale
Copy link

stale bot commented Oct 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 29, 2020
@stale stale bot closed this as completed Nov 5, 2020
@namirsab
Copy link

namirsab commented Jan 18, 2021

@tricoder42
This issue was automatically closed, but it's still happening. If you just follow the tutorials from the website, you reach this error. Any news or plans to fix it?

Just in case it got forgotten somehow, or maybe i'm doing something wrong.
I'm using:

"@lingui/cli": "^3.4.0",
"@lingui/macro": "^3.4.0",

@semoal
Copy link
Contributor

semoal commented Jan 27, 2021

This issue probably will be fixed next Tuesday, meanwhile you can pass NODE_ENV=development before lingui extract command. @namirsab

@semoal semoal added the next release 📦 Issues resolved in next release label Jan 27, 2021
@semoal
Copy link
Contributor

semoal commented Feb 2, 2021

Released 3.5.0 with this fix

@semoal semoal removed the next release 📦 Issues resolved in next release label Feb 2, 2021
@adamgruber
Copy link
Contributor

I updated to @lingui/cli 3.7.1 and babel-plugin-macros 3.0.1 but I'm still seeing this issue.

@indiejoseph
Copy link

Updated to 3.10.2, and still seeing this issue.

@ianatha
Copy link

ianatha commented Jul 22, 2021

Still seeing this issue, even with NODE_ENV=production:

Invocation:

yarn run v1.22.10
$ NODE_ENV=production lingui extract --clean
Cannot process file XXX/index.tsx: Duplicate declaration "Trans"
> 1 | import { Trans } from '@lingui/macro';
    |          ^^^^^

package.json:

    "@lingui/loader": "^3.10.2",
    "@lingui/macro": "^3.10.2",
    "@lingui/react": "^3.10.2",
    "babel-plugin-macros": "^3.1.0",
    [... elided for brevity]
  },
  "devDependencies": {
    "@babel/core": "^7.14.8",
    "@lingui/cli": "^3.10.2",
    [... elided for brevity]
  }```

@Manubi
Copy link

Manubi commented Aug 2, 2021

me too at latest lingui

Still seeing this issue, even with NODE_ENV=production:

Invocation:

yarn run v1.22.10
$ NODE_ENV=production lingui extract --clean
Cannot process file XXX/index.tsx: Duplicate declaration "Trans"
> 1 | import { Trans } from '@lingui/macro';
    |          ^^^^^

package.json:

    "@lingui/loader": "^3.10.2",
    "@lingui/macro": "^3.10.2",
    "@lingui/react": "^3.10.2",
    "babel-plugin-macros": "^3.1.0",
    [... elided for brevity]
  },
  "devDependencies": {
    "@babel/core": "^7.14.8",
    "@lingui/cli": "^3.10.2",
    [... elided for brevity]
  }```

Exactly the same for me.

@micahbule
Copy link

micahbule commented Sep 20, 2021

Any particular update on this one? not even sure why this ticket was closed.

Dependencies:

@lingui/cli: 3.11.1,
@lingui/macro: 3.11.1

Any updates on this?

EDIT: Confirmed that adding the NODE_ENV=development flag fixes the issue (I'm still on development anyway). But for production pipelines like the one stated above, this will cause an issue.

@bernd-k1337
Copy link

I go insane but this error still lives on. Isn't this the only outstanding purpose of this library???
Why the issue was closed. Even the NODE_ENV set to development is not helping!

@semoal
Copy link
Contributor

semoal commented Dec 28, 2021

@maxfetz
In the same way, I can try to fix this issue and other issues, you can contribute to. Your comment sucks and doesn't help open-source for anything.

@micahbule

Confirmed that adding the NODE_ENV=development flag fixes the issue (I'm still on development anyway). But for production pipelines like the one stated above, this will cause an issue.

This is incorrect, on production pipelines the command lingui extract can use NODE_ENV=development and won't affect your production bundle, since it's an independent command.

I know this sucks somehow but we have life and we always try our best :) We'll try to look at this as soon as possible.

Merry Xmas to everyone.

@bernd-k1337
Copy link

bernd-k1337 commented Dec 28, 2021

Sorry for my little rant. I was very frustrated.

My project looks similar to this one: @ivandotv/nextjs-translation-demo

To reproduce the problem

  1. Clone the repository.
  2. run yarn
  3. run yarn lang:extract

Now this only works under Linux and yarn. With npm and/or Window (and yarn) you need to set the environment variable with cross-env. (Maybe somebody knows why?)

But in all circumstances, it only works with NODE_ENV=development.

@stale
Copy link

stale bot commented Feb 27, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 27, 2022
@ianatha
Copy link

ianatha commented Feb 28, 2022

no, not really stale...

@stale stale bot removed the wontfix label Feb 28, 2022
@stale
Copy link

stale bot commented Apr 30, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Apr 30, 2022
@stale stale bot closed this as completed May 25, 2022
@2snEM6
Copy link

2snEM6 commented Jun 18, 2022

Hello, this is happening to me even while I set NODE_ENV=development. Any clue why? I'm on latest version.

@motionVector
Copy link

I also had this Problem after updating all my packages. NODE_ENV=development didn't fix it. I found that the problem was the browserlists config. When I use "last 2 versions, not IE 11, not dead, > 2%" instead of only "last 2 versions" the error is fixed. I hope this helps someone else.

@iStefo
Copy link
Contributor

iStefo commented Aug 17, 2022

I was intrigued by @motionVector's comment since I also noticed a change in the number of errors reported after updating the caniuse-lite db.

I was able to narrow it down to whether the Android 4.4.3 browser should be supported by babel or not. So in our case, lingui-extract works for the following browser list, because of the last rule:

  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all",
    "not android 4.4.3"
  ]

(Of course, setting the 0.2% cutoff to a higher value would also work because it then excludes the old stock android browsers, too)

It looks like babel applies some transforms to the source code (I couldn't determine what features we used in the affected files and nowhere else, though) when android 4.4.3 should be supported.

Without NODE_ENV=development, though, a lot of those declaration errors are still throws, suggesting that babel still does some incompatible transforms in that environment.

Update: I was able to figure out what "feature" triggered babel to interfere with the lingui macro: It was affecting files where we would do a destructuring assignment at the module level:

const { Foo } = { Foo: 42 }

@phuvo
Copy link

phuvo commented Aug 31, 2022

@iStefo Yep, removing destructuring at top level fixes the issue for me 🚀

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

No branches or pull requests