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

Cannot process file /<path>.tsx: Duplicate declaration "Trans" #952

Closed
ericvera opened this issue Jan 18, 2021 · 37 comments · Fixed by #1367
Closed

Cannot process file /<path>.tsx: Duplicate declaration "Trans" #952

ericvera opened this issue Jan 18, 2021 · 37 comments · Fixed by #1367

Comments

@ericvera
Copy link

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

{
  "presets": ["next/babel"],
  "plugins": ["macros"],
  "env": {
    "test": {
      "plugins": ["dynamic-import-node"]
    }
  }
}
@semoal
Copy link
Contributor

semoal commented Jan 18, 2021

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

@ericvera
Copy link
Author

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.

@semoal
Copy link
Contributor

semoal commented Jan 18, 2021

#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) :)

@semoal
Copy link
Contributor

semoal commented Jan 18, 2021

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

@ericvera
Copy link
Author

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?

@semoal
Copy link
Contributor

semoal commented Jan 18, 2021

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..

@ericvera
Copy link
Author

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"
   ...
}

@fhuszti
Copy link

fhuszti commented Jan 22, 2021

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?

@semoal
Copy link
Contributor

semoal commented Jan 25, 2021

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

@semoal
Copy link
Contributor

semoal commented Jan 27, 2021

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 =)

@semoal semoal added next release 📦 Issues resolved in next release triage: accepted bug labels Jan 27, 2021
@ericvera
Copy link
Author

ericvera commented Jan 27, 2021 via email

@semoal
Copy link
Contributor

semoal commented Feb 2, 2021

@ericvera Released 3.5.0 give it a try plz

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

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

@semoal
Copy link
Contributor

semoal commented Feb 2, 2021

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

@SleepWalker
Copy link
Contributor

SleepWalker commented Feb 2, 2021

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 😅

@SleepWalker
Copy link
Contributor

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 😅. I'll continue to use NODE_ENV=development

@ericvera
Copy link
Author

ericvera commented Feb 2, 2021

@ericvera Released 3.5.0 give it a try plz

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

@armandabric
Copy link

After upgrading from v3.2.3 to v3.6.0 I encounter the same error. Forcing the NODE_ENV to development fix the issue.

@stale
Copy link

stale bot commented May 2, 2021

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.

@Manubi
Copy link

Manubi commented Aug 3, 2021

Just wanted to let you know, I just switched to lingui and the fix was the node enviornment.
yarn run NODE_ENV=development lingui extract --clean && lingui compile

@Anonymous-dev-v
Copy link

Anonymous-dev-v commented Oct 6, 2021

The error is still there. Duplicate declaration "Trans"

@gemcloud
Copy link

I fixed the issue on my nextjs project on windows 10, windows 11!!!
Points are: 1 & 2
1. First of all you must install "npm i cross-env" on windows system (windows 10, 11).
2. You have to use "NODE_ENV=development" command
Notice: cross-env NODE_ENV=development! cross-env NODE_ENV=development!! cross-env NODE_ENV=development!!!
use below command on "package.json"

    "lang:extract": "cross-env NODE_ENV=development lingui extract --clean",
  1. run "> yarn lang:extract" on console. each "messages.po" file created!
  2. installed package version as below
    "@lingui/react": "^3.13.0",
    "@lingui/cli": "^3.13.0",
    "@lingui/loader": "^3.13.0",
    "@lingui/macro": "^3.13.0",

Hopeful help!

@stale
Copy link

stale bot commented May 25, 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 May 25, 2022
@armandabric
Copy link

This is still a thing

@stale stale bot closed this as completed Jun 10, 2022
@2snEM6
Copy link

2snEM6 commented Jun 18, 2022

This is still happening to me in the latest version. I think this is not fixed

@NitrousBGC
Copy link

I fixed the issue on my nextjs project on windows 10, windows 11!!! Points are: 1 & 2 1. First of all you must install "npm i cross-env" on windows system (windows 10, 11). 2. You have to use "NODE_ENV=development" command Notice: cross-env NODE_ENV=development! cross-env NODE_ENV=development!! cross-env NODE_ENV=development!!! use below command on "package.json"

    "lang:extract": "cross-env NODE_ENV=development lingui extract --clean",
  1. run "> yarn lang:extract" on console. each "messages.po" file created!
  2. installed package version as below
    "@lingui/react": "^3.13.0",
    "@lingui/cli": "^3.13.0",
    "@lingui/loader": "^3.13.0",
    "@lingui/macro": "^3.13.0",

Hopeful help!

This did the trick for me ! Thank you!

@mohanads
Copy link

mohanads commented Aug 19, 2022

I ran into this and the NODE_ENV=development work-around didn't work.
However, removing the @babel/preset-env preset from my .babelrc fixed this issue.

@Strnadj
Copy link

Strnadj commented Aug 30, 2022

I ran into this again, @babel/preset-env is not a part of my .babelrc... in case I remove it completely, lingui starts to complain.

@ianchanning
Copy link

I'm too using Next.js and I've been using the NODE_ENV=development fix for quite a while, but today for some reason or another I started getting the same errors:

Cannot process file /src/components/hooks/useNotifications.js: Duplicate declaration "Trans"
  1 | import React, { useEffect, useState, useContext, useRef } from 'react'
> 2 | import { Trans } from '@lingui/macro'
    |          ^^^^^

I was using lingui 3.12.1 but I updated to the current latest 3.14.0.

I noticed that without using NODE_ENV=development all files gave errors, but with using it about only 9 files gave errors. For these ones I replaced <Trans> macro with i18n from useLingui() and the t macro instead.

i18n._(t`...`)

These fixed my errors one by one.

@athammer
Copy link

Also using next.js.

I tried downgrading all the way to 3.0 with no luck. I'm currently on 3.14.0. I also had to use the t macro function sadly. I'll keep digging to see if I can find the route of the issue.

@capaj
Copy link
Contributor

capaj commented Sep 15, 2022

This lingui:extract": "NODE_ENV=development lingui extract should really get documented as all the people who use next.js will have to set this for the extraction to work properly.

@athammer
Copy link

athammer commented Sep 21, 2022

Possible solution here? In the meantime the band aid solution still seems to use cross-env and set NODE_ENV=development .

Not sure what exactly the fix for the library is but hopefully this can point someone in the right direction to fix this. I'm guessing this is impacting pretty much everyone trying to use this library right now.

@timofei-iatsenko
Copy link
Collaborator

Looks like its still an issue. I reopen this and assign for myself.

@timofei-iatsenko
Copy link
Collaborator

Fixed in #1367 would be released as part of v4

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