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

feat(ssr): add importer path to error msg when invalid url import occur #11606

Merged
merged 6 commits into from
Feb 18, 2023
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
11 changes: 9 additions & 2 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getEtag from 'etag'
import convertSourceMap from 'convert-source-map'
import type { SourceDescription, SourceMap } from 'rollup'
import colors from 'picocolors'
import type { ViteDevServer } from '..'
import type { ModuleNode, ViteDevServer } from '..'
import {
blankReplacer,
cleanUrl,
Expand Down Expand Up @@ -227,8 +227,15 @@ async function loadAndTransform(
`going through the plugin transforms, and therefore should not be ` +
`imported from source code. It can only be referenced via HTML tags.`
: `Does the file exist?`
const importerMod: ModuleNode | undefined = server.moduleGraph.idToModuleMap
.get(id)
?.importers.values()
.next().value
const importer = importerMod?.file || importerMod?.url
const err: any = new Error(
`Failed to load url ${url} (resolved id: ${id}). ${msg}`,
`Failed to load url ${url} (resolved id: ${id})${
importer ? ` in ${importer}` : ''
}. ${msg}`,
)
err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL
throw err
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { expect, test } from 'vitest'
import { createServer } from '../../server'
import { normalizePath } from '../../utils'

const root = fileURLToPath(new URL('./', import.meta.url))

Expand All @@ -13,11 +15,13 @@ async function createDevServer() {
test('ssrLoad', async () => {
expect.assertions(1)
const server = await createDevServer()
const moduleRelativePath = '/fixtures/modules/has-invalid-import.js'
const moduleAbsolutePath = normalizePath(path.join(root, moduleRelativePath))
try {
await server.ssrLoadModule('/fixtures/modules/has-invalid-import.js')
await server.ssrLoadModule(moduleRelativePath)
} catch (e) {
expect(e.message).toBe(
'Failed to load url ./non-existent.js (resolved id: ./non-existent.js). Does the file exist?',
`Failed to load url ./non-existent.js (resolved id: ./non-existent.js) in ${moduleAbsolutePath}. Does the file exist?`,
)
}
})
Expand Down