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