Skip to content

Harden twoslash config against silent regressions#740

Open
slang25 wants to merge 1 commit intomicrosoft:mainfrom
slang25:slang25/twoslash-types-deploy-safety
Open

Harden twoslash config against silent regressions#740
slang25 wants to merge 1 commit intomicrosoft:mainfrom
slang25:slang25/twoslash-types-deploy-safety

Conversation

@slang25
Copy link
Copy Markdown
Contributor

@slang25 slang25 commented Apr 21, 2026

Two small defensive changes in ec.config.mjs, layered on top of #741.

Changes

  • Throw instead of warn when src/data/twoslash/aspire.d.ts is missing. Since Fix twoslash in AppHostBuilder; source-control twoslash .d.ts bundle #741 source-controls the bundle, a missing file means the working tree is corrupted — not a normal path. Catching that at build time beats silently shipping samples that can't resolve ./.modules/aspire.js.
  • Flip noErrorValidation to false so unannotated TS errors fail the build rather than rendering as squigglies in the shipped HTML. Samples that deliberately illustrate a compiler error can opt in with // @errors: <codes>; otherwise an error means the sample (or the generated SDK shape) is wrong and should be fixed, not shown to readers.

Originally this PR also carried a frontend.esproj publish-time MSBuild target. That fix is no longer needed: #741 source-controls the generated bundle, so dotnet publish no longer depends on a pre-build type-generation step. Dropped it.

Test plan

  • Full pnpm build:skip-search passes with noErrorValidation: false — no existing sample has an unannotated TS error
  • Generated HTML in dist/ has zero Cannot find module / twoslash-error markers
  • Deleting src/data/twoslash/aspire.d.ts and re-importing ec.config.mjs throws; restoring it loads cleanly
  • pnpm test:unit:twoslash-types passes

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings April 21, 2026 17:37
@slang25 slang25 requested a review from IEvangelist as a code owner April 21, 2026 17:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents TypeScript twoslash type-bundle regressions from silently shipping to the live docs site by (1) failing the Astro build when the generated .twoslash-types/aspire.d.ts bundle is missing and (2) ensuring the frontend dist/ (and twoslash types) are produced during dotnet publish of the StaticHost.

Changes:

  • Make ec.config.mjs throw (instead of warn) when .twoslash-types/aspire.d.ts is missing to turn a prod-only failure into a build-time failure.
  • Add a publish-time MSBuild target in frontend.esproj to run pnpm install --frozen-lockfile and pnpm run build before publishing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/frontend/frontend.esproj Adds a publish hook intended to ensure pnpm install + pnpm build run during dotnet publish.
src/frontend/ec.config.mjs Fails fast if the twoslash types bundle is missing and always injects the types into twoslash’s VFS.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/frontend/frontend.esproj Outdated
slang25 added a commit to slang25/aspire.dev that referenced this pull request Apr 21, 2026
Per Copilot review on PR microsoft#740: BeforeTargets="Publish" runs after the
Publish target's dependencies, so file enumeration and copy already saw
a stale dist/ before our pnpm build ran.

Switch to two earlier hooks:
- Inject BuildFrontendForPublish into PublishDependsOn so it's the first
  dep of Publish (idiomatic .NET SDK pattern).
- BeforeTargets="PrepareForPublish;ComputeFilesToPublish" as a backstop
  in case the JS SDK invokes those outside the PublishDependsOn flow.

Both fire before any publish-time file collection, ensuring dist/ is
fresh when the SDK packages the StaticHost.
Comment thread src/frontend/frontend.esproj
Two small defensive changes on top of microsoft#741:

- Throw instead of warn when src/data/twoslash/aspire.d.ts is missing.
  Since microsoft#741 source-controls the bundle, a missing file means the tree
  is corrupted — catching that at build time beats silently shipping
  samples that can't resolve `./.modules/aspire.js`.
- Flip noErrorValidation to false so unannotated TS errors fail the
  build rather than rendering as squigglies in the shipped HTML.
  Samples that deliberately illustrate a compiler error can opt in
  with `// @errors: <codes>`; otherwise an error means the sample
  (or the generated SDK shape) is wrong and should be fixed, not
  shown to readers. Verified with a full build: zero unannotated
  errors across the current docs.
@slang25 slang25 force-pushed the slang25/twoslash-types-deploy-safety branch from b2bacf2 to 5eecbb6 Compare April 21, 2026 19:17
@slang25 slang25 changed the title Fix missing twoslash types silently shipping to production Harden twoslash config against silent regressions Apr 21, 2026
@slang25 slang25 requested a review from Copilot April 21, 2026 19:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +16 to +22
if (!existsSync(ASPIRE_TYPES_PATH)) {
throw new Error(
`[ec] src/data/twoslash/aspire.d.ts not found at ${ASPIRE_TYPES_PATH}. ` +
'Run `pnpm twoslash-types` to regenerate it from src/data/ts-modules.'
);
}
const aspireTypes = readFileSync(ASPIRE_TYPES_PATH, 'utf8');
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

This is a check-then-read pattern (TOCTOU). It’s more robust to read the file directly and throw a tailored error if it fails (e.g., wrap readFileSync in a try/catch). This avoids rare edge cases where the file is deleted/moved between existsSync and readFileSync, and keeps the failure mode consistent.

Copilot uses AI. Check for mistakes.
);
}
const aspireTypes = readFileSync(ASPIRE_TYPES_PATH, 'utf8');

Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

With the new behavior, an empty (or whitespace-only) aspire.d.ts will still pass and will populate .modules/aspire.ts with empty content, which can re-introduce the ‘silent regression’ this PR is trying to prevent (imports resolve, but types effectively disappear). Consider validating aspireTypes.trim().length > 0 and throwing with a clear message if it’s empty.

Suggested change
if (aspireTypes.trim().length === 0) {
throw new Error(
`[ec] src/data/twoslash/aspire.d.ts is empty at ${ASPIRE_TYPES_PATH}. ` +
'Run `pnpm twoslash-types` to regenerate it from src/data/ts-modules.'
);
}

Copilot uses AI. Check for mistakes.
// are declared at `.modules/aspire.ts` so docs samples that import
// `'./.modules/aspire.js'` resolve against the real API surface.
extraFiles: aspireTypes ? { '.modules/aspire.ts': aspireTypes } : {},
extraFiles: { '.modules/aspire.ts': aspireTypes },
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

With the new behavior, an empty (or whitespace-only) aspire.d.ts will still pass and will populate .modules/aspire.ts with empty content, which can re-introduce the ‘silent regression’ this PR is trying to prevent (imports resolve, but types effectively disappear). Consider validating aspireTypes.trim().length > 0 and throwing with a clear message if it’s empty.

Copilot uses AI. Check for mistakes.
// shipping samples that can't resolve `./.modules/aspire.js`.
if (!existsSync(ASPIRE_TYPES_PATH)) {
throw new Error(
`[ec] src/data/twoslash/aspire.d.ts not found at ${ASPIRE_TYPES_PATH}. ` +
Copy link

Copilot AI Apr 21, 2026

Choose a reason for hiding this comment

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

The error message includes an absolute path, which can leak runner/workspace paths into public CI logs. Consider switching to a relative path in the message (e.g., relative to process.cwd()), while still keeping enough context to debug locally.

Copilot uses AI. Check for mistakes.
@IEvangelist
Copy link
Copy Markdown
Member

Hey @slang25 is this still needed?

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