Skip to content

Commit

Permalink
fix: handle actual compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Dec 21, 2023
1 parent be2dfe4 commit 6360181
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
32 changes: 32 additions & 0 deletions packages/gatsby/src/utils/parcel/__tests__/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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": "<PROJECT_ROOT>/gatsby-node.ts",
"generalMessage": "Expected ';', '}' or <eof>",
"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`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Character> = [
{
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 }
11 changes: 7 additions & 4 deletions packages/gatsby/src/utils/parcel/compile-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6360181

Please sign in to comment.