Repository navigation
Conversation
…de the range Hosting builds server-rendered Adding AWS Blocks to an existing app on Next.js 14 said nothing about the version. Hosting builds a server-rendered Next.js app with OpenNext, whose `next` peer range is `>=15.5.28 <16 || >=16.4.1` for the version Hosting falls back to, and its adapter warns about a version outside that range only during the hosting build of the first deploy. The existing-project path now prints a warning that names the version found (the installed one from node_modules/next, else the range in package.json), the supported range, and that upgrading is the user's step. It checks against the same range as the adapter: the `next` peer range of an installed @opennextjs/aws, else a copy of the fallback range, which a test compares with the adapter's constant. It is printed only when the added backend defines a Hosting, never fails the command and edits nothing. A static export served with `framework: 'static'` has no minimum version, and the warning says so. create-blocks-app gains `semver` as a dependency to evaluate the ranges. Validation: npm run build; npm run lint; npm run lint:deps; npm test -w @aws-blocks/create-blocks-app (354 pass). Full unit suite and local e2e suite not run locally.
🦋 Changeset detectedLatest commit: c380e4b The changes in this PR will be included in the next version bump. This PR includes changesets to release 5 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
…-next-range-warning
The adapters resolve a project's packages from `<projectDir>/package.json` so the app's own copy wins over one hoisted to a workspace root. For a package that isn't installed, the resolver also tries `<base>/_index.js`, which under that base is `package.json/_index.js`; that fails with ENOTDIR instead of not-found, and local-pkg prints the error and its stack trace. A Next.js build of an app without @opennextjs/aws printed it on every run. Resolve from `<projectDir>/` instead: the lookup still starts in the app's directory, and a missing package is a plain not-found.
…g/nextjs-compatibility A tool that adds AWS Blocks to a project (create-blocks-app) wants to tell the project, before its first deploy, that its `next` is outside the range the OpenNext Hosting builds with supports. Copying Hosting's fallback range into such a tool goes stale against the Hosting release the project installs, and a second lookup misses hoisted installs. `checkNextjsCompatibility(projectDir)` returns the check the Next.js adapter makes before its OpenNext build as data: the installed `next`, the OpenNext that would build it (installed, else the fallback) with its `next` peer range, where it came from, and whether the version is inside it. The adapter now takes its warning from it, and looks OpenNext up itself only when there is no verdict, so it prints the same warnings as before. The fallback constants and the installed-version lookups move to `adapters/shared/opennext_version.ts` so the subpath loads none of the adapter's build tooling; `adapters/nextjs.ts` re-exports them. The import lines this adds move an allowlisted regex sink in `nextjs.ts` from line 1042 to 1048, updated in the sink-guard allowlist.
…ject's Hosting The warning for an existing project's `next` checked a copy of Hosting's fallback range, which describes the sibling Hosting build, not the Hosting release the project installs, and its own lookup missed hoisted installs and a `next` declared only as a peer dependency. After the install, the CLI now resolves `@aws-blocks/hosting/nextjs-compatibility` the way the backend resolves `Hosting` (from `aws-blocks/` through @aws-blocks/blocks and @aws-blocks/core), calls `checkNextjsCompatibility` and warns when it says the version is unsupported, naming the OpenNext the range is from. It stays silent when that Hosting is missing, predates the export, throws or gives no verdict. Without an installed `next` there is no verdict, so the warning from the range package.json declares is gone. The copied range, the test pinning it, and the `semver` and `@types/semver` dependencies are removed.
…ith Next.js The check ran for every backend that defines a Hosting, and the Hosting verdict reads any `next` the project resolves, including one hoisted from another app of a monorepo. Only the `nextjs` template sets `framework: 'nextjs'`; the other templates let Hosting detect the framework from package.json, so a Vite app that declares no `next` is built with another adapter, yet got an upgrade warning that said its hosting build would use OpenNext. Check only when the backend sets `framework: 'nextjs'` or package.json declares `next` in `dependencies`, `devDependencies` or `peerDependencies`, the maps Hosting's detection reads.
| // What the project's own node_modules holds of each package: its package.json, or `undefined`. | ||
| const [openNext, next] = await Promise.all( | ||
| ['@opennextjs/aws', 'next'].map((name) => | ||
| readFile(join(targetDir, 'node_modules', name, 'package.json'), 'utf-8').then((text) => JSON.parse(text), () => undefined), |
There was a problem hiding this comment.
() => undefined handles only a failed readFile. When node_modules/next/package.json exists but is not valid JSON, JSON.parse throws, the promise rejects, and the command exits 1 after aws-blocks/ and package.json have been written.
Suggestion: .then(JSON.parse).catch(() => undefined)
| // `installed` is the version in node_modules/next, read before the range package.json declares. | ||
| // `openNextPeer` is the `next` peer range of an @opennextjs/aws the project installs, which Hosting | ||
| // then builds with and checks against, instead of the OpenNext it falls back to (HOSTING_NEXT_RANGE). | ||
| type Project = { declared?: string; installed?: string; openNextPeer?: string; template?: string; warns?: string }; |
There was a problem hiding this comment.
Every case puts next in dependencies, and none installs a prerelease. Removing ?? pkg.devDependencies?.next from warnIfNextOutsideHostingRange, or removing { includePrerelease: true } from the semver.satisfies call, leaves all 13 cases green.
Suggestion: add a case with next only in devDependencies that warns, and a case with an installed 16.5.0-canary.1 that does not.
| async function warnIfNextOutsideHostingRange(targetDir: string, pkg: PackageDeps): Promise<void> { | ||
| const declared = pkg.dependencies?.next ?? pkg.devDependencies?.next; | ||
| if (typeof declared !== 'string') return; | ||
| // What the project's own node_modules holds of each package: its package.json, or `undefined`. |
There was a problem hiding this comment.
This returns when next is not in dependencies or devDependencies. A project with node_modules/next@14.2.35 and no next entry in package.json gets no warning. The adapter's detectNextVersion (packages/hosting/src/adapters/nextjs.ts:1771) reads the installed next through getProjectPackageInfo and does not read package.json, so the deploy warns.
The lookup at :1136 reads only <targetDir>/node_modules. With next@16.2.0 in the project's node_modules and @opennextjs/aws (peer >=16.0.0) in the parent directory's node_modules, this prints a warning against >=15.5.28 <16 || >=16.4.1. The adapter resolves @opennextjs/aws from <projectDir>/package.json with local-pkg (shared/package_info.ts:16-25) and uses that peer range.
| * in `@aws-blocks/hosting` (`src/adapters/nextjs.ts`), which create-blocks-app does | ||
| * not depend on at runtime; `cli.test.ts` checks that the two are equal. | ||
| */ | ||
| export const HOSTING_NEXT_RANGE = '>=15.5.28 <16 || >=16.4.1'; |
There was a problem hiding this comment.
cli.test.ts:962 compares this string to OPENNEXT_FALLBACK_NEXT_PEER_RANGE from the packages/hosting build in the same checkout. @aws-blocks/hosting is a devDependency here (package.json:38, ^0.5.0) and the changeset bumps only @aws-blocks/create-blocks-app, so a hosting release that changes OPENNEXT_FALLBACK_NEXT_PEER_RANGE leaves the published CLI printing the previous range until the CLI is republished.
|
|
||
| This writes `aws-blocks/tsconfig.json`, a TypeScript project that checks the backend with Node types, and never edits your own `tsconfig`. To keep Node types out of your frontend, reference that project from the `tsconfig` that compiles the code importing `aws-blocks`, and typecheck with `tsc -b --force`: the CLI prints the steps, and [Keep Node types out of the frontend](https://github.com/aws-devtools-labs/aws-blocks/blob/main/docs/guides/upgrading-older-apps.md#keep-node-types-out-of-the-frontend) has them in full. In a Next.js project, it writes no `aws-blocks/tsconfig.json`: `next build` ignores project references, so the app's `tsconfig.json` keeps checking `aws-blocks/`. | ||
|
|
||
| In a Next.js project whose backend gets a `Hosting` (every template except the backend-only ones), the CLI also checks the project's `next` against the versions `Hosting` builds as a server-rendered app: `>=15.5.28 <16 || >=16.4.1`, the `next` peer range of the OpenNext version `Hosting` builds with (or the `next` peer range of the `@opennextjs/aws` the project has installed). It reads the installed version from `node_modules/next`, or else the range in `package.json`. Outside that range it prints a warning that names the version it found, and changes nothing. The hosting build prints a warning for that version too and goes on with a combination OpenNext was not released for, so upgrading `next` is up to you. The check does not apply to a static export (`output: 'export'`) that `Hosting` serves as a static site (`framework: 'static'`): it is not built with OpenNext and has no minimum version. |
There was a problem hiding this comment.
amplify and observability have no new Hosting( in their index.cdk.ts either, and line 162 defines the backend-only templates as api-only, backend, cron, sql. Six of the thirteen templates print no warning.
Suggestion: name the templates that add a Hosting: agent, auth, bare, default, demo, nextjs, react.
| if (inside) return; | ||
| console.log(`⚠️ Next.js: this project has next@${version ?? declared}. \`Hosting\` builds a server-rendered Next.js app with OpenNext,`); | ||
| console.log(` which supports next ${range}. Outside that range the hosting build warns and goes on`); | ||
| console.log(' with a combination OpenNext was not released for. Upgrading `next` is up to you: nothing was changed.'); |
There was a problem hiding this comment.
The command has just written aws-blocks/ and edited package.json, so "nothing was changed" reads as a statement about the whole run.
Suggestion: "your next was not changed".
…he templates it covers The warning ended with "nothing was changed" right after the command wrote aws-blocks/ and edited package.json, which reads as a claim about the whole run; it now says the project's `next` was not changed. The README and changeset said every template except the backend-only ones adds a Hosting, but observability and amplify add none either; they now name the templates. A case with `next` only in devDependencies covers the third dependency map Hosting's detection reads.
…check The paragraph restated, in a second place, how Hosting checks `next`, how it detects the framework and which templates add a Hosting. The check is documented where it lives, in the Hosting README, and the CLI's own warning says what it found.
…rd the warning
A caller of the check had to decide for itself whether Hosting builds the
project with Next.js, and to phrase the mismatch itself. Both are Hosting
facts: the framework is `props.framework ?? detectFramework(root)`, and
the adapter already has a warning for the mismatch. A tool that copies
either goes stale against the Hosting release a project installs, the
same way a copied version range does.
`checkNextjsCompatibility(projectDir, { framework })` now makes that
choice itself (`framework`, else detection from package.json) and returns
`undefined` for a project Hosting would not build with its Next.js
adapter. An unsupported verdict carries `warning`, the text the adapter
prints; the adapter prints that field, so the words exist once. The
adapter passes `framework: 'nextjs'`, so its behavior is unchanged.
`detectFramework` and `readProjectDeps` move to
`adapters/shared/detect_framework.ts`, re-exported from
`adapters/index.ts`, so the subpath still loads no adapter.
…f deciding and wording it Without a version range, the CLI still held two copies of Hosting's rules: when Hosting builds an app with Next.js (`next` in the dependency maps, or `framework: 'nextjs'`), and what a mismatch means (OpenNext, the fallback, what the hosting build does, static exports). Its changeset also listed which templates add a Hosting. The CLI now passes the `framework` its copied backend gives `Hosting`, if any, to the project's `checkNextjsCompatibility` and prints the `warning` it returns, adding only that `next` was not changed. It has no framework detection and no wording about Hosting left, and the changeset no longer restates Hosting's rules or the template list.
The import is used only as a type. The lint check runs on changed files, and moving the framework detection out of this file put it in that set.
… own The CLI loaded the project's Hosting module into its own process and guarded the call with try/catch. That stops a throw, but not what the module prints: with a malformed node_modules/next/package.json, Hosting's package lookup logs the error and its stack and returns no verdict, and the trace appeared in the CLI's output. A module that exits or hangs would end or stall the command. The CLI now runs the question in a child process with the error output dropped and a timeout, and reads the warning from the last line of its output. Whatever that module does, the command prints either its warning or nothing, and still exits 0. The CLI's own line no longer says what the hosting build does; it says where the warning is from and that `next` was not changed. The changeset describes only what the CLI does. The tests take their expectations for the repository's Hosting from that Hosting's own answer instead of stating which versions it accepts.
…meout that cannot be ignored The child process that asks the project's Hosting wrote its answer as the last line of stdout, where the module prints too: a module that printed JSON and exited was read as the answer, and one that printed after answering hid it. The answer now goes to a pipe of its own (file descriptor 3), and stdout is dropped with stderr. The timeout sent the default SIGTERM, which a process can handle without exiting; the synchronous call then waits for it forever. It now sends SIGKILL, after ten seconds. The script exits once it has answered, so a timer or socket the module leaves open does not hold it until the timeout.
…running it in a child process The child process kept everything a project's Hosting module could do (print, exit, hang) away from the command, at the cost of a script kept in a string, a process per scaffold and an extra pipe. The one case seen in practice is narrower: for a broken installed package, Hosting's package lookup logs the error and its stack trace and then answers that it has no verdict. Import the module again and mute console.error and console.warn while the check runs. A throw is still caught. A module that exits or hangs is no longer contained; it is the project's own @aws-blocks/hosting, which the project runs on every hosting build.
Problem
npx @aws-blocks/create-blocks-app . --template nextjs --yesin an existing app on Next.js 14 adds AWS Blocks and says nothing about the version. The backend it adds defines aHosting, which builds a server-rendered Next.js app with OpenNext; for anextoutside thenextpeer range of that OpenNext, the Next.js adapter warns during the hosting build of the first deploy and goes on.The first version of this PR made the scaffolder decide that itself: it copied Hosting's fallback range (
>=15.5.28 <16 || >=16.4.1) and reimplemented the adapter's check. Every such copy describes the Hosting next to the CLI, not the Hosting release the project installs (the fallback moved from OpenNext 4.0.3 to 4.1.14 while@aws-blocks/hostingstayed at 0.5.0). The same holds for the other things a scaffolder would have to know: which packages count, whether Hosting builds the app with Next.js at all, and what to tell the user.So the scaffolder now decides nothing. It asks the project's installed Hosting one question and prints the answer.
While doing this I found that Hosting's package lookup prints an
ENOTDIRerror with its stack trace whenever a project lacks the package it looks for. A Next.js build of an app without@opennextjs/awsprinted it on every run, and the scaffolder would print it too.Issue #, if available:
Changes
@aws-blocks/hostingNew public subpath
@aws-blocks/hosting/nextjs-compatibility(minor). The adapter makes this check before its OpenNext build; the subpath returns it as data:It returns
undefinedwhen Hosting would not build the project with its Next.js adapter (options.framework ?? detectFramework(projectDir)is not'nextjs', the choiceHostingitself makes), when the project resolves no validnext, or when there is no valid range to check it against. It resolves packages the way the adapter does (the app's own copy first, then hoisted copies), usesincludePrerelease: true, and reads installed versions only. It loads noaws-cdk-lib, no adapter and no build tooling (a test records every module the import resolves). Added toexportsastypes+default, like the other subpaths. It is not re-exported from@aws-blocks/blocks: the umbrella re-exports no Hosting subpath, and no export-parity rule covers Hosting.One implementation, one wording. The adapter calls
checkNextjsCompatibility(projectDir, { framework: 'nextjs' })and printswarningafter its⚠️prefix. The printed line is byte-for-byte the one onmain(compared againstmain's source for a fixture project), the warning conditions and build arguments are unchanged, and the adapter's existing tests pass untouched. It looks up OpenNext itself only when there is no verdict, so it makes the same lookups as before.Moved, not copied.
OPENNEXT_FALLBACK_VERSION,OPENNEXT_FALLBACK_NEXT_PEER_RANGE,detectNextVersionand the installed-OpenNext lookup move toadapters/shared/opennext_version.ts;detectFrameworkandreadProjectDepsmove toadapters/shared/detect_framework.ts.adapters/nextjs.tsandadapters/index.tsre-export the names they exported before, so existing imports and tests are unchanged. None of them is exported from the new subpath.Package lookup without a stack trace (patch).
adapters/shared/package_info.tsresolved from<projectDir>/package.json. For a package that isn't installed, the resolver also tries<base>/_index.js, herepackage.json/_index.js, which fails withENOTDIRinstead of not-found, andlocal-pkgprints it with its stack. It now resolves from<projectDir>/: the app's own copy still wins over a hoisted one, and a missing package is a plain not-found.README: a paragraph in "Next.js: the OpenNext version", with an example.
@aws-blocks/create-blocks-appHOSTING_NEXT_RANGE, thesemverand@types/semverdependencies (and theirpackage-lock.jsonlines) and the test that compared the copy with the adapter's constant are gone.Hosting, the existing-project path resolves@aws-blocks/hosting/nextjs-compatibilitythe way that backend resolvesHosting: fromaws-blocks/to@aws-blocks/blocks, then the@aws-blocks/corethat it importsHostingfrom, then that core's@aws-blocks/hosting. That is never the CLI's own copy. It callscheckNextjsCompatibility(targetDir, { framework }), whereframeworkis whatever the copied backend gives itsHosting(only thenextjstemplate gives one), and prints thewarningit returns, followed by one line of its own: the warning is from the@aws-blocks/hostingthe project installs, and yournextwas not changed.nextjs.import()and stays silent, with no error or stack trace, when the module can't be resolved (--skip-installwithout Blocks, or a Hosting that predates the export), when loading or calling it throws, or when there is no warning.console.errorandconsole.warnare muted while the check runs, because Hosting's package lookup logs an error and its stack for a broken installed package before it answers that it has no verdict. It never fails the command and never editsnext.packages/create-blocks-app/README.mdis unchanged againstmain. The check is documented in the Hosting README.The review comments on the first version
JSON.parseof a malformednode_modules/next/package.jsonrejected and exited 1: that lookup is gone. With such a file, Hosting's own lookup logs the parse error and gives no verdict; the CLI mutesconsole.errorduring the check, so its stderr stays empty and it exits 0 (checked by hand with this repository's Hosting, and covered by a fixture that logs an error and returns nothing).nextonly indevDependencies, or for a prerelease: both rules are Hosting's now. A prerelease case is in Hosting's tests (16.4.2-canary.1against>=16.4.1; it fails withoutincludePrerelease). Hosting's existingdetectFrameworktests covernextindependenciesand indevDependencies, and a compatibility test covers a peer-onlynext.next, and an OpenNext hoisted to a parentnode_modules: resolution is Hosting's (the app's own copy first, then hoisted). For thenextjstemplate the CLI passesframework: 'nextjs', so an undeclarednextis checked, as the build checks it. For the other templates Hosting detects the framework frompackage.json, so a Vite app with some other app'snextinstalled gets no warning; its hosting build doesn't use OpenNext.amplifyandobservabilityadd noHostingeither: the README paragraph is removed and the changeset no longer lists templates. The CLI reads the backend it copied.nextwas not changed."For a maintainer to decide
@aws-blocks/hosting. On naming:docs/design/API-DESIGN.mdG14 sayscheck*returns a boolean, and G3 prefersnullfor absence. Both are written for Building Block methods, and Hosting's own helpers returnundefined, so I kept the name.warningputs a sentence in a data result; it is there so the adapter and a caller print the same words.node_modules, in its own process: the@aws-blocks/hostingthe project installed, which the project already runs on every hosting build. It imports only that one subpath and calls only that one function. A throw is caught and logged errors are muted; a module that exited the process or hung would take the command with it. An earlier revision on this branch ran the check in a child process to contain that; it was dropped as more machinery than the case needs.warningfield; the path@aws-blocks/blocks→@aws-blocks/core→@aws-blocks/hosting(a test with this repository's real packages fails if that path changes); and how its own templates spellnew Hosting(andframework: '…'(the tests run the real templates).package.jsonwhennextisn't installed, and the note that a static export has no minimum version (a Hosting fact that its build warning does not carry).ENOTDIRtraces for missing packages are gone. Resolution results are the same. Windows paths are not exercised by these tests.Validation
packages/hosting/src/nextjs_compatibility.test.ts.checkNextjsCompatibilitycases: an installed OpenNext'snextpeer range wins over the fallback (16.3.0 supported against 4.0.3's range, and nowarning); without OpenNext it uses the fallback version and range withopenNextSource: 'fallback', and thewarningnames the version, the OpenNext and the range; a project that declares nonexthas no verdict, and has one when givenframework: 'nextjs'; a project that depends onnexthas no verdict when givenframework: 'static'; anextand an OpenNext hoisted to a workspace root are found; the app's ownnextwins over a hoisted one; an installednextdeclared only as a peer dependency is checked; a prerelease inside the range is supported; no installednextgivesundefined; an installed OpenNext without anextpeer range, or with an invalid one, givesundefined. A child process records every module the subpath's import resolves: noaws-cdk-lib,constructs,adapters/index.js,adapters/nextjs.js,fast-globorcross-spawn.packages/hosting/src/adapters/shared/package_info.test.ts: for a missing package,getProjectPackageInforeturnsundefined,projectHasPackagereturnsfalse, and nothing is written to stderr.sub_path_exports.test.tsresolves the new subpath.nextjs.test.ts,adapters.test.tsandversion_pins.test.tspass without changes. I also compared the adapter's warning line withmain's:main's two source lines, evaluated with the values of a fixture project, equal⚠️ ${warning}\nfrom this branch (a one-off script, not kept).packages/create-blocks-app/src/cli.test.tsrun the built CLI (--skip-install) on an existing project in a temporary directory outside the repository, with a fixture@aws-blocks/blocks→@aws-blocks/core→@aws-blocks/hostingchain whose answer is its own. The fixture warning names thenextin the directory it was given and theframeworkit was passed.nextjstemplate (framework nextjs); when only the Hosting nested under Core has a warning; for thedefaulttemplate (framework undefined). That fixture also logs an error and a warning, which do not reach the output.backendtemplate.nextinpackage.jsonandnode_modulesis unchanged.includePrerelease; always using the fallback range; dropping the framework choice; ignoringoptions.framework(the adapter's own tests fail too); dropping thewarningtext (the adapter's own tests fail too); importingadapters/index.jsor the adapter from the subpath; resolving from<projectDir>/package.jsonagain; resolving fromprojectDirwithout the separator.nextjs; not checking for aHostingin the backend; removing thecatch; not mutingconsole.error, or muting it withoutconsole.warn.c380e4b18, run locally on Node.js 22.23.1 afternpm ci, all exit 0:npm run build,npm run lint,npm run lint:deps(655 files). The build failed twice before it passed, for a reason in my shell and not in the repository: with some environment variables of my local setup present,next buildinpackages/admin-uistops withSecurityFatalErrorabout an advisory fornext@16.4.2. With those variables unset the same command exits 0 on the same commit, as it does in CI.npm run check:api: the API reports are unchanged. Hosting has no API report.npm run check:exports-consistency,npm run check:runtime-guard,npm run check:client-user-agentnode --test scripts/sink-guards/{regex,shell,workflows,codegen,html}.test.mjs: 35 tests. The import lines added toadapters/nextjs.tsmove its allowlisted regex sink from line 1042 to 1048; the allowlist entry is updated in the commit that moves it.npm test(every workspace): 13,278 tests, 0 failures, 10 skipped.@aws-blocks/hosting: 1,840 tests, 0 failures.@aws-blocks/create-blocks-app: 365 tests, 0 failures. The intermittentdist/secret-cli.test.jsfailure described in test(hosting,blocks): keep CLI and adapter output off the test runner's stdout #1330 did not occur in this run. One earlier full Hosting run, on work not yet committed, failed that file as a whole (1 of 1,833 tests); I did not capture the message, and the file alone then passed (32 tests).npm run test:e2e:local: 557 tests, 0 failures, 6 skippednext@14.2.35gets Hosting's warning and the CLI's line with an empty stderr; a project whosenode_modules/next/package.jsonis not valid JSON gets no warning, exit code 0 and an empty stderr.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.