Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid fs write next-env.d.ts on read-only filesystems #28206

Merged
merged 4 commits into from Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 23 additions & 14 deletions packages/next/lib/typescript/writeAppTypeDeclarations.ts
@@ -1,6 +1,7 @@
import { promises as fs } from 'fs'
import os from 'os'
import path from 'path'
import { fileExists } from '../file-exists'

export async function writeAppTypeDeclarations(
baseDir: string,
Expand All @@ -9,19 +10,27 @@ export async function writeAppTypeDeclarations(
// Reference `next` types
const appTypeDeclarations = path.join(baseDir, 'next-env.d.ts')

await fs.writeFile(
appTypeDeclarations,
const content =
'/// <reference types="next" />' +
os.EOL +
'/// <reference types="next/types/global" />' +
os.EOL +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + os.EOL
: '') +
os.EOL +
'// NOTE: This file should not be edited' +
os.EOL +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
os.EOL
)
os.EOL +
'/// <reference types="next/types/global" />' +
os.EOL +
(imageImportsEnabled
? '/// <reference types="next/image-types/global" />' + os.EOL
: '') +
os.EOL +
'// NOTE: This file should not be edited' +
os.EOL +
'// see https://nextjs.org/docs/basic-features/typescript for more information.' +
os.EOL

// Avoids a write for read-only filesystems
if (
(await fileExists(appTypeDeclarations)) &&
(await fs.readFile(appTypeDeclarations, 'utf8')) === content
Comment on lines +29 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(await fileExists(appTypeDeclarations)) &&
(await fs.readFile(appTypeDeclarations, 'utf8')) === content
(await fs.readFile(appTypeDeclarations, 'utf8').catch(() => '') === content

Instead of checking if the file exists and then reading the content we could read the content and handle it failing gracefully which seems to be recommended in the node docs here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. That's a good point and would have fixed it if the merge queue hadn't kicked in. I think practically, that case is unlikely to be an issue here - the window where the user would have to write the file is slim and the consequence is small. Is it worth a PR to fix?

Copy link
Member

@ijjk ijjk Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense, I think it's alright to leave as is.

) {
return
}

await fs.writeFile(appTypeDeclarations, content)
}
@@ -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.
@@ -0,0 +1,3 @@
export default function Index() {
return <div />
}
@@ -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 test/integration/typescript-app-type-declarations/tsconfig.json
@@ -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"]
}