jetbridge / cdk-nextjs

Deploy a NextJS application using AWS CDK
https://constructs.dev/packages/cdk-nextjs-standalone
Apache License 2.0
273 stars 45 forks source link

v3.2.x uses the latest open-next by default, but it's not compatible with open-next@3.0.0 #213

Closed myles2007 closed 2 months ago

myles2007 commented 5 months ago

A few hours ago, I started experiencing build problems unrelated to any recent changes in my project.

After some digging, I realized that open-next published v3.0.0 about 6 hours ago and that cdk-nextjs@v3.2.1 runs npx --yes open-next build with no apparent version specification specification anywhere. As I understand it, this means the latest version on npm will be executed and this is consistent with what I'm seeing (up to and including the build process stating OpenNext v3.0.0 is used).

Specifying a build command like npx --yes open-next@^2 build or installing open-next@2.3.9 as a part of my package dependencies resolves the problem.

There is nothing broken with open-next that I've found, but the default build command in cdk-nextjs@3.2.x is no longer working as of open-next@3.0.0 (at least in some scenarios) unless open-next is declared directly as a project dependency, this is the version that is used to build the target project.

It looks like the beta versions of cdk-nextjs (i.e., v4.0.0-beta.x) won't have this problem because it specifies a version constraint: https://github.com/jetbridge/cdk-nextjs/blob/3a9a84f56f7d78abd4be5e452ab6f6514998dab5/src/NextjsBuild.ts#L145

I believe the v3.2.x problem can be resolved by adopting the same default command as in v4.0.0-beta.x and would be happy to take a stab at the fix if that is agreeable.

myles2007 commented 5 months ago

In case it's helps anyone to the source of their own issue, I initially encountered this via an error stating that the server-function directory could not be found. Looking at the files on disk, I noticed only server-functions (plural) was present.

With some minor deidentification, the error was:

/path-to/git/project/node_modules/cdk-nextjs-standalone/src/NextjsBuild.ts:149
      if (!silent) throw new Error(Could not find ${standaloneDir} directory.);
                         ^
Error: Could not find /path-to/git/project/project-ui/.open-next/server-function directory.
    at NextjsBuild._getOutputDir (/path-to/git/project/node_modules/cdk-nextjs-standalone/src/NextjsBuild.ts:149:26)
    at new NextjsBuild (/path-to/git/project/node_modules/cdk-nextjs-standalone/src/NextjsBuild.ts:69:33)
    at new Nextjs (/path-to/git/project/node_modules/cdk-nextjs-standalone/src/Nextjs.ts:131:22)
    at new ProjectUiStack (/path-to/git/project/src/stack.project-ui.ts:118:30)
    at new ProjectWebApp (/path-to/git/project/src/construct.projectwebapp.ts:21:21)
    at Object.<anonymous> (/path-to/git/project/src/main.ts:9:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module.m._compile (/path-to/git/project/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .ts] (/path-to/git/project/node_modules/ts-node/src/index.ts:1621:12)

Subprocess exited with error 1

A quick search showed that at version v3.2.x, cdk-nextjs hardcodes an expectation of the name server-function: https://github.com/jetbridge/cdk-nextjs/blob/2bf3c44db4b32027cab55c1ff85ed3f3ec8a41d0/src/NextjsBuild.ts#L13

This led me to look for changes in open-next and to https://github.com/sst/open-next/blame/ee5794559ede83c0982b1f0a9076d2a0fd9e03a6/packages/open-next/src/build/createServerBundle.ts#L126 and subsequently https://github.com/sst/open-next/pull/402 (OpenNext v3) which made clear open-next had changed recently. This sent me down the path of looking for a way that could happen without a specification change in our project and ultimately to everything outlined above.

myles2007 commented 5 months ago

I also left a message in the Discord group to hopefully help others find a solution if they also experience this issue: https://discord.com/channels/983865673656705025/1027265626085019769/1236072672572477552

khuezy commented 5 months ago

Thanks for the investigation!

bestickley commented 2 months ago

v4.0.0 was just released which should resolve this issue. Please reopen if the issue persists.