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
kodiakhq
merged 4 commits into
vercel:canary
from
chrislloyd:fix-type-declarations-write
Aug 18, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9465640
Avoid a fs write for read-only filesystems
chrislloyd d3ebf94
Merge branch 'canary' into fix-type-declarations-write
chrislloyd 206bb7a
Check for non-existant files and tests
chrislloyd c593ef0
Merge branch 'canary' into fix-type-declarations-write
chrislloyd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.