Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid fs write
next-env.d.ts
on read-only filesystems (#28206)
Next.js currently writes the TS type declarations on startup, regardless of the existing content of the file. This is good for ensuring the file content stays consistent. However, if the file content is already correct, this will perform an unnessecary write. When running Next in read-only filesystems (such as the Bazel sandbox) this can cause the build to fail even if the content of the type declaration file is already correct. This fixes this by only writing the contents of the file if the current contents don't match. ## Test Plan Added an integration test for the general behavior of writing `next-env.d.ts`.
- Loading branch information
1 parent
baeb98e
commit 52c2f8b
Showing
5 changed files
with
114 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
test/integration/typescript-app-type-declarations/next-env.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> | ||
/// <reference types="next/image-types/global" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/basic-features/typescript for more information. |
3 changes: 3 additions & 0 deletions
3
test/integration/typescript-app-type-declarations/pages/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Index() { | ||
return <div /> | ||
} |
61 changes: 61 additions & 0 deletions
61
test/integration/typescript-app-type-declarations/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { findPort, launchApp, killApp } from 'next-test-utils' | ||
import { promises as fs } from 'fs' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
|
||
const appDir = join(__dirname, '..') | ||
const appTypeDeclarations = join(appDir, 'next-env.d.ts') | ||
|
||
describe('TypeScript App Type Declarations', () => { | ||
it('should write a new next-env.d.ts if none exist', async () => { | ||
const prevContent = await fs.readFile(appTypeDeclarations, 'utf8') | ||
try { | ||
await fs.unlink(appTypeDeclarations) | ||
const appPort = await findPort() | ||
let app | ||
try { | ||
app = await launchApp(appDir, appPort, {}) | ||
const content = await fs.readFile(appTypeDeclarations, 'utf8') | ||
expect(content).toEqual(prevContent) | ||
} finally { | ||
await killApp(app) | ||
} | ||
} finally { | ||
await fs.writeFile(appTypeDeclarations, prevContent) | ||
} | ||
}) | ||
|
||
it('should overwrite next-env.d.ts if an incorrect one exists', async () => { | ||
const prevContent = await fs.readFile(appTypeDeclarations, 'utf8') | ||
try { | ||
await fs.writeFile(appTypeDeclarations, prevContent + 'modification') | ||
const appPort = await findPort() | ||
let app | ||
try { | ||
app = await launchApp(appDir, appPort, {}) | ||
const content = await fs.readFile(appTypeDeclarations, 'utf8') | ||
expect(content).toEqual(prevContent) | ||
} finally { | ||
await killApp(app) | ||
} | ||
} finally { | ||
await fs.writeFile(appTypeDeclarations, prevContent) | ||
} | ||
}) | ||
|
||
it('should not touch an existing correct next-env.d.ts', async () => { | ||
const prevStat = await fs.stat(appTypeDeclarations) | ||
const appPort = await findPort() | ||
let app | ||
try { | ||
app = await launchApp(appDir, appPort, {}) | ||
const stat = await fs.stat(appTypeDeclarations) | ||
expect(stat.mtime).toEqual(prevStat.mtime) | ||
} finally { | ||
await killApp(app) | ||
} | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
test/integration/typescript-app-type-declarations/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"jsx": "preserve", | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"incremental": true, | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true | ||
}, | ||
"exclude": ["node_modules"], | ||
"include": ["next-env.d.ts", "components", "pages"] | ||
} |