From 25643a0bebd62c64c0d77505d211c3cf5cf86455 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 7 Oct 2022 15:48:17 -0400 Subject: [PATCH] Prevent server hang in MDX when code throws --- packages/astro/src/runtime/server/jsx.ts | 1 + .../fixtures/mdx-throw-error/astro.config.mjs | 5 ++++ .../fixtures/mdx-throw-error/package.json | 7 ++++++ .../src/components/component.mdx | 5 ++++ .../mdx-throw-error/src/js/throwerror.ts | 5 ++++ .../mdx-throw-error/src/pages/index.astro | 16 ++++++++++++ .../mdx/test/mdx-throw-errors.test.js | 25 +++++++++++++++++++ pnpm-lock.yaml | 8 ++++++ 8 files changed, 72 insertions(+) create mode 100644 packages/integrations/mdx/test/fixtures/mdx-throw-error/astro.config.mjs create mode 100644 packages/integrations/mdx/test/fixtures/mdx-throw-error/package.json create mode 100644 packages/integrations/mdx/test/fixtures/mdx-throw-error/src/components/component.mdx create mode 100644 packages/integrations/mdx/test/fixtures/mdx-throw-error/src/js/throwerror.ts create mode 100644 packages/integrations/mdx/test/fixtures/mdx-throw-error/src/pages/index.astro create mode 100644 packages/integrations/mdx/test/mdx-throw-errors.test.js diff --git a/packages/astro/src/runtime/server/jsx.ts b/packages/astro/src/runtime/server/jsx.ts index 05efe2b31..19c6b898e 100644 --- a/packages/astro/src/runtime/server/jsx.ts +++ b/packages/astro/src/runtime/server/jsx.ts @@ -85,6 +85,7 @@ Did you forget to import the component or is it possible there is a typo?`); } } catch (e) { skipAstroJSXCheck.add(vnode.type); + throw e; } finally { finishUsingConsoleFilter(); } diff --git a/packages/integrations/mdx/test/fixtures/mdx-throw-error/astro.config.mjs b/packages/integrations/mdx/test/fixtures/mdx-throw-error/astro.config.mjs new file mode 100644 index 000000000..f1d5e8bd7 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-throw-error/astro.config.mjs @@ -0,0 +1,5 @@ +import mdx from '@astrojs/mdx'; + +export default { + integrations: [mdx()] +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-throw-error/package.json b/packages/integrations/mdx/test/fixtures/mdx-throw-error/package.json new file mode 100644 index 000000000..f9a61e0f4 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-throw-error/package.json @@ -0,0 +1,7 @@ +{ + "name": "@test/mdx-throw-error", + "dependencies": { + "@astrojs/mdx": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/components/component.mdx b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/components/component.mdx new file mode 100644 index 000000000..9990660c0 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/components/component.mdx @@ -0,0 +1,5 @@ +import { throwError } from '../js/throwerror'; + +This will probably result in a stack overflow. :( + +
{ throwError() }
diff --git a/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/js/throwerror.ts b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/js/throwerror.ts new file mode 100644 index 000000000..9e1ab9b9c --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/js/throwerror.ts @@ -0,0 +1,5 @@ +export function throwError() { + console.log(`I'm going to throw an error. The server will just keep spinning until it runs out of memory...`); + + throw new Error('Oh no'); +} diff --git a/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/pages/index.astro b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/pages/index.astro new file mode 100644 index 000000000..da6f37972 --- /dev/null +++ b/packages/integrations/mdx/test/fixtures/mdx-throw-error/src/pages/index.astro @@ -0,0 +1,16 @@ +--- + +import Component from '../components/component.mdx'; +--- + + + Testing + + +
+

Astro MDX Errors

+ + +
+ + diff --git a/packages/integrations/mdx/test/mdx-throw-errors.test.js b/packages/integrations/mdx/test/mdx-throw-errors.test.js new file mode 100644 index 000000000..fe50425e0 --- /dev/null +++ b/packages/integrations/mdx/test/mdx-throw-errors.test.js @@ -0,0 +1,25 @@ +import mdx from '@astrojs/mdx'; + +import { expect } from 'chai'; +import { loadFixture } from '../../../astro/test/test-utils.js'; + +describe('MDX errors', () => { + /** @type {import('../../../astro/test/test-utils.js').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/mdx-throw-error/', import.meta.url), + integrations: [mdx()], + }); + }); + + it('throws error during the build (does not lock up)', async () => { + try { + await fixture.build(); + expect(false).to.equal(true); + } catch(err) { + expect(err.message).to.equal('Oh no'); + } + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1c0d473b8..55380cebb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2821,6 +2821,14 @@ importers: react: 18.2.0 react-dom: 18.2.0_react@18.2.0 + packages/integrations/mdx/test/fixtures/mdx-throw-error: + specifiers: + '@astrojs/mdx': workspace:* + astro: workspace:* + dependencies: + '@astrojs/mdx': link:../../.. + astro: link:../../../../../astro + packages/integrations/mdx/test/fixtures/mdx-vite-env-vars: specifiers: '@astrojs/mdx': workspace:*