Unverified Commit 4e8fac93 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

cache typechecking with incremental compilation (#24559)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 42c4743f
......@@ -124,7 +124,7 @@
"tailwindcss": "1.1.3",
"taskr": "1.1.0",
"tree-kill": "1.2.2",
"typescript": "3.8.3",
"typescript": "4.3.0-beta",
"wait-port": "0.2.2",
"web-streams-polyfill": "2.1.1",
"webpack-bundle-analyzer": "4.3.0",
......
......@@ -146,8 +146,8 @@ export default async function build(
const { headers, rewrites, redirects } = customRoutes
const cacheDir = path.join(distDir, 'cache')
if (ciEnvironment.isCI && !ciEnvironment.hasNextSupport) {
const cacheDir = path.join(distDir, 'cache')
const hasCache = await fileExists(cacheDir)
if (!hasCache) {
......@@ -193,7 +193,7 @@ export default async function build(
const verifyResult = await nextBuildSpan
.traceChild('verify-typescript-setup')
.traceAsyncFn(() =>
verifyTypeScriptSetup(dir, pagesDir, !ignoreTypeScriptErrors)
verifyTypeScriptSetup(dir, pagesDir, !ignoreTypeScriptErrors, cacheDir)
)
const typeCheckEnd = process.hrtime(typeCheckStart)
......
import path from 'path'
import {
DiagnosticCategory,
getFormattedDiagnostic,
......@@ -18,7 +19,8 @@ export interface TypeCheckResult {
export async function runTypeCheck(
ts: typeof import('typescript'),
baseDir: string,
tsConfigPath: string
tsConfigPath: string,
cacheDir?: string
): Promise<TypeCheckResult> {
const effectiveConfiguration = await getTypeScriptConfiguration(
ts,
......@@ -35,11 +37,28 @@ export async function runTypeCheck(
}
const requiredConfig = getRequiredConfiguration(ts)
const program = ts.createProgram(effectiveConfiguration.fileNames, {
const options = {
...effectiveConfiguration.options,
...requiredConfig,
noEmit: true,
})
}
let program: import('typescript').Program
let incremental = false
if (options.incremental && cacheDir) {
incremental = true
const builderProgram = ts.createIncrementalProgram({
rootNames: effectiveConfiguration.fileNames,
options: {
...options,
incremental: true,
tsBuildInfoFile: path.join(cacheDir, '.tsbuildinfo'),
},
})
program = builderProgram.getProgram()
} else {
program = ts.createProgram(effectiveConfiguration.fileNames, options)
}
const result = program.emit()
// Intended to match:
......@@ -79,6 +98,6 @@ export async function runTypeCheck(
warnings,
inputFilesCount: effectiveConfiguration.fileNames.length,
totalFilesCount: program.getSourceFiles().length,
incremental: false,
incremental,
}
}
import { promises as fs } from 'fs'
import chalk from 'chalk'
import * as CommentJson from 'next/dist/compiled/comment-json'
import semver from 'next/dist/compiled/semver'
import os from 'os'
import { getTypeScriptConfiguration } from './getTypeScriptConfiguration'
......@@ -28,6 +29,9 @@ function getDesiredCompilerOptions(
strict: { suggested: false },
forceConsistentCasingInFileNames: { suggested: true },
noEmit: { suggested: true },
...(semver.gte(ts.version, '4.3.0-beta')
? { incremental: { suggested: true } }
: undefined),
// These values are required and cannot be changed by the user
// Keep this in sync with the webpack config
......
......@@ -15,7 +15,8 @@ import { writeConfigurationDefaults } from './typescript/writeConfigurationDefau
export async function verifyTypeScriptSetup(
dir: string,
pagesDir: string,
typeCheckPreflight: boolean
typeCheckPreflight: boolean,
cacheDir?: string
): Promise<{ result?: TypeCheckResult; version: string | null }> {
const tsConfigPath = path.join(dir, 'tsconfig.json')
......@@ -48,7 +49,7 @@ export async function verifyTypeScriptSetup(
const { runTypeCheck } = require('./typescript/runTypeCheck')
// Verify the project passes type-checking before we go to webpack phase:
result = await runTypeCheck(ts, dir, tsConfigPath)
result = await runTypeCheck(ts, dir, tsConfigPath, cacheDir)
}
return { result, version: ts.version }
} catch (err) {
......
......@@ -11,7 +11,7 @@ export const ShadowPortal: React.FC<ShadowPortalProps> = function Portal({
let mountNode = React.useRef<HTMLDivElement | null>(null)
let portalNode = React.useRef<HTMLElement | null>(null)
let shadowNode = React.useRef<ShadowRoot | null>(null)
let [, forceUpdate] = React.useState()
let [, forceUpdate] = React.useState<{} | undefined>()
React.useLayoutEffect(() => {
const ownerDocument = mountNode.current!.ownerDocument!
......
......@@ -7,6 +7,7 @@
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
......
......@@ -10,6 +10,7 @@
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -7,6 +7,7 @@
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -39,6 +39,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"esModuleInterop\\": true,
\\"module\\": \\"esnext\\",
\\"moduleResolution\\": \\"node\\",
......@@ -83,6 +84,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"esModuleInterop\\": true,
\\"module\\": \\"esnext\\",
\\"moduleResolution\\": \\"node\\",
......@@ -150,6 +152,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true,
\\"isolatedModules\\": true,
......@@ -198,6 +201,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true,
\\"isolatedModules\\": true,
......@@ -243,6 +247,7 @@ describe('tsconfig.json verifier', () => {
\\"strict\\": false,
\\"forceConsistentCasingInFileNames\\": true,
\\"noEmit\\": true,
\\"incremental\\": true,
\\"moduleResolution\\": \\"node\\",
\\"resolveJsonModule\\": true,
\\"isolatedModules\\": true,
......@@ -281,6 +286,7 @@ describe('tsconfig.json verifier', () => {
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
......
......@@ -11,6 +11,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -11,6 +11,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -7,44 +7,65 @@ jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..')
const nextConfigFile = new File(join(appDir, 'next.config.js'))
const tsConfigFile = new File(join(appDir, 'tsconfig.json'))
describe('TypeScript with error handling options', () => {
// Dev can no longer show errors (for now), logbox will cover this in the
// future.
for (const ignoreBuildErrors of [false, true]) {
describe(`ignoreBuildErrors: ${ignoreBuildErrors}`, () => {
beforeAll(() => {
const nextConfig = {
typescript: { ignoreBuildErrors },
}
nextConfigFile.write('module.exports = ' + JSON.stringify(nextConfig))
})
afterAll(() => {
nextConfigFile.restore()
})
for (const incremental of [false, true]) {
for (const ignoreBuildErrors of [false, true]) {
describe(`ignoreBuildErrors: ${ignoreBuildErrors}`, () => {
beforeAll(() => {
const nextConfig = {
typescript: { ignoreBuildErrors },
}
nextConfigFile.write('module.exports = ' + JSON.stringify(nextConfig))
const tsconfig = JSON.parse(tsConfigFile.originalContent)
tsConfigFile.write(
JSON.stringify(
{
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
incremental,
},
},
null,
2
)
)
})
afterAll(() => {
nextConfigFile.restore()
tsConfigFile.restore()
})
it(
ignoreBuildErrors
? 'Next builds the application despite type errors'
: 'Next fails to build the application despite type errors',
async () => {
const { stdout, stderr } = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
})
it(
(ignoreBuildErrors
? 'Next builds the application despite type errors'
: 'Next fails to build the application despite type errors') +
(incremental
? ' in incremental mode'
: ' without incremental mode'),
async () => {
const { stdout, stderr } = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
})
if (ignoreBuildErrors) {
expect(stdout).toContain('Compiled successfully')
expect(stderr).not.toContain('Failed to compile.')
expect(stderr).not.toContain("not assignable to type 'boolean'")
} else {
expect(stdout).not.toContain('Compiled successfully')
expect(stderr).toContain('Failed to compile.')
expect(stderr).toContain('./pages/index.tsx:2:31')
expect(stderr).toContain("not assignable to type 'boolean'")
if (ignoreBuildErrors) {
expect(stdout).toContain('Compiled successfully')
expect(stderr).not.toContain('Failed to compile.')
expect(stderr).not.toContain("not assignable to type 'boolean'")
} else {
expect(stdout).not.toContain('Compiled successfully')
expect(stderr).toContain('Failed to compile.')
expect(stderr).toContain('./pages/index.tsx:2:31')
expect(stderr).toContain("not assignable to type 'boolean'")
}
}
}
)
})
)
})
}
}
})
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -23,6 +23,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -23,6 +23,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -10,6 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true
......
......@@ -16122,6 +16122,11 @@ typescript@3.8.3:
version "3.8.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.8.3.tgz#409eb8544ea0335711205869ec458ab109ee1061"
typescript@4.3.0-beta:
version "4.3.0-beta"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.3.0-beta.tgz#8098e7be29032f09827e94b8e3a6befc9ff70c77"
integrity sha512-bl3wxSVL6gWLQFa466Vm5Vk3z0BNx+QxWhb9wFiYEHm6H8oqFd8Wo3XjgCVxAa5yiSFFKgo/ngBpXdIwqo5o0A==
typescript@^4.1.3:
version "4.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7"
......
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