Unverified Commit 655b808c authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Run with `--enable-source-maps` by default in `next dev` (#71820)

We already create server source maps in dev by default,
so we should make use of them.

To opt-out, run `next dev --disable-source-maps` instead.

Our internal `next-no-sourcemaps` script is now defunct.
parent bfdf5278
...@@ -51,8 +51,8 @@ ...@@ -51,8 +51,8 @@
"prepublishOnly": "turbo run build", "prepublishOnly": "turbo run build",
"lint-staged": "lint-staged", "lint-staged": "lint-staged",
"next-with-deps": "./scripts/next-with-deps.sh", "next-with-deps": "./scripts/next-with-deps.sh",
"next": "cross-env NEXT_TELEMETRY_DISABLED=1 node --trace-deprecation --enable-source-maps packages/next/dist/bin/next", "next": "cross-env NEXT_TELEMETRY_DISABLED=1 NODE_OPTIONS=\"--trace-deprecation --enable-source-maps\" next",
"next-no-sourcemaps": "cross-env NEXT_TELEMETRY_DISABLED=1 node --trace-deprecation packages/next/dist/bin/next", "next-no-sourcemaps": "echo 'No longer supported. Use `pnpm next --disable-source-maps` instead'; exit 1;",
"clean-trace-jaeger": "node scripts/rm.mjs test/integration/basic/.next && TRACE_TARGET=JAEGER pnpm next build test/integration/basic", "clean-trace-jaeger": "node scripts/rm.mjs test/integration/basic/.next && TRACE_TARGET=JAEGER pnpm next build test/integration/basic",
"debug": "cross-env NEXT_TELEMETRY_DISABLED=1 node --inspect --trace-deprecation --enable-source-maps packages/next/dist/bin/next", "debug": "cross-env NEXT_TELEMETRY_DISABLED=1 node --inspect --trace-deprecation --enable-source-maps packages/next/dist/bin/next",
"postinstall": "node scripts/git-configure.mjs && node scripts/install-native.mjs", "postinstall": "node scripts/git-configure.mjs && node scripts/install-native.mjs",
......
...@@ -172,6 +172,11 @@ program ...@@ -172,6 +172,11 @@ program
'-H, --hostname <hostname>', '-H, --hostname <hostname>',
'Specify a hostname on which to start the application (default: 0.0.0.0).' 'Specify a hostname on which to start the application (default: 0.0.0.0).'
) )
.option(
'--disable-source-maps',
"Don't start the Dev server with `--enable-source-maps`.",
false
)
.option( .option(
'--experimental-https', '--experimental-https',
'Starts the server with HTTPS and generates a self-signed certificate.' 'Starts the server with HTTPS and generates a self-signed certificate.'
......
...@@ -40,6 +40,7 @@ import { flushAllTraces, trace } from '../trace' ...@@ -40,6 +40,7 @@ import { flushAllTraces, trace } from '../trace'
import { traceId } from '../trace/shared' import { traceId } from '../trace/shared'
export type NextDevOptions = { export type NextDevOptions = {
disableSourceMaps: boolean
turbo?: boolean turbo?: boolean
turbopack?: boolean turbopack?: boolean
port: number port: number
...@@ -267,6 +268,12 @@ const nextDev = async ( ...@@ -267,6 +268,12 @@ const nextDev = async (
delete nodeOptions['max_old_space_size'] delete nodeOptions['max_old_space_size']
} }
if (options.disableSourceMaps) {
delete nodeOptions['enable-source-maps']
} else {
nodeOptions['enable-source-maps'] = true
}
if (nodeDebugType) { if (nodeDebugType) {
const address = getParsedDebugAddress() const address = getParsedDebugAddress()
address.port = address.port + 1 address.port = address.port + 1
......
...@@ -480,6 +480,10 @@ ${ENDGROUP}`) ...@@ -480,6 +480,10 @@ ${ENDGROUP}`)
// unset CI env so CI behavior is only explicitly // unset CI env so CI behavior is only explicitly
// tested when enabled // tested when enabled
CI: '', CI: '',
// But some tests need to fork based on machine? CI? behavior differences
// Only use read this in tests.
// For implementation forks, use `process.env.CI` instead
NEXT_TEST_CI: process.env.CI,
...(options.local ...(options.local
? {} ? {}
......
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
assertNoRedbox, assertNoRedbox,
check, check,
getRedboxSource, getRedboxSource,
retry,
} from 'next-test-utils' } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
...@@ -31,23 +32,22 @@ describe('middleware - development errors', () => { ...@@ -31,23 +32,22 @@ describe('middleware - development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await next.fetch('/') await next.fetch('/')
const output = stripAnsi(next.cliOutput)
await check(() => { await retry(() => {
if (isTurbopack) { expect(stripAnsi(next.cliOutput)).toContain('boom')
expect(stripAnsi(next.cliOutput)).toMatch( })
/middleware.js \(\d+:\d+\) @ __TURBOPACK__default__export__/ // TODO: assert on full, ignore-listed stack
) expect(stripAnsi(next.cliOutput)).toContain(
} else { isTurbopack
expect(stripAnsi(next.cliOutput)).toMatch( ? '\n ⨯ middleware.js (3:15) @ __TURBOPACK__default__export__' +
/middleware.js \(\d+:\d+\) @ default/ '\n ⨯ Error: boom' +
) '\n at __TURBOPACK__default__export__ (./middleware.js:3:15)'
} : '\n ⨯ middleware.js (3:15) @ default' +
'\n ⨯ boom' +
expect(stripAnsi(next.cliOutput)).toMatch(/boom/) '\n 1 |' +
return 'success' '\n 2 | export default function () {' +
}, 'success') "\n> 3 | throw new Error('boom')" +
expect(output).not.toContain( '\n | ^'
'webpack-internal:///(middleware)/./middleware.js'
) )
}) })
...@@ -79,13 +79,20 @@ describe('middleware - development errors', () => { ...@@ -79,13 +79,20 @@ describe('middleware - development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await next.fetch('/') await next.fetch('/')
await check(
() => stripAnsi(next.cliOutput), await retry(() => {
new RegExp(`unhandledRejection: Error: async boom!`, 'm') expect(stripAnsi(next.cliOutput)).toContain(
'unhandledRejection: Error: async boom!'
)
})
// TODO: assert on full, ignore-listed stack
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? 'unhandledRejection: Error: async boom!\n at throwError ('
: 'unhandledRejection: Error: async boom!' +
'\n at throwError (webpack-internal:///(middleware)/./middleware.js:8:11)' +
'\n at __WEBPACK_DEFAULT_EXPORT__ (webpack-internal:///(middleware)/./middleware.js:11:5)'
) )
// expect(output).not.toContain(
// 'webpack-internal:///(middleware)/./middleware.js'
// )
}) })
it('does not render the error', async () => { it('does not render the error', async () => {
...@@ -112,17 +119,37 @@ describe('middleware - development errors', () => { ...@@ -112,17 +119,37 @@ describe('middleware - development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await next.fetch('/') await next.fetch('/')
// const output = stripAnsi(next.cliOutput)
await check(() => { await retry(() => {
expect(stripAnsi(next.cliOutput)).toMatch( expect(stripAnsi(next.cliOutput)).toContain('Dynamic Code Evaluation')
/middleware.js \(\d+:\d+\) @ eval/ })
// TODO: assert on full, ignore-listed stack
if (isTurbopack) {
// Locally, prefixes the "test is not defined".
// In CI, it prefixes "Dynamic Code Evaluation".
expect(stripAnsi(next.cliOutput)).toContain(
'\n ⚠ middleware.js (3:22) @ __TURBOPACK__default__export__' +
'\n ⨯ middleware.js (4:9) @ eval'
) )
expect(stripAnsi(next.cliOutput)).toMatch(/test is not defined/) }
return 'success' expect(stripAnsi(next.cliOutput)).toContain(
}, 'success') isTurbopack
// expect(output).not.toContain( ? '\n ⨯ Error: test is not defined' +
// 'webpack-internal:///(middleware)/./middleware.js' '\n at eval (./middleware.js:4:9)' +
// ) '\n at <unknown> (./middleware.js:4:9'
: '\n ⨯ Error [ReferenceError]: test is not defined' +
'\n at eval (file://webpack-internal:///(middleware)/./middleware.js)' +
'\n at eval (webpack://_N_E/middleware.js?3bcb:4:8)'
)
expect(stripAnsi(next.cliOutput)).toContain(
isTurbopack
? "\n ⚠ Error: Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation' +
'\n at __TURBOPACK__default__export__ (./middleware.js:3:22)'
: '\n ⚠ middleware.js (4:9) @ eval' +
"\n ⚠ Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime" +
'\nLearn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation'
)
}) })
it('renders the error correctly and recovers', async () => { it('renders the error correctly and recovers', async () => {
...@@ -150,22 +177,18 @@ describe('middleware - development errors', () => { ...@@ -150,22 +177,18 @@ describe('middleware - development errors', () => {
it('logs the error correctly', async () => { it('logs the error correctly', async () => {
await next.fetch('/') await next.fetch('/')
const output = stripAnsi(next.cliOutput)
await check(() => { await retry(() => {
if (isTurbopack) { expect(stripAnsi(next.cliOutput)).toContain(`Error: booooom!`)
expect(stripAnsi(next.cliOutput)).toMatch( })
/middleware.js \(\d+:\d+\) @ \[project\]\/middleware\.js \[middleware\] \(ecmascript\)/ // TODO: assert on full, ignore-listed stack
) expect(stripAnsi(next.cliOutput)).toContain(
} else { isTurbopack
expect(stripAnsi(next.cliOutput)).toMatch( ? '\n ⨯ middleware.js (3:13) @ [project]/middleware.js [middleware] (ecmascript)' +
/middleware.js \(\d+:\d+\) @ <unknown>/ '\n ⨯ Error: booooom!' +
) '\n at <unknown> ([project]/middleware.js [middleware] (ecmascript) (./middleware.js:3:13)'
} : '\n ⨯ Error: booooom!' +
expect(stripAnsi(next.cliOutput)).toMatch(/booooom!/) '\n at <unknown> (webpack://_N_E/middleware.js'
return 'success'
}, 'success')
expect(output).not.toContain(
'webpack-internal:///(middleware)/./middleware.js'
) )
}) })
......
node_modules/
pnpm-lock.yaml
\ No newline at end of file
import { run } from 'internal-pkg'
function logError() {
const error = new Error('Boom')
console.error(error)
}
export default function Page() {
run(() => logError())
return null
}
'use client'
import { run } from 'internal-pkg'
function logError() {
const error = new Error('Boom')
console.error(error)
}
export default function Page() {
run(() => logError())
return null
}
'use client'
function logError() {
const error = new Error('Boom')
console.error(error)
}
export default function Page() {
logError()
return null
}
{
"name": "internal-pkg",
"private": true,
"type": "module",
"exports": {
".": {
"default": "./index.js"
}
}
}
{
"dependencies": {
"internal-pkg": "file:./internal-pkg"
}
}
/* eslint-disable jest/no-standalone-expect */
import * as path from 'path' import * as path from 'path'
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
import { retry } from 'next-test-utils'
/**
* TODO: Remove this function once we log the file name on disk by adjusting `moduleFileNameTemplate`.
* in: webpack:///app/ssr-error-log-ignore-listed/page.js?1234:5:16
* out: webpack:///app/ssr-error-log-ignore-listed/page.js?[search]:5:16
*/
function normalizeWebpackLocation(frame: string) {
const webpackQueryStringtMatch = frame.match(/\?\w+:(\d+):(\d+)\)$/)
if (webpackQueryStringtMatch === null) {
return frame
}
return frame.replace(
webpackQueryStringtMatch[0],
`?[search]:${webpackQueryStringtMatch[1]}:${webpackQueryStringtMatch[2]})`
)
}
function normalizeCliOutput(output: string) {
return stripAnsi(output)
.split('\n')
.map((line) => {
return (
normalizeWebpackLocation(line)
.replaceAll(
path.join(__dirname, 'fixtures/default'),
'[fixture-root]'
)
// in: webpack://[fixture-root]/node_modules/.pnpm/internal-pkg@file+internal-pkg/node_modules/internal-pkg/index.js?[search]:2:0
// out: webpack://[fixture-root]/node_modules/[pnpm]/internal-pkg/index.js?[search]:2:0
.replace(/\/.pnpm\/[^/]+\/node_modules/g, '/[pnpm]')
)
})
.join('\n')
}
describe('app-dir - server source maps', () => { describe('app-dir - server source maps', () => {
const { skipped, next, isNextDev } = nextTestSetup({ const dependencies =
// 'link:' is not suitable for this test since this makes internal-pkg
// not appear in node_modules.
{
'internal-pkg': `file:${path.resolve(__dirname, 'fixtures/default/internal-pkg')}`,
}
const { skipped, next, isNextDev, isTurbopack } = nextTestSetup({
dependencies,
files: path.join(__dirname, 'fixtures/default'), files: path.join(__dirname, 'fixtures/default'),
// Deploy tests don't have access to runtime logs. // Deploy tests don't have access to runtime logs.
// Manually verify that the runtime logs match. // Manually verify that the runtime logs match.
...@@ -12,13 +57,153 @@ describe('app-dir - server source maps', () => { ...@@ -12,13 +57,153 @@ describe('app-dir - server source maps', () => {
if (skipped) return if (skipped) return
it('logged errors have a sourcemapped stack with a codeframe', async () => { it('logged errors have a sourcemapped stack with a codeframe', async () => {
// TODO: Write test once we run with `--enable-source-maps` when `experimental.serverSourceMaps` is set await next.render('/rsc-error-log')
if (isNextDev) {
await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain(
isTurbopack
? '\nError: Boom' +
'\n at logError (turbopack://[project]/app/rsc-error-log/page.js:2:16)' +
'\n at Page (turbopack://[project]/app/rsc-error-log/page.js:7:2)' +
'\n 1 | function logError() {' +
"\n> 2 | const error = new Error('Boom')" +
'\n | ^' +
'\n 3 | console.error(error)' +
'\n 4 | }' +
'\n 5 |' +
'\n'
: '\nError: Boom' +
'\n at logError (webpack:///app/rsc-error-log/page.js?[search]:2:16)' +
// FIXME: Method name should be "Page"
'\n at logError (webpack:///app/rsc-error-log/page.js?[search]:7:2)' +
'\n 1 | function logError() {' +
"\n> 2 | const error = new Error('Boom')" +
'\n | ^' +
'\n 3 | console.error(error)' +
'\n 4 | }' +
'\n 5 |' +
'\n'
)
})
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
}) })
it('logged errors have a sourcemapped `cause`', async () => { it('logged errors have a sourcemapped `cause`', async () => {
// TODO: Write test once we run with `--enable-source-maps` when `experimental.serverSourceMaps` is set await next.render('/rsc-error-log-cause')
if (isNextDev) {
await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain(
isTurbopack
? '\nError: Boom' +
'\n at logError (turbopack://[project]/app/rsc-error-log-cause/page.js:2:16)' +
'\n at Page (turbopack://[project]/app/rsc-error-log-cause/page.js:8:2)' +
'\n 1 | function logError(cause) {' +
"\n> 2 | const error = new Error('Boom', { cause })" +
'\n | ^' +
'\n 3 | console.error(error)' +
'\n 4 | }' +
'\n 5 | {' +
'\n [cause]: Error: Boom' +
'\n at Page (turbopack://[project]/app/rsc-error-log-cause/page.js:7:16)' +
'\n 5 |' +
'\n 6 | export default function Page() {' +
"\n > 7 | const error = new Error('Boom')" +
'\n | ^' +
'\n 8 | logError(error)' +
'\n 9 | return null' +
'\n 10 | }' +
'\n'
: '\nError: Boom' +
'\n at logError (webpack:///app/rsc-error-log-cause/page.js?[search]:2:16)' +
// FIXME: Method name should be "Page"
'\n at logError (webpack:///app/rsc-error-log-cause/page.js?[search]:8:2)' +
'\n 1 | function logError(cause) {' +
"\n> 2 | const error = new Error('Boom', { cause })" +
'\n | ^' +
'\n 3 | console.error(error)' +
'\n 4 | }' +
'\n 5 | {' +
'\n [cause]: Error: Boom' +
'\n at Page (webpack:///app/rsc-error-log-cause/page.js?[search]:7:16)' +
'\n 5 |' +
'\n 6 | export default function Page() {' +
"\n > 7 | const error = new Error('Boom')" +
'\n | ^' +
'\n 8 | logError(error)' +
'\n 9 | return null' +
'\n 10 | }' +
'\n'
)
})
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
}) })
// FIXME: Turbopack resolver bug
// FIXME: Turbopack build? bugs taint the whole dev server
;(isTurbopack ? it.skip : it)(
'stack frames are not ignore-listed in ssr',
async () => {
await next.render('/ssr-error-log-ignore-listed')
if (isNextDev) {
await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain(
isTurbopack
? // FIXME: Turbopack resolver bug
"Module not found: Can't resolve 'internal-pkg'"
: '\nError: Boom' +
'\n at logError (webpack:///app/ssr-error-log-ignore-listed/page.js?[search]:5:16)' +
// FIXME: Method name should be "Page"
'\n at logError (webpack:///app/ssr-error-log-ignore-listed/page.js?[search]:10:12)' +
(process.env.NEXT_TEST_CI
? '\n at run (webpack:///node_modules/[pnpm]/internal-pkg/index.js?[search]:2:0)'
: '\n at run (webpack://[fixture-root]/node_modules/[pnpm]/internal-pkg/index.js?[search]:2:0)') +
'\n at Page (webpack:///app/ssr-error-log-ignore-listed/page.js?[search]:10:6)' +
'\n 3 |'
)
})
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
}
)
// FIXME: Turbopack resolver bug
// FIXME: Turbopack build? bugs taint the whole dev server
;(isTurbopack ? it.skip : it)(
'stack frames are not ignore-listed in rsc',
async () => {
await next.render('/rsc-error-log-ignore-listed')
if (isNextDev) {
await retry(() => {
expect(normalizeCliOutput(next.cliOutput)).toContain(
isTurbopack
? // FIXME: Turbopack resolver bug
"Module not found: Can't resolve 'internal-pkg'"
: '\nError: Boom' +
'\n at logError (webpack:///app/rsc-error-log-ignore-listed/page.js?[search]:4:16)' +
// FIXME: Method name should be "Page"
'\n at logError (webpack:///app/rsc-error-log-ignore-listed/page.js?[search]:9:12)' +
(process.env.NEXT_TEST_CI
? '\n at run (webpack:///node_modules/[pnpm]/internal-pkg/index.js?[search]:2:0)'
: '\n at run (webpack://[fixture-root]/node_modules/[pnpm]/internal-pkg/index.js?[search]:2:0)') +
'\n at Page (webpack:///app/rsc-error-log-ignore-listed/page.js?[search]:9:6)' +
'\n 2 |'
)
})
} else {
// TODO: Test `next build` with `--enable-source-maps`.
}
}
)
it('logged errors preserve their name', async () => { it('logged errors preserve their name', async () => {
await next.render('/rsc-error-log-custom-name') await next.render('/rsc-error-log-custom-name')
......
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