Js-lingui: Cannot process file /<path>.tsx: Duplicate declaration "Trans"

Created on 18 Jan 2021  ·  17Comments  ·  Source: lingui/js-lingui

Describe the bug
When running lingui extract I get the error Duplicate declaration "Trans" ("t" seems to be OK).

To Reproduce

Cannot process file /<path>.tsx: Duplicate declaration "Trans"
  15 | // i18n
  16 | import { useI18N, Locale } from 'i18n';
> 17 | import { t, Trans } from '@lingui/macro';

Expected behavior
No error on lingui extract

Additional context

  • Tried v3.4.0 and still had issues
  • CLI version v3.2.1 repros the issue
  • CLI version v3.1.0 does not repro the issue
  • @babel/[email protected]
  • Using Lingui in a NextJS project
  • Looked at the diff (https://github.com/lingui/js-lingui/compare/v3.1.0...v3.2.1) and there are two changes that jump out, but I don't understand the code enough to troubleshoot part this point. (https://github.com/lingui/js-lingui/pull/846 and https://github.com/lingui/js-lingui/pull/854) (cc @semoal and @Bertg in case they have any ideas)
  • .babelrc
    { "presets": ["next/babel"], "plugins": ["macros"], "env": { "test": { "plugins": ["dynamic-import-node"] } } }
accepted 🐞bug

All 17 comments

Hi Eric! Thanks for using Lingui
Are you using plain lingui extract or are you passing NODE_ENV=production

Hi Eric! Thanks for using Lingui
Are you using plain lingui extract or are you passing NODE_ENV=production

I was using lingui extract with the --overwrite option, but also tried with without that option (same result). No NODE_ENV set. Also tried echo before the command to make sure it is not set.

https://github.com/lingui/js-lingui/issues/433 We have here a similar issue here, let me create a sample repo (or if you could provide us a sample repo for reproducing the issue, I could check in detail tomorrow) :)

https://codesandbox.io/s/nextjs-lingui-djzqm?file=/pages/index.tsx Here is a codesandbox that you could modify with your changes :)

I just set the NODE_ENV=test and it seems to work. I was not setting NODE_ENV=production, but I guess that would be the default?

I just set the NODE_ENV=test and it seems to work. I was not setting NODE_ENV=production, but I guess that would be the default?

Probably in some process is setting up NODE_ENV=production, or the ci.. Dunno
Is not a directly issue with lingui, this is because lingui uses source code trough AST to explore what needs to extract, so if the code is generated via NODE_ENV=production babel applies other algorithm and brokes lingui.

Anyways I'll take a look to our internal babel generator if we can force this to be in NODE_ENV=development for example..

Sounds good. For some more info...

I used yarn repro with the below package.json definition and it works. a.js just contains console.log(process.env.NODE_ENV) which prints out undefined.

"scripts": { "repro" : "cross-env NODE_ENV=development lingui extract && node a.js" ... }

Thanks guys, changing "extract": "lingui extract" to "extract": "NODE_ENV=development lingui extract" fixed it on my fresh install with NextJS.
If I understood correctly what is said here I can keep it like that and not worry about it because it doesn't need to have NODE_ENV=production when building for prod?

Exactly, basically node_env=production applies changes to code that lingui can't interpret

I think i got a fix for this "issue", babel transform api allows to overwrite NODE_ENV variable internally, so probably won't be required to use nODE_ENV=development before lingui extract command.

@ericvera next tuesday we'll release a new version with this change introduced if you can tell us if this fixes the issue, would be amazing =)

Happy to.

On Wed, Jan 27, 2021 at 9:49 AM Sergio Moreno notifications@github.com
wrote:

I think i got a fix for this "issue", babel transform api allows to
overwrite NODE_ENV variable internally, so probably won't be required to
use nODE_ENV=development before lingui extract command.

@ericvera https://github.com/ericvera next tuesday we'll release a new
version with this change introduced if you can tell us if this fixes the
issue, would be amazing =)


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/lingui/js-lingui/issues/952#issuecomment-768378752,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABUKOB3QL6EWUKJBIL64LKLS4AYW5ANCNFSM4WHYMYHA
.

@ericvera Released 3.5.0 give it a try plz

The error is still there. It still does not work without NODE_ENV=development

Alright i'll take a deeper look, tough that forcing babel to use development was enough, but looks that NODE_ENV always has precedence.

If any of you has a reproduction repo where i can try will be super

I'll try to debug it a little bit. Looks like the error happens somewhere after transformFileSync call. I have commented out that call and the error is still there.


That's because I'm using ts and therefore typescript extractor is executed :sweat_smile:

In my case the solution is to pass babelrc: false in the options here:

https://github.com/lingui/js-lingui/blob/main/packages/cli/src/api/extractors/typescript.ts#L50-L55

Here is babelrc from my project:

{
  "presets": [
    [
      "next/babel",
      {
        "preset-react": {
          "runtime": "automatic",
          "importSource": "@emotion/react"
        }
      }
    ]
  ],
  "plugins": ["@emotion/babel-plugin", "macros"]
}

The conflicting part is inside babel-plugin-transform-react-remove-prop-types which is enabled by next/babel in isProduction mode (detected from process.env:

https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/preset.ts

If babel-plugin-transform-react-remove-prop-types is configured with { removeImport: true } (removes prop-types imports from code base), it will break the extract process. The error happens because of this line:

https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/d3a618a9c72be08485f5e20659c917b300cf7b74/src/index.js#L364

I don't know what scope.crawl does under the hood.

Looks like my issue is not due to the bug inside lingui :sweat_smile:. I'll continue to use NODE_ENV=development

@ericvera Released 3.5.0 give it a try plz

Same as @SleepWalker. Also still seeing the issue with the latest version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

landsman picture landsman  ·  5Comments

mojtabast picture mojtabast  ·  4Comments

zdzarsky picture zdzarsky  ·  5Comments

LFDMR picture LFDMR  ·  7Comments

felipeko picture felipeko  ·  6Comments