Skip to content

Commit bb5f826

Browse files
committed
fix: show root relative path
1 parent b72127b commit bb5f826

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/plugin-rsc/src/plugins/validate-import.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path'
12
import type { DevEnvironment, Plugin, Rollup } from 'vite'
23

34
// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
@@ -51,7 +52,11 @@ export function validateImportPlugin(): Plugin {
5152
if (this.environment.mode === 'dev') {
5253
if (id.startsWith(`\0virtual:vite-rsc/validate-imports/invalid/`)) {
5354
const chain = getImportChainDev(this.environment, id)
54-
const error = formatError(chain, this.environment.name)
55+
const error = formatError(
56+
chain,
57+
this.environment.name,
58+
this.environment.config.root,
59+
)
5560
if (error) {
5661
this.error({
5762
id: chain[1],
@@ -69,15 +74,23 @@ export function validateImportPlugin(): Plugin {
6974
this,
7075
'\0virtual:vite-rsc/validate-imports/invalid/server-only',
7176
)
72-
const serverOnlyError = formatError(serverOnly, this.environment.name)
77+
const serverOnlyError = formatError(
78+
serverOnly,
79+
this.environment.name,
80+
this.environment.config.root,
81+
)
7382
if (serverOnlyError) {
7483
throw new Error(serverOnlyError)
7584
}
7685
const clientOnly = getImportChainBuild(
7786
this,
7887
'\0virtual:vite-rsc/validate-imports/invalid/client-only',
7988
)
80-
const clientOnlyError = formatError(clientOnly, this.environment.name)
89+
const clientOnlyError = formatError(
90+
clientOnly,
91+
this.environment.name,
92+
this.environment.config.root,
93+
)
8194
if (clientOnlyError) {
8295
throw new Error(clientOnlyError)
8396
}
@@ -121,6 +134,7 @@ function getImportChainBuild(ctx: Rollup.PluginContext, id: string): string[] {
121134
function formatError(
122135
chain: string[],
123136
environmentName: string,
137+
root: string,
124138
): string | undefined {
125139
if (chain.length === 0) return
126140
const id = chain[0]!
@@ -130,7 +144,9 @@ function formatError(
130144
result += chain
131145
.slice(1, 6)
132146
.map(
133-
(id, i) => ' '.repeat(i + 1) + `imported by ${id.replaceAll('\0', '')}\n`,
147+
(id, i) =>
148+
' '.repeat(i + 1) +
149+
`imported by ${path.relative(root, id).replaceAll('\0', '')}\n`,
134150
)
135151
.join('')
136152
if (chain.length > 6) {

0 commit comments

Comments
 (0)