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

fix(vite-node): invalidate source map cache #6774

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ jobs:
run: pnpm run build

- name: Test
run: pnpm run test:ci
run: pnpm -C test/vite-node test
# run: pnpm run test:ci

- name: Test Examples
run: pnpm run test:examples
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-node/src/hmr/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { HMRPayload, Plugin } from 'vite'
import { EventEmitter } from 'node:events'
import { sourceMapCache } from '../source-map-cache'

export type EventType = string | symbol
export type Handler<T = unknown> = (event: T) => void
Expand Down Expand Up @@ -73,5 +74,9 @@ export function viteNodeHmrPlugin(): Plugin {
}
}
},

handleHotUpdate(ctx) {
delete sourceMapCache[ctx.file]
},
}
}
1 change: 1 addition & 0 deletions packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export class ViteNodeServer {
}

getSourceMap(source: string) {
source = normalizeModuleId(source)
const fetchResult = this.fetchCache.get(source)?.result
if (fetchResult?.map) {
return fetchResult.map
Expand Down
7 changes: 7 additions & 0 deletions packages/vite-node/src/source-map-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { TraceMap } from '@jridgewell/trace-mapping'

// Maps a file path to a source map for that file
export const sourceMapCache: Record<
string,
{ url: string | null; map: TraceMap | null }
> = {}
7 changes: 1 addition & 6 deletions packages/vite-node/src/source-map-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,14 @@ import type {
import fs from 'node:fs'
import path from 'node:path'
import { originalPositionFor, TraceMap } from '@jridgewell/trace-mapping'
import { sourceMapCache } from './source-map-cache'

// Only install once if called multiple times
let errorFormatterInstalled = false

// Maps a file path to a string containing the file contents
const fileContentsCache: Record<string, string> = {}

// Maps a file path to a source map for that file
const sourceMapCache: Record<
string,
{ url: string | null; map: TraceMap | null }
> = {}

// Regex for detecting source maps
const reSourceMap = /^data:application\/json[^,]+base64,/

Expand Down
16 changes: 16 additions & 0 deletions test/vite-node/src/watch/source-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 1
// 1
async function main() {
try {
// 2
// 2
throw new Error('boom')
}
catch (e) {
// eslint-disable-next-line no-console
console.log(e)
}
}
// 3
// 3
main()
8 changes: 8 additions & 0 deletions test/vite-node/test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@
editFile(entryPath, c => c.replace('test 1', 'test 2'))
await viteNode.waitForStdout('test 2')
})

it('invalidate source map cache', async () => {
const entryPath = resolve(__dirname, '../src/watch/source-map.ts')
const { viteNode } = await runViteNodeCli('--watch', entryPath)
await viteNode.waitForStdout('source-map.ts:7:11')
editFile(entryPath, c => c.replaceAll(' // 2\n', ''))
await viteNode.waitForStdout('source-map.ts:5:11')

Check failure on line 61 in test/vite-node/test/cli.test.ts

View workflow job for this annotation

GitHub Actions / Build&Test: node-20, windows-latest

test/cli.test.ts > invalidate source map cache

Error: Timeout when waiting for error "source-map.ts:5:11". Received: stdout: Error: boom at main (D:\a\vitest\vitest\test\vite-node\src\watch\source-map.ts:7:11) at D:\a\vitest\vitest\test\vite-node\src\watch\source-map.ts:16:1 at ViteNodeRunner.runModule (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:400:11) at ViteNodeRunner.directRequest (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:382:16) at ViteNodeRunner.cachedRequest (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:207:14) at ViteNodeRunner.executeFile (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:170:12) at CAC.run (file:///D:/a/vitest/vitest/packages/vite-node/dist/cli.mjs:99:5) Error: boom at main (D:\a\vitest\vitest\test\vite-node\src\watch\source-map.ts:7:11) at D:\a\vitest\vitest\test\vite-node\src\watch\source-map.ts:16:1 at ViteNodeRunner.runModule (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:400:11) at ViteNodeRunner.directRequest (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:382:16) at ViteNodeRunner.cachedRequest (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:207:14) at ViteNodeRunner.executeId (file:///D:/a/vitest/vitest/packages/vite-node/dist/client.mjs:174:12) at async Promise.all (index 0) at handleMessage (file:///D:/a/vitest/vitest/packages/vite-node/dist/chunk-hmr.mjs:161:7) stderr: ❯ test/cli.test.ts:61:18
})
Loading