hozana / next-translate-routes

Flexible and translated routes for Next.js without custom server
MIT License
115 stars 31 forks source link

Error: spawn E2BIG when running in Docker #79

Open michalpulpan opened 1 year ago

michalpulpan commented 1 year ago

Hello everyone,

I've been dealing with an interesting and, to me, not very self-explanatory error when running Next.js in Docker using the provided Dockerfile. This whole rabbit hole started after we decided to upgrade from next@13.2.4 to next@13.4.12 (not the latest version due to https://github.com/vercel/next.js/issues/54765).

Basically, what happens is that when running under dev or building and running locally on macOS with a built project, it works fine. However, after building the Dockerfile and running it, it immediately crashes with the following error:

docker run -p 3000:3000 ecommerce-base-storefront 
Error: spawn E2BIG
    at ChildProcess.spawn (node:internal/child_process:421:11)
    at spawn (node:child_process:761:9)
    at fork (node:child_process:172:10)
    at ChildProcessWorker.initialize (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:10927)
    at new ChildProcessWorker (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:10815)
    at WorkerPool.createWorker (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:6315)
    at new BaseWorkerPool (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:8183)
    at new WorkerPool (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:6111)
    at new Worker (/app/node_modules/next/dist/compiled/jest-worker/index.js:1:23381)
    at createServerHandler (/app/node_modules/next/dist/server/lib/render-server-standalone.js:21:26) {
  errno: -7,
  code: 'E2BIG',
  syscall: 'spawn'
}

I'm not blaming next-translate-routes; however, after several hours, I decided to delete all _routes.json files, and voilà - it started. So I reverted it back and started deleting routes.json one by one (with docker build after each file was deleted). It was fairly time-consuming, but I managed to find a few pages where after deleting _routes.json, projects started working again.

However, there's nothing that would allow me to relate these _routes.json files together and find some actual issue. They're all valid JSONs with a valid structure for this library (it worked under 12.2.12) and the pages, of course, build with no issues.

The worst part? I'm unable to reproduce this error in a "cleaner" environment than our main project, so I'm quite stuck in a situation where a few routes cannot be translated. I primarily wanted to ask if somebody else has seen this error as well. 😞

EDIT: Okay, so it turns out (at least now), that this error is caused by large amount of _routes.json files that are parsed (?). I endedup naming some files as stoproutes.json (so that they don't get parsed) and it works. @cvolant don't you have some inside to this, please? Can I somehow print out number of rewrites, etc. to debug this a bit more?

EDIT2: Okay, now I'm almost sure, it's caused by large amount of something. As previously mentioned - i renamed some of my routes to stoproutes.json. I set

translateRoutes: {
    routesDataFileName: "stoproutes",
    debug: true,
  },

and it works. Of course, many pages are not translated, but it works, so there's no syntactical issue or something.

cvolant commented 10 months ago

Strange... We run our project under docker too, without any issue.

Could you count the number of elements in your routes tree? Just execute the following code in the console of your browser while it displays your website:

countElements = (children) => children?.reduce((acc, child) => acc + 1 + countElements(child.children), 0) || 0;
countElements(__NEXT_TRANSLATE_ROUTES_DATA.routesTree.children)

And how many routes.json files do you have?

countRoutesFiles = (children) => children?.reduce((acc, child) => acc + countRoutesFiles(child.children) + (Object.keys(child.paths).length > 1 ? 1 : 0), 0) || 0;
countRoutesFiles(__NEXT_TRANSLATE_ROUTES_DATA.routesTree.children)

I get 80 elements and 37 routes.json files. Do you have a lot more?

michalpulpan commented 10 months ago

Hi @cvolant ,

thank you for your response! I get 108 elements and 18 routes.json. However, I've reduced number of routes.json in order to prevent this error. I tried to add few more and 109 elements and 20 files + 1 file would make it crash with this error.

Hope it makes sense like this. I've a feeling it has something to do with my _routes.json having quite a lot of languages?

{
    "/": {
        "cs": "objednavka",
        "sk": "objednavka",
        "en": "order",
        "de": "auftrag",
        "pl": "zamowienie",
        "default": "objednavka",
        "cs-CZ": "objednavka",
        "en-BE": "order",
        "en-BG": "order",
        "en-CY": "order",
        "en-DK": "order",
        "de-DE": "auftrag",
        "en-EE": "order",
        "en-FI": "order",
        "en-FR": "order",
        "en-GR": "order",
        "en-HU": "order",
        "en-IE": "order",
        "en-IT": "order",
        "en-LV": "order",
        "en-LT": "order",
        "en-LU": "order",
        "en-MT": "order",
        "en-NL": "order",
        "pl-PL": "zamowienie",
        "en-PT": "order",
        "en-RO": "order",
        "en-SI": "order",
        "sk-SK": "objednavka",
        "en-ES": "order",
        "en-SE": "order",
        "en-GB": "order",
        "de-AT": "auftrag"
    }
}

I'm honestly not sure why I don't use fallback languages as of #66 since it's quite some time since I digged into this issue and ended up banging my head against wall and just not translating some of the routes 😅

cvolant commented 10 months ago

:open_mouth: I can't imagine what your redirects/rewrites look like ! :smile: Indeed, fallback languages is probably the way to go. I hope it will solve you issue.

michalpulpan commented 10 months ago

😮 I can't imagine what your redirects/rewrites look like ! 😄 Indeed, fallback languages is probably the way to go. I hope it will solve you issue.

Well! They're pretty big 🙈. However, this ended up beeing the best solution for making Next.js locale feature work with locale-COUNTRY coding in eCommerce 👎.

michalpulpan commented 10 months ago

@cvolant So I rewrote several _routes.json into fall back languages. I tested that and it sadly did not help 😞.

cvolant commented 10 months ago

Could you tell me how big are your rewrites?

JSON.stringify(__BUILD_MANIFEST.__rewrites).length

Mine has 9300 characters...

michalpulpan commented 10 months ago

Interesting and thank you - mine in the working situation (so few _routes.json less) has 8686 characters. 🤔 So it might not be what I think...

cvolant commented 9 months ago

Difficult to say. Do you have a working configuration without Docker but with all the routes files ? If you reach ~8000 with a few routes files, you probably end up with a lot bigger rewrites objet... The redirects object could be the cause too : it is bigger than the rewrites one, but is more difficult to measure since it does not reach the browser...

michalpulpan commented 9 months ago

Hi! I'm sorry it took me that long; it has been a hectic period on my end lately.

Do you have a working configuration without Docker but with all the routes files

Yes, of course! I modified my project so that it's in the state where docker looks like:

Snímek obrazovky 2023-12-05 v 17 08 03

But npm run build && npm run start works flawlessly (actually never had issue running it outside of docker): Snímek obrazovky 2023-12-05 v 17 09 05

Commands you suggested previously output following:

  1. elements in the routes tree: 109

  2. routes.json files: 20

  3. characters in rewrites: 10814

Number of rewrites has a significant increase, but I guess it's mostly due to the fact that some of my routes look like .../.../shipping-billing-details.

Do you think that might be the cause of the issue? So that the Node worker can't maybe allocate enough memory inside of the container to store that?

Thank you very much for your help 🙏

cvolant commented 9 months ago

I am a total newbie on Docker, so I am quite helpless here, but the numbers you gave don't seem to be that much, and we use Docker too with similar numbers, without such an issue...

cvolant commented 9 months ago

I asked our Docker master Jedi, and he says that it looks more like a Node issue than a Docker issue, but that you seem to be on MacOS where Docker could behave a bit differently. Could you try to run your project on a Linux distro and check if you still have the issue? What version of Docker do you use?

michalpulpan commented 9 months ago

Thank you very much for quick response! Sadly, it crashes on both MacOS and on our production Ubuntu 22.04 with Docker 23.0.1 🙁

michalpulpan commented 9 months ago

Thank you very much for quick response! Sadly, it crashes on both MacOS and on our production Ubuntu 22.04 with Docker 23.0.1 🙁

By the way, I'm using 1:1 Dockerfile from https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile and tested node:20-alpine and node:21-alpine

LuK3zz commented 6 months ago

Is there any possible solution here?

michalpulpan commented 6 months ago

Is there any possible solution here?

Do you have the same issue? 🤔 I haven't resolved it, we just kept only most crucial urls translated and all new ones are untranslated for now.

LuK3zz commented 6 months ago

Is there any possible solution here?

Do you have the same issue? 🤔 I haven't resolved it, we just kept only most crucial urls translated and all new ones are untranslated for now.

Yes, I have the same issue 😞 I have to proceed with the solution that some languages will not have translations.

LuK3zz commented 6 months ago

That's how I managed to solve this case. It was enough to update next to version 13.5.2 and slightly modify next-translate-routes.

In version 13.5.2 a new thing cleanup was added. The new thing added in next seems to have solved this problem.

michalpulpan commented 6 months ago

That's how I managed to solve this case. It was enough to update next to version 13.5.2 and slightly modify next-translate-routes.

In version 13.5.2 a new thing cleanup was added. The new thing added in next seems to have solved this problem.

Good point! So you're now running with no issues? I was stuck on older version (13.4.2) due to this (Next.js problem)[https://github.com/vercel/next.js/issues/54765]. However I think it's time to give it a try. Would you mind sharing how did you fixed the ROUTER_CONTEXT_PATH error comming from next-translate-routes@1.11.0-4?

Thanks!

edit: nevermind, https://github.com/hozana/next-translate-routes/pull/86#issue-2049625922 seems to do the trick :)

michalpulpan commented 6 months ago

So after some trial and error, I think I'm back on previous version (both next and next-translate-routes). Even though E2BIG seems to be fine, other errors occured which are mentioned in other currently open issues. https://github.com/hozana/next-translate-routes/issues/77