diff --git a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts index 76ec8a9fabaf2..e5e6cb92da849 100644 --- a/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts +++ b/packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts @@ -18,6 +18,7 @@ const dir = { misnamedJS: `${__dirname}/fixtures/misnamed-js`, misnamedTS: `${__dirname}/fixtures/misnamed-ts`, gatsbyNodeAsDirectory: `${__dirname}/fixtures/gatsby-node-as-directory`, + errorInCode: `${__dirname}/fixtures/error-in-code-ts`, } jest.setTimeout(15000) @@ -175,6 +176,37 @@ describe(`gatsby file compilation`, () => { }) }) }) + + it(`handles errors in TS code`, async () => { + process.chdir(dir.errorInCode) + await remove(`${dir.errorInCode}/.cache`) + await compileGatsbyFiles(dir.errorInCode) + + expect(reporterPanicMock).toMatchInlineSnapshot(` + [MockFunction] { + "calls": Array [ + Array [ + Object { + "context": Object { + "filePath": "/gatsby-node.ts", + "generalMessage": "Expected ';', '}' or ", + "hints": null, + "origin": "@parcel/transformer-js", + "specificMessage": "This is the expression part of an expression statement", + }, + "id": "11901", + }, + ], + ], + "results": Array [ + Object { + "type": "return", + "value": undefined, + }, + ], + } + `) + }) }) describe(`gatsby-node directory is allowed`, () => { diff --git a/packages/gatsby/src/utils/parcel/__tests__/fixtures/error-in-code-ts/gatsby-node.ts b/packages/gatsby/src/utils/parcel/__tests__/fixtures/error-in-code-ts/gatsby-node.ts new file mode 100644 index 0000000000000..a72e769a773d8 --- /dev/null +++ b/packages/gatsby/src/utils/parcel/__tests__/fixtures/error-in-code-ts/gatsby-node.ts @@ -0,0 +1,47 @@ +import { GatsbyNode } from "gatsby" +import { working } from "../utils/say-what-ts" +import { createPages } from "../utils/create-pages-ts" + +this is wrong syntax that should't compile + +export const onPreInit: GatsbyNode["onPreInit"] = ({ reporter }) => { + reporter.info(working) +} + +type Character = { + id: string + name: string +} + +export const sourceNodes: GatsbyNode["sourceNodes"] = async ({ actions, createNodeId, createContentDigest }) => { + const { createNode } = actions + + let characters: Array = [ + { + id: `0`, + name: `A` + }, + { + id: `1`, + name: `B` + } + ] + + characters.forEach((character: Character) => { + const node = { + ...character, + id: createNodeId(`characters-${character.id}`), + parent: null, + children: [], + internal: { + type: 'Character', + content: JSON.stringify(character), + contentDigest: createContentDigest(character), + }, + } + + createNode(node) + }) +} + +export { createPages } diff --git a/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts b/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts index eed6bb9106ea9..2136d1c85b13d 100644 --- a/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts +++ b/packages/gatsby/src/utils/parcel/compile-gatsby-files.ts @@ -152,15 +152,18 @@ export async function compileGatsbyFiles( // entire .cache for users, which is not ideal either especially when we can just delete parcel's cache // and to recover automatically bundles = await worker.single.runParcel(siteRoot) - } catch (e) { - if (retry >= RETRY_COUNT) { + } catch (error) { + if (error.diagnostics) { + handleErrors(error.diagnostics) + return + } else if (retry >= RETRY_COUNT) { reporter.panic({ id: `11904`, - error: e, + error, context: { siteRoot, retries: RETRY_COUNT, - sourceMessage: e.message, + sourceMessage: error.message, }, }) } else {