Skip to content

Commit 2918422

Browse files
committed
Cleanup Turbopack and error message
1 parent 05f0940 commit 2918422

File tree

5 files changed

+42
-54
lines changed

5 files changed

+42
-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,12 +172,10 @@ 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\n\
175+
- Ensure this file has either a default or \"{}\" function export.\n\n\
205176
Learn more: https://nextjs.org/docs/messages/middleware-to-proxy",
206177
type_description,
207178
migration_bullet,
208-
relative_path_str,
209179
self.function_name
210180
);
211181

packages/next/errors.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,5 +902,6 @@
902902
"901": "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",
903903
"902": "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",
904904
"903": "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\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy",
905-
"904": "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\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy"
905+
"904": "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\\nLearn more: https://nextjs.org/docs/messages/middleware-to-proxy",
906+
"905": "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"
906907
}

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\n` +
413+
`- Ensure this file has either a default or "${fileName}" function export.\n\n` +
415414
`Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
416415
)
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\n` +
39+
`- Ensure this file has either a default or "${fileName}" function export.\n` +
4140
`Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
4241
)
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
1816
Learn more: https://nextjs.org/docs/messages/middleware-to-proxy`
1917

@@ -34,14 +32,25 @@ describe('proxy-missing-export', () => {
3432
'export function middleware() {}'
3533
)
3634

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

4756
await next.stop()
@@ -106,16 +115,26 @@ describe('proxy-missing-export', () => {
106115
'const proxy = () => {}; export { proxy as handler };'
107116
)
108117

118+
let cliOutput: string
119+
109120
if (isNextDev) {
110121
await next.start().catch(() => {})
111122
// Use .catch() because Turbopack errors during compile and exits before runtime.
112123
await next.browser('/').catch(() => {})
113-
expect(next.cliOutput).toContain(errorMessage)
124+
cliOutput = next.cliOutput
114125
} else {
115-
const { cliOutput } = await next.build()
116-
expect(cliOutput).toContain(errorMessage)
126+
cliOutput = (await next.build()).cliOutput
117127
}
118128

129+
if (process.env.IS_TURBOPACK_TEST) {
130+
expect(cliOutput).toContain(`./proxy.ts
131+
Proxy is missing expected function export name
132+
${errorMessage}`)
133+
} else {
134+
expect(cliOutput)
135+
.toContain(`The file "./proxy.ts" must export a function, either as a default export or as a named "proxy" export.
136+
${errorMessage}`)
137+
}
119138
await next.stop()
120139
})
121140
})

0 commit comments

Comments
 (0)