Skip to content

fix(create-blocks-app,core): let the framework adapter build an existing SSR project - #1669

Open
osama-rizk wants to merge 6 commits into
mainfrom
fix/scaffold-build-values
Open

osama-rizk wants to merge 6 commits into
mainfrom
fix/scaffold-build-values

Conversation

@osama-rizk

@osama-rizk osama-rizk commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Running npx @aws-blocks/create-blocks-app . in an existing project copies the default template's backend, whose Hosting has a Vite app's build settings, for every framework:

new Hosting(blocksStack, 'Hosting', {
  ...(previewMode ? HostingPresets.preview : HostingPresets.production),
  root: join(__dirname, '..'),
  buildCommand: 'npm run build',
  buildOutputDir: 'dist',
  api: blocksStack,
});

In a Next.js, Nuxt, Astro or SvelteKit project the first npm run deploy fails:

  • Astro with server-rendered pages and no server adapter: Hosting runs npm run build before the Astro adapter, so the build fails with [NoAdapterInstalled] Cannot use server-rendered pages without an adapter. The adapter's own build would have added @astrojs/node through its config bridge, but it never gets to run.
  • Nuxt, SvelteKit, Next.js: their builds write .output/, build/ or .next/, never dist/, so the synth fails with Build output directory not found: <app>/dist.

Leaving out buildCommand doesn't help. Hosting checks that the build output dir exists before it runs the adapter (packages/core/src/hosting.ts), so a fresh checkout fails with Build output directory not found … Provide a buildCommand, even though these four adapters build the app themselves. The default output dir for a Nitro app was also dist rather than .output.

To reproduce: npm create astro@latest (minimal), add a page with export const prerender = false, then run npx @aws-blocks/create-blocks-app . --yes and npm run deploy.

Changes

  • packages/create-blocks-app/src/index.ts: adding AWS Blocks to an existing project detects the framework from its package.json (detectProjectFramework, using the same rules as detectFramework in @aws-blocks/hosting/adapters). For nextjs, nitro, astro and sveltekit it replaces the template's buildCommand/buildOutputDir (or the Next.js template's buildCommand) in aws-blocks/index.cdk.ts with framework: '<framework>'. Every other project keeps the Vite settings. The match accepts LF or CRLF template line endings and keeps them. If a template's Hosting doesn't have the expected build settings, the CLI leaves the file alone and prints the edit to make by hand. A test checks that the CLI's detection agrees with Hosting's for each rule.
  • packages/core/src/hosting.ts: with framework nextjs, nitro (or its alias nuxt), astro or sveltekit, no buildCommand and no customAdapter, a missing build output dir no longer fails the synth before the adapter runs: the adapter builds the app and reports its own missing-output error if its build produced nothing. Under BLOCKS_SKIP_FRONTEND_BUILD=1 the output still has to exist, as before. nitro and nuxt now default to .output and astro to dist.
  • Docs: packages/core/README.md (Hosting) and the buildCommand/buildOutputDir JSDoc say when buildCommand can be omitted, the customAdapter and BLOCKS_SKIP_FRONTEND_BUILD=1 exceptions, the default output dirs, that only spa/static deploy from buildOutputDir, and the full framework union and detection order. packages/create-blocks-app/README.md (Add to Existing Project) says what Hosting the CLI writes for each kind of project.
  • Changeset: patch for @aws-blocks/create-blocks-app and @aws-blocks/core.

Tests

  • packages/core/src/hosting.test.ts: Hosting with framework sveltekit, astro, nitro, nuxt or nextjs, no build output and no buildCommand runs the adapter. The adapter stops at its first step that needs the framework (the SvelteKit/Astro version check, or the npm run build of Nitro and of Next.js's edge pre-build), which proves it ran; before this change the synth failed with Build output directory not found. The output is still required with BLOCKS_SKIP_FRONTEND_BUILD=1, with a customAdapter, and when a buildCommand writes nothing.
  • packages/create-blocks-app/src/cli.test.ts: an existing Astro, Nuxt, SvelteKit and Next.js project gets framework: '<framework>' and no buildCommand/buildOutputDir; a Vite project gets the template's index.cdk.ts unchanged; detectProjectFramework matches Hosting's detectFramework for every Nitro package, precedence case and peer dependency; every template with a Hosting is matched by the rewrite, with LF and CRLF endings.

Before the fix 7 of these fail; with it all pass. The full core and create-blocks-app suites, npm run build, npm run lint:deps and npm run check:api pass.

@osama-rizk
osama-rizk requested a review from a team as a code owner November 19, 2026 13:45
Comment thread packages/create-blocks-app/src/index.ts Outdated
Comment on lines +1217 to +1222
async function fitHostingToFramework(indexCdk: string, framework: string): Promise<void> {
if (framework === 'spa' || !(await exists(indexCdk))) return;
const source = await readFile(indexCdk, 'utf-8');
const updated = source.replace(TEMPLATE_BUILD_SETTINGS, `\n framework: '${framework}',\n`);
if (updated !== source) await writeFile(indexCdk, updated);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] If TEMPLATE_BUILD_SETTINGS stops matching, this does nothing and says nothing. That can happen after a template edit (reordering props, changing indentation, a new template under --template), or when the repo templates have CRLF endings (a Windows checkout with core.autocrlf, which the local-monorepo path copies from). The project then gets the Vite buildCommand/dist again, which is the bug this PR fixes. Two suggestions: (1) make the newline \r?\n and keep the matched line ending; (2) when framework !== 'spa', the file contains new Hosting(, and nothing matched, print a warning that names the file and the edit to make by hand (framework: '<fw>', no buildCommand/buildOutputDir). Also add a test that loops over every templates/*/aws-blocks/index.cdk.ts containing new Hosting( and asserts the regex matches. Today only the default template is pinned, through the existing-project tests, so a drift in auth, react, demo and the others would go unnoticed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5bbac90. The pattern now accepts \r?\n and the replacement keeps the matched line ending. When framework !== 'spa', the file has a new Hosting( and nothing matched, the CLI leaves the file alone and prints the file path plus the edit to make by hand (framework: '<fw>', no buildCommand/buildOutputDir). The matching is now a pure fitHostingSource(), and a new test runs it over every templates/*/aws-blocks/index.cdk.ts that contains new Hosting( (agent, auth, auth-oidc, bare, default, demo, nextjs, react), with LF and with CRLF endings, and asserts that it matches, writes no mixed endings and removes buildCommand/buildOutputDir. The create-blocks-app README mentions the warning.

Comment thread packages/create-blocks-app/src/index.ts Outdated
* `detectFramework` in `@aws-blocks/hosting/adapters` (which this CLI doesn't load: the
* project may not have installed it yet). A test checks that the two agree.
*/
export function detectProjectFramework(pkg: PackageDeps & { peerDependencies?: Record<string, unknown> }): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Could the return type be 'nextjs' | 'nitro' | 'astro' | 'sveltekit' | 'spa' instead of string? fitHostingToFramework interpolates the value into generated source, so a closed union documents that only these literals reach it and lets the compiler check the 'spa' comparison.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5bbac90: detectProjectFramework now returns ProjectFramework = 'nextjs' | 'nitro' | 'astro' | 'sveltekit' | 'spa', and fitHostingSource takes Exclude<ProjectFramework, 'spa'>, so only those literals reach the generated source.

Comment on lines +278 to +302
// An adapter that builds the app itself (SvelteKit, Astro, Nuxt/Nitro, Next.js)
// needs no build before it runs: with no buildCommand, the missing output dir
// is the adapter's to build, not a synth error. Each adapter here stops at its
// version check (no framework is installed), which proves it ran.
for (const [framework, dependency, adapterError] of [
['sveltekit', '@sveltejs/kit', 'UnsupportedSvelteKitVersionError'],
['astro', 'astro', 'UnsupportedAstroVersionError'],
] as const) {
it(`runs the ${framework} adapter, which builds the app, when there is no build output and no buildCommand`, () => {
fs.writeFileSync(
path.join(tmpDir, 'package.json'),
JSON.stringify({ name: 'test', dependencies: { [dependency]: '*' } }),
);
const app = new App();
const stack = new Stack(app, `NoBuildYet-${framework}`);
assert.throws(
() => new Hosting(stack, 'Hosting', { root: tmpDir, framework, api: MOCK_API }),
(err: Error) => {
assert.ok(!err.message.includes('Build output directory not found'), `Expected the adapter to run, got: ${err.message}`);
assert.strictEqual(err.name, adapterError);
return true;
},
);
});
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] These tests cover the positive path for two of the four frameworks. The adapterBuilds condition has other branches, and none of them is tested:

  • BLOCKS_SKIP_FRONTEND_BUILD=1 + framework: 'sveltekit' (or 'astro') + no buildCommand + no output should still throw Build output directory not found and name the variable. The existing SKIP tests near line 4460 all pass buildCommand, so they never reach the reuseBuild !== 'required' term.
  • customAdapter + framework: 'astro' + no output should still throw Build output directory not found.
  • framework: 'astro' + a buildCommand that writes nothing should still throw Build output directory not found.
  • nitro and nextjs: the comment lists them, but the loop doesn't include them. Nitro with no build script in package.json fails in the adapter's npm run build (NitroBuildError), which would also prove the adapter ran.

The version-check assertion also depends on where the tmp dir is. The repo root node_modules contains astro (and next, nuxt), and tmpDir comes from os.tmpdir(). If TMPDIR points inside the checkout, getProjectPackageInfo('astro', tmpDir) can find the root install, and the test then runs a real astro build and fails with a different error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 764371b:

  • The loop now covers all four frameworks plus the nuxt alias. Nitro and nuxt stop at NitroBuildError (no build script). Next.js stops at NextBuildError, from its edge-route pre-build (the fixture has an app/page.tsx with runtime = 'edge'), so OpenNext is never fetched.
  • BLOCKS_SKIP_FRONTEND_BUILD=1 + framework: 'sveltekit' + no buildCommand + no output still throws Build output directory not found and names the variable (in the skip-build describe, which saves and restores the env).
  • customAdapter + framework: 'astro' + no output still throws.
  • framework: 'astro' + a buildCommand that writes nothing still throws.
  • For the tmp dir: each adapter test now skips with a message when the framework resolves from a node_modules in a parent of the tmp dir (TMPDIR inside the checkout), instead of running a real framework build.

While doing this I found that framework: 'nuxt' (the alias the registry keeps) wasn't in the self-building set and defaulted to dist. It now matches nitro (.output), with a test.

Comment thread packages/core/src/hosting.ts Outdated
Comment on lines +244 to +246
* Can be omitted for `nextjs`, `nitro`, `astro` and `sveltekit` too: their
* adapters build the app themselves, so a missing build output dir is not an
* error then (the adapter builds it).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Please add the two exceptions the code makes: the output still has to exist with a customAdapter, or under BLOCKS_SKIP_FRONTEND_BUILD=1. It would also help to mention, here or on buildOutputDir, that these four adapters read their own output dir (getAdapter passes buildOutputDir only to spa/static). Setting buildOutputDir for them only changes the existence check, and without a buildCommand it now has no effect at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 764371b. The buildCommand JSDoc now names both exceptions (a customAdapter, or BLOCKS_SKIP_FRONTEND_BUILD=1: the output dir still has to exist). buildOutputDir now says that only the spa and static adapters deploy from it, that nextjs/nitro/astro/sveltekit read their own output dir, so for them it only sets the dir that gets checked, and that without a buildCommand it has no effect.

Comment thread packages/core/README.md Outdated
@@ -492,7 +492,11 @@ new Hosting(stack, 'Web', {

The `framework` option selects the frontend type: `'spa' | 'static' | 'nextjs'`. When omitted, the framework is auto-detected by reading your app's OWN `package.json` (not `node_modules`): a `next` dependency → `nextjs`; otherwise `spa`; and `static` when there is no `package.json`. Set `framework: 'spa'` explicitly to override auto-detection — e.g. when a stray `next` dependency would otherwise trigger an unwanted Next.js/OpenNext build. Full reference lives in the source JSDoc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minor] This line is not changed by the PR, but the new paragraph below it and the create-blocks-app README ("which the CLI detects from package.json as Hosting does") now contradict it. detectFramework also returns nitro (nuxt, nitropack, @solidjs/start, @analogjs/platform-server, @tanstack/start), astro and sveltekit (@sveltejs/kit), in the order next → Nitro → astro → sveltekit → spa, and the option accepts those values. Suggest listing the full union and the detection order here, so a reader of the new paragraph knows when the "leave buildCommand out" behaviour applies without setting framework.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 764371b. That paragraph now lists the full union ('nextjs' | 'nitro' | 'nuxt' | 'astro' | 'sveltekit' | 'spa' | 'static', with nuxt as an alias of nitro) and the detection order: next → nuxt/nitropack/@solidjs/start/@analogjs/platform-server/@tanstack/start → astro → @sveltejs/kit → spa. It also fixes an older inaccuracy: with no package.json, detectFramework returns spa, not static (readProjectDeps returns {}), so the README now says static is never detected.

Comment thread packages/core/README.md Outdated
`buildCommand` runs during synth, with `BLOCKS_API_URL` set to the backend the frontend talks to, and then the framework adapter runs its own build (OpenNext, Nitro, Astro, SvelteKit). Two synths skip both:
`buildCommand` runs during synth, with `BLOCKS_API_URL` set to the backend the frontend talks to, and then the framework adapter runs its own build (OpenNext, Nitro, Astro, SvelteKit).

The Next.js, Nuxt (Nitro), Astro and SvelteKit adapters build the app themselves, so their apps can leave `buildCommand` out: `Hosting` then runs the adapter even when no build output exists yet, and the adapter's build adds what the deploy needs (OpenNext, Astro's and SvelteKit's Node server adapter). An Astro app without a server adapter needs this, because its own `npm run build` fails before the adapter can add one. With a `buildCommand`, or for `spa` and `static`, the build output dir (`buildOutputDir`, else `dist`, `.next` for Next.js, `.output` for Nitro, `build` for SvelteKit) has to exist when the build has run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] "With a buildCommand, or for spa and static": please add "or with a customAdapter". The code (!props.customAdapter) requires the output dir in that case too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 764371b. It now reads "With a buildCommand, with a customAdapter, on a deploy with BLOCKS_SKIP_FRONTEND_BUILD=1, or for spa and static", and the paragraph adds that only spa/static deploy from buildOutputDir.

@osama-rizk
osama-rizk requested a review from soberm November 19, 2026 15:27
@osama-rizk
osama-rizk force-pushed the fix/scaffold-build-values branch 2 times, most recently from 4ff62e6 to db2ef5a Compare November 19, 2026 15:38
@osama-rizk

Copy link
Copy Markdown
Contributor Author

I rebased onto main to pick up the cdk-blocks/ folder rename (#1630) and #1655, so the review fixes now have new SHAs: 5bbac90 → ac75f34 (create-blocks-app), 764371b → d9fd5e9 (core tests and docs), 77881df → db2ef5a (allowlist line, changeset). The template test now reads templates/*/cdk-blocks/index.cdk.ts.

…ing SSR project

Adding AWS Blocks to an existing Next.js, Nuxt, Astro or SvelteKit project
wrote Vite's buildCommand and buildOutputDir into Hosting. The added backend
now names the framework and leaves the build to the adapter, and Hosting runs
a self-building adapter when there is no buildCommand and no build output yet.
… be fitted, type the detected framework

Every template with a Hosting is now checked against the build settings the CLI replaces, with LF and CRLF line endings.
…ll need the build output

Adds the nuxt alias to the self-building frameworks, and documents the customAdapter and BLOCKS_SKIP_FRONTEND_BUILD exceptions, which output dir each adapter reads, and the full framework detection order.
…its new line; note the nuxt alias in the changeset

This branch was previously deployed

2 inactive deployments
agent-bench — 30b416e1 Deployed Nov 20, 2026 by osama-rizk via bench / summary #6752
e2e — 30b416e1 Deployed Nov 20, 2026 by osama-rizk via E2E Teardown Verify #7728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants