opennextjs / opennextjs-netlify

Open Next.js adapter for Netlify
https://opennext.js.org/netlify
679 stars 87 forks source link

[Bug]: `getPageResolver` generating incorrect paths on Windows #941

Closed rshackleton closed 2 years ago

rshackleton commented 2 years ago

Steps to reproduce

When using the latest RC version of the plugin all SSR pages in Next.js are failing to find the styled-components dependency imported in _document.ts.

This is happening with and without the esbuild function bundle option. The repo is a private repo so I cannot share this currently, I can see about trying to put together a reproduction repo but that will take some time.

A link to a reproduction repository

No response

Plugin version

^4.0.0-rc.1

More information about your build

What OS are you using?

Windows

Your netlify.toml file

`netlify.toml` ```toml [build] command = "yarn build" publish = ".next" [functions] node_bundler = "esbuild" [[plugins]] package = "@netlify/plugin-nextjs" ```

Relevant log output (or link to your logs)

Build logs ``` 1:45:45 PM: f3de4f18 ERROR Error: Cannot find module 'styled-components' Require stack: - /var/task/packages/web/.next/server/pages/_document.js - /var/task/node_modules/next/dist/server/require.js - /var/task/node_modules/next/dist/server/load-components.js - /var/task/node_modules/next/dist/server/next-server.js - /var/task/packages/web/.netlify/functions-internal/___netlify-odb-handler/handlerUtils.js - /var/task/packages/web/.netlify/functions-internal/___netlify-odb-handler/___netlify-odb-handler.js - /var/task/___netlify-odb-handler.js - /var/runtime/UserFunction.js - /var/runtime/index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15) at Function.Module._load (internal/modules/cjs/loader.js:746:27) at Module.require (internal/modules/cjs/loader.js:974:19) at require (internal/modules/cjs/helpers.js:93:18) at Object.7518 (/var/task/packages/web/.next/server/pages/_document.js:115:18) at __webpack_require__ (/var/task/packages/web/.next/server/webpack-runtime.js:25:42) at Object.8684 (/var/task/packages/web/.next/server/pages/_document.js:16:75) at __webpack_require__ (/var/task/packages/web/.next/server/webpack-runtime.js:25:42) at __webpack_exec__ (/var/task/packages/web/.next/server/pages/_document.js:125:39) at /var/task/packages/web/.next/server/pages/_document.js:126:70 { code: 'MODULE_NOT_FOUND', requireStack: [ '/var/task/packages/web/.next/server/pages/_document.js', '/var/task/node_modules/next/dist/server/require.js', '/var/task/node_modules/next/dist/server/load-components.js', '/var/task/node_modules/next/dist/server/next-server.js', '/var/task/packages/web/.netlify/functions-internal/___netlify-odb-handler/handlerUtils.js', '/var/task/packages/web/.netlify/functions-internal/___netlify-odb-handler/___netlify-odb-handler.js', '/var/task/___netlify-odb-handler.js', '/var/runtime/UserFunction.js', '/var/runtime/index.js' ] } ```
ascorbic commented 2 years ago

Hey. Thanks for the report. I think I am going to need a repro from you, because I've failed to reproduce it myself. See #943 and the deploy preview

rshackleton commented 2 years ago

I'm thinking it might be due to it being in a monorepo. It is a Sanity project so we have the following structure:

package.json
packages/
  studio/
    package.json
  web/
    package.json

Perhaps the Netlify function isn't able to bundle the packages from the monorepo properly? I should have some time free tomorrow to try and investigate and put together a reproduction. 🀞

ascorbic commented 2 years ago

Thank you. It shouldn't be a problem with it being a monorepo, as we have several monorepo test sites, but it may still be related. A repro would be useful. It would also be helpful if you could run netlify build locally, and share the content of .netlify/functions-internal/___netlify-odb-handler/pages.js

rshackleton commented 2 years ago

The pages.js file from running the netlify build locally. Not sure if this is what is generated on the server though, can't see how it could possible resolve from here if it was.

// This file is purely to allow nft to know about these pages. It should be temporary.
exports.resolvePages = () => {
    try {
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/404.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/500.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/[slug].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/_app.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/_document.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/_error.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/formium/[formid].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/indexers/products-full.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/indexers/products-webhook.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/indexers/site-full.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/indexers/site-webhook.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/media.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/api/vacancies.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/applications/[application].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/applications/[application]/[subapplication].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/index.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/media.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/media/[slug].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/products.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/products/[category].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/products/[category]/[subcategory].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/products/[category]/[subcategory]/[product].js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/search-results.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/sitemap.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/sitemap.xml.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/vacancies.js')
        require.resolve('../../../C:\Projects\ClientName\packages\web\.next/server/pages/vacancies/[slug].js')
    } catch {}
}
rshackleton commented 2 years ago

We're using Azure DevOps for our CI process and decided to try running the same build/release process on a Unix agent and it appears to have resolved the issue for us. There must be something going awry on Windows agents specifically.

ascorbic commented 2 years ago

Oh yes that does look like it! Thanks for the report. I will rename this issue and prioritise a fix.

ascorbic commented 2 years ago

Can you confirm which version of Node is being used in the build? It would be useful if you could share the build logs too. We run integration tests on Windows which include snapshots of the pages.js file, so it does seem to generate the correct ones there at least.

rshackleton commented 2 years ago

Sorry just checked back in on this, the node version would have been 14.16.0. To be honest it should have been updated to use the latest LTS but that was how the release was setup when we last ran it on the Windows agent.

I can share the log directly if you're able to provide a contact email or similar. Would prefer not to post the entire log on here just in case.

ascorbic commented 2 years ago

I have a probable solution. I'll see if I can get that in a release today.

ascorbic commented 2 years ago

This should be fixed in rc.2

rshackleton commented 2 years ago

@ascorbic I've updated to rc.2 and ran the release process on the Windows agent and we're getting the entire site timing out with this error now.

{"errorMessage":"2021-12-10T14:37:39.530Z 43f7001e-3228-4aa8-ad43-fa9a3d2abb6d Task timed out after 10.01 seconds"}

image

ascorbic commented 2 years ago

@rshackleton oh dear. Can you share the pages.js file again? Does it still work with the linux agent? I have been thinking about it, and realise that there are svereal things that could cause problems deploying from a Windows agent, because it's building functions that are deployed to Lambda functions running on Linux. I would liek to get builds working properly on Windows, but this is somehting to bear in mind.

rshackleton commented 2 years ago

We're ok using the hosted Ubuntu agents for now, and we're looking at setting up our own dedicated Ubuntu agent to run alongside our Windows agents so that should be fine for us in the long run.

I'll try and get the pages.js file and logs to you but we're currently trying to fix the stack overflow issue that has come up with installing the latest Netlify CLI. 😫

ascorbic commented 2 years ago

Would that be this issue?

rshackleton commented 2 years ago

Yep, we were using the CLI via a global install so we're updating the CI to use it via a dev dependency. That is fixed now and I think the timeout issue might actually be due to me enabling ISR earlier on - I wasn't able to properly check that was working properly because of the release issues. πŸ€¦β€β™‚οΈ

Just undoing those changes so we can verify it is working on the Ubuntu agent again, then I can try it on the Windows agent and see how it works out.

It has been a bit of a day. πŸ˜…

rshackleton commented 2 years ago

Ok, so we're back up and running. Tested the build on rc.2 on both Ubuntu and Windows agent and both are working great now, no more dependency issue even on the Windows agent!

ascorbic commented 2 years ago

Oh wow, that's great news! Thanks so much for your help in tracking this down.

rshackleton commented 2 years ago

No worries, cheers for getting a fix out so promptly!