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

Ensure we detect config correctly with turbo flag #42201

Merged
merged 2 commits into from Oct 31, 2022
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
1 change: 1 addition & 0 deletions packages/next/build/index.ts
Expand Up @@ -340,6 +340,7 @@ export default async function build(
(!!appDir && path.relative(dir, appDir).startsWith('src')),
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
})
)

Expand Down
40 changes: 34 additions & 6 deletions packages/next/cli/next-dev.ts
Expand Up @@ -12,6 +12,8 @@ import { CONFIG_FILES } from '../shared/lib/constants'
import path from 'path'
import type { NextConfig } from '../types'
import { interopDefault } from '../lib/interop-default'
import { Telemetry } from '../telemetry/storage'
import { NextConfigComplete } from '../server/config-shared'

const nextDev: cliCommand = (argv) => {
const validArgs: arg.Spec = {
Expand Down Expand Up @@ -121,6 +123,10 @@ const nextDev: cliCommand = (argv) => {
require('../shared/lib/constants') as typeof import('../shared/lib/constants')
const chalk =
require('next/dist/compiled/chalk') as typeof import('next/dist/compiled/chalk')
const { eventCliSession } =
require('../telemetry/events/version') as typeof import('../telemetry/events/version')
const { findPagesDir } =
require('../lib/find-pages-dir') as typeof import('../lib/find-pages-dir')

// To regenerate the TURBOPACK gradient require('gradient-string')('blue', 'red')('>>> TURBOPACK')
const isTTY = process.stdout.isTTY
Expand Down Expand Up @@ -153,7 +159,8 @@ const nextDev: cliCommand = (argv) => {
configKey === 'appDir' ||
configKey === 'transpilePackages' ||
configKey === 'reactStrictMode' ||
configKey === 'swcMinify'
configKey === 'swcMinify' ||
configKey === 'configFileName'
) {
return false
}
Expand All @@ -163,7 +170,7 @@ const nextDev: cliCommand = (argv) => {
if (typeof defaultValue !== 'object') {
return defaultValue !== userValue
}
return Object.keys(userValue).some((key: string) => {
return Object.keys(userValue || {}).some((key: string) => {
return checkUnsupportedCustomConfig(key, userValue, defaultValue)
})
}
Expand Down Expand Up @@ -277,10 +284,29 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
console.log(feedbackMessage)

return loadBindings()
.then((bindings: any) => {
// eslint-disable-next-line no-shadow
const findUp =
require('next/dist/compiled/find-up') as typeof import('next/dist/compiled/find-up')
.then(async (bindings: any) => {
const distDir = path.join(dir, rawNextConfig.distDir || '.next')
const { pagesDir, appDir } = findPagesDir(
dir,
!!rawNextConfig.experimental?.appDir
)
const telemetry = new Telemetry({
distDir,
})

telemetry.record(
eventCliSession(distDir, rawNextConfig as NextConfigComplete, {
webpackVersion: 5,
cliCommand: 'dev',
isSrcDir:
(!!pagesDir &&
path.relative(dir, pagesDir).startsWith('src')) ||
(!!appDir && path.relative(dir, appDir).startsWith('src')),
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: false,
turboFlag: true,
})
)
const turboJson = findUp.sync('turbo.json', { cwd: dir })
// eslint-disable-next-line no-shadow
const packagePath = findUp.sync('package.json', { cwd: dir })
Expand All @@ -300,6 +326,8 @@ If you cannot make the changes above, but still want to try out\nNext.js v13 wit
})
// Start preflight after server is listening and ignore errors:
preflight().catch(() => {})

await telemetry.flush()
return server
})
.then(
Expand Down
1 change: 1 addition & 0 deletions packages/next/export/index.ts
Expand Up @@ -182,6 +182,7 @@ export default async function exportApp(
isSrcDir: null,
hasNowJson: !!(await findUp('now.json', { cwd: dir })),
isCustomServer: null,
turboFlag: false,
})
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/dev/next-dev-server.ts
Expand Up @@ -728,6 +728,7 @@ export default class DevServer extends Server {
(!!this.appDir && relative(this.dir, this.appDir).startsWith('src')),
hasNowJson: !!(await findUp('now.json', { cwd: this.dir })),
isCustomServer: this.isCustomServer,
turboFlag: false,
})
)
// This is required by the tracing subsystem.
Expand Down
2 changes: 2 additions & 0 deletions packages/next/telemetry/events/version.ts
Expand Up @@ -29,6 +29,7 @@ type EventCliSessionStarted = {
trailingSlashEnabled: boolean
reactStrictMode: boolean
webpackVersion: number | null
turboFlag: boolean
}

function hasBabelConfig(dir: string): boolean {
Expand Down Expand Up @@ -111,6 +112,7 @@ export function eventCliSession(
trailingSlashEnabled: !!nextConfig?.trailingSlash,
reactStrictMode: !!nextConfig?.reactStrictMode,
webpackVersion: event.webpackVersion || null,
turboFlag: event.turboFlag || false,
}
return [{ eventName: EVENT_VERSION, payload }]
}
27 changes: 27 additions & 0 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -368,6 +368,32 @@ describe('Telemetry CLI', () => {
expect(stderr).toMatch(/isSrcDir.*?true/)
})

it('detects --turbo correctly for `next dev`', async () => {
let port = await findPort()
let stderr = ''

const handleStderr = (msg) => {
stderr += msg
}
let app = await launchApp(appDir, port, {
onStderr: handleStderr,
env: {
NEXT_TELEMETRY_DEBUG: 1,
},
turbo: true,
})
await waitFor(1000)

if (app) {
await killApp(app)
}
const event1 = /NEXT_CLI_SESSION_STARTED[\s\S]+?{([\s\S]+?)}/
.exec(stderr)
.pop()

expect(event1).toMatch(/"turboFlag": true/)
})

it('detect reportWebVitals correctly for `next build`', async () => {
// Case 1: When _app.js does not exist.
let build = await nextBuild(appDir, [], {
Expand Down Expand Up @@ -484,6 +510,7 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"imageFormats": "image\/avif,image\/webp"/)
expect(event1).toMatch(/"trailingSlashEnabled": false/)
expect(event1).toMatch(/"reactStrictMode": false/)
expect(event1).toMatch(/"turboFlag": false/)

await fs.rename(
path.join(appDir, 'next.config.i18n-images'),
Expand Down