Unverified Commit 3a78ccd4 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Add type checking events (#24595)

Will send events like this:

```
[telemetry] {
  "eventName": "NEXT_TYPE_CHECK_COMPLETED",
  "payload": {
    "durationInSeconds": 2,
    "typescriptVersion": "3.8.3",
    "inputFilesCount": 16,
    "totalFilesCount": 289,
    "incremental": false
  }
}
```

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.

## Documentation / Examples

- [ ] Make sure the linting passes
parent 3e6ce907
......@@ -66,6 +66,7 @@ import {
eventBuildOptimize,
eventCliSession,
eventNextPlugins,
eventTypeCheckCompleted,
} from '../telemetry/events'
import { Telemetry } from '../telemetry/storage'
import { CompilerResult, runCompiler } from './compiler'
......@@ -158,10 +159,6 @@ export default async function build(
}
}
const typeCheckingSpinner = createSpinner({
prefixText: `${Log.prefixes.info} Checking validity of types`,
})
const telemetry = new Telemetry({ distDir })
setGlobal('telemetry', telemetry)
......@@ -184,12 +181,29 @@ export default async function build(
)
const ignoreTypeScriptErrors = Boolean(config.typescript?.ignoreBuildErrors)
await nextBuildSpan
const typeCheckStart = process.hrtime()
const typeCheckingSpinner = createSpinner({
prefixText: `${Log.prefixes.info} Checking validity of types`,
})
const verifyResult = await nextBuildSpan
.traceChild('verify-typescript-setup')
.traceAsyncFn(() =>
verifyTypeScriptSetup(dir, pagesDir, !ignoreTypeScriptErrors)
)
const typeCheckEnd = process.hrtime(typeCheckStart)
telemetry.record(
eventTypeCheckCompleted({
durationInSeconds: typeCheckEnd[0],
typescriptVersion: verifyResult.version,
inputFilesCount: verifyResult.result?.inputFilesCount,
totalFilesCount: verifyResult.result?.totalFilesCount,
incremental: verifyResult.result?.incremental,
})
)
if (typeCheckingSpinner) {
typeCheckingSpinner.stopAndPersist()
}
......
......@@ -50,7 +50,7 @@ interface ExportPageInput {
subFolders?: boolean
serverless: boolean
optimizeFonts: boolean
optimizeImages: boolean
optimizeImages?: boolean
optimizeCss: any
parentSpanId: any
}
......
......@@ -10,6 +10,9 @@ import { CompileError } from '../compile-error'
export interface TypeCheckResult {
hasWarnings: boolean
warnings?: string[]
inputFilesCount: number
totalFilesCount: number
incremental: boolean
}
export async function runTypeCheck(
......@@ -23,7 +26,12 @@ export async function runTypeCheck(
)
if (effectiveConfiguration.fileNames.length < 1) {
return { hasWarnings: false }
return {
hasWarnings: false,
inputFilesCount: 0,
totalFilesCount: 0,
incremental: false,
}
}
const requiredConfig = getRequiredConfiguration(ts)
......@@ -66,5 +74,11 @@ export async function runTypeCheck(
.filter((d) => d.category === DiagnosticCategory.Warning)
.map((d) => getFormattedDiagnostic(ts, baseDir, d))
)
return { hasWarnings: true, warnings }
return {
hasWarnings: true,
warnings,
inputFilesCount: effectiveConfiguration.fileNames.length,
totalFilesCount: program.getSourceFiles().length,
incremental: false,
}
}
......@@ -16,14 +16,14 @@ export async function verifyTypeScriptSetup(
dir: string,
pagesDir: string,
typeCheckPreflight: boolean
): Promise<TypeCheckResult | boolean> {
): Promise<{ result?: TypeCheckResult; version: string | null }> {
const tsConfigPath = path.join(dir, 'tsconfig.json')
try {
// Check if the project uses TypeScript:
const intent = await getTypeScriptIntent(dir, pagesDir)
if (!intent) {
return false
return { version: null }
}
const firstTimeSetup = intent.firstTimeSetup
......@@ -43,13 +43,14 @@ export async function verifyTypeScriptSetup(
// Next.js' types:
await writeAppTypeDeclarations(dir)
let result
if (typeCheckPreflight) {
const { runTypeCheck } = require('./typescript/runTypeCheck')
// Verify the project passes type-checking before we go to webpack phase:
return await runTypeCheck(ts, dir, tsConfigPath)
result = await runTypeCheck(ts, dir, tsConfigPath)
}
return true
return { result, version: ts.version }
} catch (err) {
// These are special errors that should not show a stack trace:
if (err instanceof CompileError) {
......
......@@ -2,7 +2,25 @@ const REGEXP_DIRECTORY_DUNDER = /[\\/]__[^\\/]+(?<![\\/]__(?:tests|mocks))__[\\/
const REGEXP_DIRECTORY_TESTS = /[\\/]__(tests|mocks)__[\\/]/i
const REGEXP_FILE_TEST = /\.(?:spec|test)\.[^.]+$/i
const EVENT_BUILD_DURATION = 'NEXT_BUILD_COMPLETED'
const EVENT_TYPE_CHECK_COMPLETED = 'NEXT_TYPE_CHECK_COMPLETED'
type EventTypeCheckCompleted = {
durationInSeconds: number
typescriptVersion: string | null
inputFilesCount?: number
totalFilesCount?: number
incremental?: boolean
}
export function eventTypeCheckCompleted(
event: EventTypeCheckCompleted
): { eventName: string; payload: EventTypeCheckCompleted } {
return {
eventName: EVENT_TYPE_CHECK_COMPLETED,
payload: event,
}
}
const EVENT_BUILD_COMPLETED = 'NEXT_BUILD_COMPLETED'
type EventBuildCompleted = {
durationInSeconds: number
totalPageCount: number
......@@ -18,7 +36,7 @@ export function eventBuildCompleted(
>
): { eventName: string; payload: EventBuildCompleted } {
return {
eventName: EVENT_BUILD_DURATION,
eventName: EVENT_BUILD_COMPLETED,
payload: {
...event,
totalPageCount: pagePaths.length,
......@@ -33,7 +51,7 @@ export function eventBuildCompleted(
}
}
const EVENT_BUILD_OPTIMIZE = 'NEXT_BUILD_OPTIMIZED'
const EVENT_BUILD_OPTIMIZED = 'NEXT_BUILD_OPTIMIZED'
type EventBuildOptimized = {
durationInSeconds: number
totalPageCount: number
......@@ -61,7 +79,7 @@ export function eventBuildOptimize(
>
): { eventName: string; payload: EventBuildOptimized } {
return {
eventName: EVENT_BUILD_OPTIMIZE,
eventName: EVENT_BUILD_OPTIMIZED,
payload: {
...event,
totalPageCount: pagePaths.length,
......
......@@ -71,7 +71,7 @@ export class Span {
}
}
async traceAsyncFn(fn: any) {
async traceAsyncFn<T>(fn: () => T | Promise<T>): Promise<T> {
try {
return await fn()
} finally {
......
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