Skip to content

Commit

Permalink
fix(gatsby): don't serve codeframes for files outside of compilation (#…
Browse files Browse the repository at this point in the history
…38059) (#38063)

* test: add test case for overlay handlers

* fix: don't serve codeframes for files outside of compilation

(cherry picked from commit ed5855e)

Co-authored-by: Michal Piechowiak <[email protected]>
  • Loading branch information
gatsbybot and pieh authored May 5, 2023
1 parent 8889bfe commit fc22f4b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/development-runtime/SHOULD_NOT_SERVE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
this file shouldn't be allowed to be served
this file shouldn't be allowed to be served. CYPRESS-MARKER
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const cwd = Cypress.config(`projectRoot`)

describe(`overlay handlers don't serve unrelated files`, () => {
it(`__file-code-frame`, () => {
cy.request(
`__file-code-frame?filePath=${cwd}/SHOULD_NOT_SERVE&lineNumber=0`
).should(response => {
expect(response.body.codeFrame).not.to.match(/CYPRESS-MARKER/)
})
})
})
6 changes: 6 additions & 0 deletions packages/gatsby/src/commands/build-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { PackageJson } from "../.."
import { IPageDataWithQueryResult } from "../utils/page-data"

import type { GatsbyWorkerPool } from "../utils/worker/pool"
import { setFilesFromDevelopHtmlCompilation } from "../utils/webpack/utils/is-file-inside-compilations"

type IActivity = any // TODO

const isPreview = process.env.GATSBY_IS_PREVIEW === `true`
Expand Down Expand Up @@ -211,6 +213,10 @@ const doBuildRenderer = async (
)
}

if (stage === `develop-html`) {
setFilesFromDevelopHtmlCompilation(stats.compilation)
}

// render-page.js is hard coded in webpack.config
return {
rendererPath: `${directory}/${ROUTES_DIRECTORY}render-page.js`,
Expand Down
22 changes: 20 additions & 2 deletions packages/gatsby/src/utils/start-server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import webpackHotMiddleware from "@gatsbyjs/webpack-hot-middleware"
import webpackDevMiddleware from "webpack-dev-middleware"
import got, { Method } from "got"
import webpack from "webpack"
import webpack, { Compilation } from "webpack"
import express from "express"
import compression from "compression"
import { graphqlHTTP, OptionsData } from "express-graphql"
Expand Down Expand Up @@ -55,6 +55,7 @@ import { getPageMode } from "./page-mode"
import { configureTrailingSlash } from "./express-middlewares"
import type { Express } from "express"
import { addImageRoutes } from "gatsby-plugin-utils/polyfill-remote-file"
import { isFileInsideCompilations } from "./webpack/utils/is-file-inside-compilations"

type ActivityTracker = any // TODO: Replace this with proper type once reporter is typed

Expand Down Expand Up @@ -502,7 +503,24 @@ export async function startServer(
return
}

const sourceContent = await fs.readFile(filePath, `utf-8`)
const absolutePath = path.resolve(
store.getState().program.directory,
filePath
)

const compilation: Compilation =
res.locals?.webpack?.devMiddleware?.stats?.compilation
if (!compilation) {
res.json(emptyResponse)
return
}

if (!isFileInsideCompilations(absolutePath, compilation)) {
res.json(emptyResponse)
return
}

const sourceContent = await fs.readFile(absolutePath, `utf-8`)

const codeFrame = codeFrameColumns(
sourceContent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Compilation, NormalModule } from "webpack"

const filesInsideDevelopHtmlCompilation = new Set<string>()

function removeQueryParams(path: string): string {
return path.split(`?`)[0]
}

export function setFilesFromDevelopHtmlCompilation(
developHtmlCompilation: Compilation
): void {
filesInsideDevelopHtmlCompilation.clear()

for (const module of developHtmlCompilation.modules) {
if (module instanceof NormalModule && module.resource) {
filesInsideDevelopHtmlCompilation.add(removeQueryParams(module.resource))
}
}
}

/**
* Checks if a file is inside either `develop` or `develop-html` compilation. Used to determine if
* we should generate codeframe for this file for error overlay.
*/
export function isFileInsideCompilations(
absolutePath: string,
developBrowserCompilation: Compilation
): boolean {
if (filesInsideDevelopHtmlCompilation.has(absolutePath)) {
return true
}

for (const module of developBrowserCompilation.modules) {
if (module instanceof NormalModule && module.resource) {
if (absolutePath === removeQueryParams(module.resource)) {
return true
}
}
}

return false
}

0 comments on commit fc22f4b

Please sign in to comment.