/** * A freshly scaffolded app must typecheck against the framework it pinned. * * `buddy new` writes the template from THIS checkout and then pins the * framework to a PUBLISHED version. Those two can disagree: `resolvePublishedVersion` * falls back to the newest published release when the vendored version is not * on npm yet, which is the normal state of this repo between a version bump * and its publish. When the template references something added since that * release — a config field, an exported type — the scaffolded app fails * `./buddy typecheck` on its first run, with an error in a file the user did * not write. * * That has happened at least twice: `MobileConfig` in `config/mobile.ts` * against a framework that did not export it yet (stacksjs/stacks#2322), and * `security.api` in `config/security.ts` (stacksjs/stacks#2375), which this * script caught within minutes of the field landing. * * A red run here is not a false alarm on main when the vendored version IS * published: then it says `buddy new` produces a broken app right now. * * In the publish window (the vendored version is not on npm yet) the pinned * framework is by definition the PREVIOUS release, so the app can never agree * with a template that uses something this release adds, and the release that * would publish it waits on this job being green: the check could only ever * fail. Then the app is typechecked against THIS commit's core packages * (built and linked into the app's node_modules, the way the attached-site * deploy links the framework it is about to publish), which still catches the * template disagreeing with the framework it ships with. * * Deliberately runs `tsc` directly rather than `./buddy typecheck`: the action * wraps it in a subprocess whose failure surfaces as a stack through the CLI's * error handler, and the point of this job is to print the type errors. * * `--desktop` scaffolds `buddy new --desktop --minimal` instead, into a * directory that already holds a `.git` (a repository cloned before * scaffolding, the usual way to start), and also checks the Mac app's files * are there: `config/desktop.ts`, its screen and its agent routes. * * Run: `bun .github/scripts/scaffold-smoke.ts [--keep] [--desktop]` */ import { existsSync, mkdirSync } from 'node:fs' import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import process from 'node:process' import { buildFramework } from './build-framework' async function main(): Promise { const repoRoot = process.cwd() const source = Bun.spawnSync(['git', 'rev-parse', 'HEAD'], { cwd: repoRoot, stdout: 'pipe', stderr: 'pipe' }) const sourceRef = source.stdout.toString().trim() if (source.exitCode !== 0 || !/^[a-f0-9]{40}$/.test(sourceRef)) throw new Error('The scaffold smoke requires an exact source commit') const keep = process.argv.includes('--keep') const desktop = process.argv.includes('--desktop') const workspace = await mkdtemp(join(tmpdir(), 'stacks-scaffold-smoke-')) const appDir = join(workspace, 'smoke-app') if (desktop) { mkdirSync(appDir, { recursive: true }) const init = Bun.spawnSync(['git', 'init', '--quiet', appDir], { stdout: 'inherit', stderr: 'inherit' }) if (init.exitCode !== 0) { console.error('git init failed, so the .git-only target could not be prepared.') process.exit(1) } } console.log(`Scaffolding into ${appDir}${desktop ? ' (--desktop --minimal, into a directory holding only .git)' : ''}`) const scaffold = Bun.spawn(['./buddy', 'new', appDir, '--name', 'smoke-app', '--no-provision', '--ref', sourceRef, ...(desktop ? ['--desktop', '--minimal'] : [])], { cwd: repoRoot, stdout: 'inherit', stderr: 'inherit', }) const scaffoldCode = await scaffold.exited /** * CI already provides the native tools. --no-provision still installs the * complete JavaScript graph, so a nonzero exit means an incomplete app and * cannot count as a successful scaffold even if its config files exist. */ const scaffolded = ['package.json', 'tsconfig.json', 'config'].every(entry => existsSync(join(appDir, entry))) if (!scaffolded) { console.error(`\n\`buddy new\` exited ${scaffoldCode} without producing an app, so nothing was typechecked.`) process.exit(1) } if (scaffoldCode !== 0) { console.error(`\n\`buddy new --no-provision\` exited ${scaffoldCode}; the scaffold must complete before typechecking.`) process.exit(1) } if (desktop) { const missing = ['config/desktop.ts', 'resources/native/Main.stx', 'app/Desktop/Agent.ts', '.git'].filter(entry => !existsSync(join(appDir, entry))) const scripts = (await Bun.file(join(appDir, 'package.json')).json()).scripts ?? {} if (!scripts['dev:desktop'] || !scripts['build:desktop']) missing.push('package.json dev:desktop/build:desktop scripts') if (missing.length) { console.error(`\n\`buddy new --desktop\` did not write: ${missing.join(', ')}`) process.exit(1) } console.log('✓ the desktop files are there, and the .git that was already in the directory survived') } const manifest = await Bun.file(join(appDir, 'package.json')).json() const pinned = manifest.dependencies?.stacks ?? manifest.devDependencies?.stacks ?? '(none)' // The FRAMEWORK version, not the root package.json's. The root is the // playground app and carries its own unrelated version, so reporting it here // said things like "pinned ^0.74.0; this checkout is 0.70.52" — which reads // as the scaffold being ahead when it is a different number entirely. const vendored = (await Bun.file(join(repoRoot, 'storage/framework/package.json')).json()).version console.log(`\nScaffold pinned stacks@${pinned}; this checkout's framework is ${vendored}.`) const packument = await fetch('https://registry.npmjs.org/stacks').then(r => r.ok ? r.json() : null).catch(() => null) const publishWindow = Boolean(packument && vendored && !(vendored in (packument.versions ?? {}))) if (publishWindow) { console.log(`stacks@${vendored} is not on npm yet: typechecking against this commit's built core packages.`) // Config types import declarations from other framework packages. Linking // only @stacksjs/types still parses the previous release's queue/database // declarations, so an already-fixed declaration could block its own release. const builds = await buildFramework(repoRoot) if (builds.some(result => result.exitCode !== 0)) { console.error('Could not build this commit\'s core packages, so the scaffold was not typechecked.') process.exit(1) } const link = Bun.spawnSync(['./buddy', 'link:core', '--all', '--path', repoRoot, '--cwd', appDir], { cwd: repoRoot, stdout: 'inherit', stderr: 'inherit' }) if (link.exitCode !== 0) { console.error('Could not link this commit\'s built core packages into the scaffolded app, so it was not typechecked.') process.exit(1) } // The app's name registries were written while it still had the previous // release's defaults. A release that adds a middleware class, a job or an // action then typechecks the new alias map against the old list of names, // and blocks itself on a name the app does have (v0.75.574, `Honeypot`). // Write them again from the defaults that were just linked. const registries = Bun.spawnSync(['./buddy', 'generate:auto-imports'], { cwd: appDir, stdout: 'inherit', stderr: 'inherit' }) if (registries.exitCode !== 0) { console.error('Could not regenerate the scaffolded app\'s name registries from this commit\'s defaults, so it was not typechecked.') process.exit(1) } } if (!existsSync(join(appDir, 'tsconfig.json'))) { console.error('The scaffolded app has no tsconfig.json to check.') process.exit(1) } const typecheck = Bun.spawn(['bun', 'x', '--bun', 'tsc', '--noEmit', '-p', 'tsconfig.json', '--pretty', 'false'], { cwd: appDir, stdout: 'pipe', stderr: 'pipe', }) const [out, err] = await Promise.all([ new Response(typecheck.stdout).text(), new Response(typecheck.stderr).text(), ]) const code = await typecheck.exited if (!keep) await rm(workspace, { recursive: true, force: true }) if (code === 0) { console.log(publishWindow ? '✓ the scaffolded app typechecks against this commit\'s built core packages (the release is not published yet)' : '✓ the scaffolded app typechecks against the framework version it pinned') return } const errors = `${out}${err}`.trim() console.error(`\nA freshly scaffolded app does not typecheck against stacks@${pinned}:\n`) console.error(errors) /* * Say what actually went wrong, at the bottom, where the reader is. * * These two hints used to print unconditionally, and the note about `buddy * new` exiting non-zero was a dozen lines further up. So a run whose real * problem was "a system package failed to install, and we typechecked a * half-built app anyway" presented as a version-skew problem and sent me to * check the registry - where nothing was wrong (stacksjs/stacks#2414). * * The version hint is worth having when it applies. It applies when the * errors are about resolution: a module or preload that cannot be found is * what an app pinned to a version older than this checkout looks like. It * does not apply to a type mismatch inside a file the template wrote, which * is a real scaffold bug and should not be explained away. */ const looksLikeResolution = /cannot find module|preload not found|cannot find name|could not resolve|TS2307|TS2304/i.test(errors) /* * Lead with the publish window when that is what this is. * * A release lands the version bump as a commit and publishes minutes later. * Every run in between fails here, and the output it produces - a screen of * "failed to resolve" lines ending in `preload not found * "@stacksjs/env/plugin.js"` - points at a package that is published and * whose exports are fine. That last error is a symptom of the aborted * install, and reading it as the cause costs an hour (stacksjs/stacks#2056). * * The check stays red: `buddy new` really is broken until the publish * finishes, and this file argues above that relaxing it would be wrong. But * red should say which of the two failures it is. */ if (publishWindow) { console.error( `\nstacks@${vendored} is not on npm yet, so this app was typechecked against the types built from this commit, ` + `and the errors above are real disagreements between the template and the framework it ships with.\n`, ) } if (scaffoldCode !== 0) { console.error( `\n\`buddy new\` exited ${scaffoldCode} before it finished, so this app may be incomplete — ` + `the errors above can be a consequence of that rather than of the template. ` + `Check its output at the top of this log first.\n`, ) } else if (looksLikeResolution) { console.error( `\nEach error names a file the template wrote. If this checkout's framework (${vendored}) is ahead ` + `of the published one, publishing is the fix — until then \`buddy new\` produces this app.\n`, ) } else { console.error( `\nEach error names a file the template wrote, and none of them is a resolution failure — ` + `so this is the template disagreeing with the framework it pinned, not a publishing lag.\n`, ) } process.exit(1) } if (import.meta.main) await main()