Skip to content

Commit 926983c

Browse files
committed
Cleanup Turbopack and error message
1 parent 43b2df1 commit 926983c

File tree

6 files changed

+43
-54
lines changed

6 files changed

+43
-54
lines changed

crates/next-core/src/middleware.rs

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::env;
2-
31
use anyhow::Result;
4-
use pathdiff::diff_paths;
52
use turbo_rcstr::{RcStr, rcstr};
63
use turbo_tasks::{ResolvedVc, Vc, fxindexmap};
74
use turbo_tasks_fs::FileSystemPath;
@@ -125,30 +122,6 @@ struct MiddlewareMissingExportIssue {
125122
file_path: FileSystemPath,
126123
}
127124

128-
impl MiddlewareMissingExportIssue {
129-
fn get_relative_path(&self) -> String {
130-
// Get relative path from current working directory (like process.cwd() in JavaScript)
131-
let cwd = env::current_dir().ok();
132-
let file_path_str = self.file_path.path.as_str();
133-
let relative_path = if let Some(cwd) = cwd {
134-
diff_paths(file_path_str, cwd)
135-
.and_then(|p| p.to_str().map(|s| s.to_string()))
136-
.unwrap_or_else(|| self.file_path.file_name().to_string())
137-
} else {
138-
self.file_path.file_name().to_string()
139-
};
140-
format!("./{}", relative_path)
141-
}
142-
143-
fn get_type_description(&self) -> &str {
144-
if self.file_type.as_str() == "Proxy" {
145-
"proxy (previously called middleware)"
146-
} else {
147-
"middleware"
148-
}
149-
}
150-
}
151-
152125
#[turbo_tasks::value_impl]
153126
impl Issue for MiddlewareMissingExportIssue {
154127
#[turbo_tasks::function]
@@ -167,24 +140,23 @@ impl Issue for MiddlewareMissingExportIssue {
167140

168141
#[turbo_tasks::function]
169142
async fn title(&self) -> Result<Vc<StyledString>> {
170-
let relative_path_str = self.get_relative_path();
171-
172-
// Only the first line goes in title to avoid formatIssue indentation
173143
let title_text = format!(
174-
"The file \"{}\" must export a function, either as a default export or as a named \
175-
\"{}\" export.",
176-
relative_path_str, self.function_name
144+
"{} is missing expected function export name",
145+
self.file_type
177146
);
178147

179148
Ok(StyledString::Text(title_text.into()).cell())
180149
}
181150

182151
#[turbo_tasks::function]
183152
async fn description(&self) -> Result<Vc<OptionStyledString>> {
184-
let relative_path_str = self.get_relative_path();
185-
let type_description = self.get_type_description();
153+
let type_description = if self.file_type == "Proxy" {
154+
"proxy (previously called middleware)"
155+
} else {
156+
"middleware"
157+
};
186158

187-
let migration_bullet = if self.file_type.as_str() == "Proxy" {
159+
let migration_bullet = if self.file_type == "Proxy" {
188160
"- You are migrating from `middleware` to `proxy`, but haven't updated the exported \
189161
function.\n"
190162
} else {
@@ -200,13 +172,11 @@ impl Issue for MiddlewareMissingExportIssue {
200172
- The export is not a function (e.g., an object or constant).\n\
201173
- There's a syntax error preventing the export from being recognized.\n\n\
202174
To fix it:\n\
203-
- Check your \"{}\" file.\n\
204-
- Ensure it has either a default or \"{}\" function export.\n\
175+
- Ensure this file has either a default or \"{}\" function export.\n\
205176
- Restart the dev server if the error persists.\n\n\
206177
Learn more: https://nextjs.org/docs/messages/middleware-to-proxy",
207178
type_description,
208179
migration_bullet,
209-
relative_path_str,
210180
self.function_name
211181
);
212182

packages/next/errors.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,5 +897,6 @@
897897
"896": "oops",
898898
"897": "Expected HTML document to start with doctype prefix",
899899
"898": "The file \"%s\" must export a function, either as a default export or as a named \"%s\" export.\\nThis function is what Next.js runs for every request handled by this %s.\\n\\nWhy this happens:\\n- The file exists but doesn't export a function.\\n- The export is not a function (e.g., an object or constant).\\n- There's a syntax error preventing the export from being recognized.\\n\\nTo fix it:\\n- Check your \"%s\" file.\\n- Ensure it has either a default or \"%s\" function export.\\n- Restart the dev server if the error persists.\\n\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy",
900-
"899": "The file \"%s\" must export a function, either as a default export or as a named \"%s\" export.\\nThis function is what Next.js runs for every request handled by this %s.\\n\\nWhy this happens:\\n%s- The file exists but doesn't export a function.\\n- The export is not a function (e.g., an object or constant).\\n- There's a syntax error preventing the export from being recognized.\\n\\nTo fix it:\\n- Check your \"%s\" file.\\n- Ensure it has either a default or \"%s\" function export.\\n- Restart the dev server if the error persists.\\n\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy"
900+
"899": "The file \"%s\" must export a function, either as a default export or as a named \"%s\" export.\\nThis function is what Next.js runs for every request handled by this %s.\\n\\nWhy this happens:\\n%s- The file exists but doesn't export a function.\\n- The export is not a function (e.g., an object or constant).\\n- There's a syntax error preventing the export from being recognized.\\n\\nTo fix it:\\n- Check your \"%s\" file.\\n- Ensure it has either a default or \"%s\" function export.\\n- Restart the dev server if the error persists.\\n\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy",
901+
"900": "The file \"%s\" must export a function, either as a default export or as a named \"%s\" export.\\nThis function is what Next.js runs for every request handled by this %s.\\n\\nWhy this happens:\\n%s- The file exists but doesn't export a function.\\n- The export is not a function (e.g., an object or constant).\\n- There's a syntax error preventing the export from being recognized.\\n\\nTo fix it:\\n- Ensure this file has either a default or \"%s\" function export.\\n- Restart the dev server if the error persists.\\n\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy"
901902
}

packages/next/src/build/analysis/get-page-static-info.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ function validateMiddlewareProxyExports({
410410
`- The export is not a function (e.g., an object or constant).\n` +
411411
`- There's a syntax error preventing the export from being recognized.\n\n` +
412412
`To fix it:\n` +
413-
`- Check your "${relativeFilePath}" file.\n` +
414-
`- Ensure it has either a default or "${fileName}" function export.\n` +
413+
`- Ensure this file has either a default or "${fileName}" function export.\n` +
415414
`- Restart the dev server if the error persists.\n\n` +
416415
`Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
417416
)

packages/next/src/build/templates/middleware.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ if (typeof handler !== 'function') {
3636
`- The export is not a function (e.g., an object or constant).\n` +
3737
`- There's a syntax error preventing the export from being recognized.\n\n` +
3838
`To fix it:\n` +
39-
`- Check your "${resolvedRelativeFilePath}" file.\n` +
40-
`- Ensure it has either a default or "${fileName}" function export.\n` +
39+
`- Ensure this file has either a default or "${fileName}" function export.\n` +
4140
`- Restart the dev server if the error persists.\n\n` +
4241
`Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
4342
)

test/e2e/app-dir/proxy-missing-export/proxy-missing-export.test.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
22
import { join } from 'node:path'
33
import { writeFile } from 'node:fs/promises'
44

5-
const errorMessage = `The file "./proxy.ts" must export a function, either as a default export or as a named "proxy" export.
6-
This function is what Next.js runs for every request handled by this proxy (previously called middleware).
5+
const errorMessage = `This function is what Next.js runs for every request handled by this proxy (previously called middleware).
76
87
Why this happens:
98
- You are migrating from \`middleware\` to \`proxy\`, but haven't updated the exported function.
@@ -12,8 +11,7 @@ Why this happens:
1211
- There's a syntax error preventing the export from being recognized.
1312
1413
To fix it:
15-
- Check your "./proxy.ts" file.
16-
- Ensure it has either a default or "proxy" function export.
14+
- Ensure this file has either a default or "proxy" function export.
1715
- Restart the dev server if the error persists.
1816
1917
Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
@@ -35,14 +33,25 @@ describe('proxy-missing-export', () => {
3533
'export function middleware() {}'
3634
)
3735

36+
let cliOutput: string
37+
3838
if (isNextDev) {
3939
await next.start().catch(() => {})
4040
// Use .catch() because Turbopack errors during compile and exits before runtime.
4141
await next.browser('/').catch(() => {})
42-
expect(next.cliOutput).toContain(errorMessage)
42+
cliOutput = next.cliOutput
43+
} else {
44+
cliOutput = (await next.build()).cliOutput
45+
}
46+
47+
if (process.env.IS_TURBOPACK_TEST) {
48+
expect(cliOutput).toContain(`./proxy.ts
49+
Proxy is missing expected function export name
50+
${errorMessage}`)
4351
} else {
44-
const { cliOutput } = await next.build()
45-
expect(cliOutput).toContain(errorMessage)
52+
expect(cliOutput)
53+
.toContain(`The file "./proxy.ts" must export a function, either as a default export or as a named "proxy" export.
54+
${errorMessage}`)
4655
}
4756

4857
await next.stop()
@@ -107,16 +116,26 @@ describe('proxy-missing-export', () => {
107116
'const proxy = () => {}; export { proxy as handler };'
108117
)
109118

119+
let cliOutput: string
120+
110121
if (isNextDev) {
111122
await next.start().catch(() => {})
112123
// Use .catch() because Turbopack errors during compile and exits before runtime.
113124
await next.browser('/').catch(() => {})
114-
expect(next.cliOutput).toContain(errorMessage)
125+
cliOutput = next.cliOutput
115126
} else {
116-
const { cliOutput } = await next.build()
117-
expect(cliOutput).toContain(errorMessage)
127+
cliOutput = (await next.build()).cliOutput
118128
}
119129

130+
if (process.env.IS_TURBOPACK_TEST) {
131+
expect(cliOutput).toContain(`./proxy.ts
132+
Proxy is missing expected function export name
133+
${errorMessage}`)
134+
} else {
135+
expect(cliOutput)
136+
.toContain(`The file "./proxy.ts" must export a function, either as a default export or as a named "proxy" export.
137+
${errorMessage}`)
138+
}
120139
await next.stop()
121140
})
122141
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export function x() {}

0 commit comments

Comments
 (0)