Unverified Commit b35fb85d authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Create cache directory before persisting Chrome DevTools workspace UUID (#100802)

### What?

When the dev server answers the Chrome DevTools workspace request
(`/.well-known/appspecific/com.chrome.devtools.json`) and there's no
persisted UUID yet, it now creates `<distDir>/cache` before writing
`chrome-devtools-workspace-uuid`.

### Why?

The UUID was written with a bare `writeFile`. If the cache directory
didn't exist at the time of the first DevTools request (e.g. it was
removed while `next dev` was running), the write failed with `ENOENT`.
That logged the "needs to be reconnected after the next page reload"
warning and kept the UUID in memory only, so the next server process
generated a new UUID and the DevTools workspace had to be reconnected.

On a normal startup the directory usually already exists because other
startup writers create it, which is why this rarely shows up.

### How?

- The directory is created recursively inside the existing `try/catch`.
If creating the directory or writing the file fails (e.g. an unwritable
path), the endpoint still responds with an in-memory UUID and logs the
existing warning.

**Test:** a dev-only e2e test (`// @force-gate dev`) in
`test/e2e/chrome-devtools-workspace/`, using a dedicated fixture:
- It waits for the asynchronous startup writes into the cache directory
(`.rscinfo`, plus `next-devtools-config.json` with webpack), then
removes the directory.
- It then checks the endpoint returns a UUID, that the same UUID is on
disk, and that no persistence warning is logged. After restarting the
server, the same UUID must be returned.
- The fixture disables the Turbopack filesystem cache so it can't
recreate the directory mid-test.

### Verification

- The new test fails without the fix and passes with it under
`test-dev-turbo` and `test-dev-webpack`.
- The existing `test/e2e/chrome-devtools-workspace/` suites still pass
in dev (Turbopack and webpack) and in `test-start-turbo`.
- Manual check: when the cache path is a regular file, so the directory
can't be created, the endpoint still returns a UUID and logs the warning
instead of crashing.

<!-- NEXT_JS_LLM -->


<!-- fleet f94f85f4-f8af-450a-a1ca-28243552f5f0 -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 94ffce78
......@@ -64,6 +64,8 @@ async function getChromeDevtoolsWorkspace(
workspaceUUID = randomUUID()
try {
// The cache directory might not exist yet.
await fs.promises.mkdir(cacheBaseDir, { recursive: true })
await fs.promises.writeFile(cachedUUIDPath, workspaceUUID, 'utf8')
} catch (cause) {
console.warn(
......
import * as path from 'path'
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('chrome-devtools-workspace default', () => {
const { isNextDev, next } = nextTestSetup({
......@@ -31,6 +32,62 @@ describe('chrome-devtools-workspace default', () => {
})
})
// The endpoint is only served by the dev server.
// @force-gate dev
describe('chrome-devtools-workspace persistence', () => {
const { isTurbopack, next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'persistence'),
})
it('persists the UUID when the cache directory does not exist yet', async () => {
const cacheDir = path.join(next.distDir, 'cache')
const uuidFile = path.join(cacheDir, 'chrome-devtools-workspace-uuid')
// Dev server startup asynchronously writes other files into the cache
// directory (the server actions encryption key, and with webpack the dev
// tools config). Wait for them so they don't recreate the directory after
// we've removed it.
const startupFiles = isTurbopack
? ['.rscinfo']
: ['.rscinfo', 'next-devtools-config.json']
await retry(async () => {
for (const file of startupFiles) {
expect({
file,
exists: await next.hasFile(path.join(cacheDir, file)),
}).toEqual({ file, exists: true })
}
})
await next.remove(cacheDir)
expect(await next.hasFile(cacheDir)).toBe(false)
const devtoolsResponse = await next.fetch(
'/.well-known/appspecific/com.chrome.devtools.json'
)
const json = await devtoolsResponse.json()
expect(json).toEqual({
workspace: {
uuid: expect.stringMatching(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
),
root: next.testDir,
},
})
expect(next.cliOutput).not.toContain(
'Failed to persist Chrome DevTools workspace UUID'
)
expect(await next.readFile(uuidFile)).toBe(json.workspace.uuid)
// A fresh server process must reuse the persisted UUID.
await next.stop()
await next.start()
const restartedResponse = await next.fetch(
'/.well-known/appspecific/com.chrome.devtools.json'
)
expect(await restartedResponse.json()).toEqual(json)
})
})
describe('chrome-devtools-workspace proxy', () => {
const { isNextDev, next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'proxy'),
......
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return 'Hello, Dave!'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
// Keep Turbopack from re-creating the cache directory while the test runs.
turbopackFileSystemCacheForDev: false,
},
}
module.exports = nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment