Skip to content
Open
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
4 changes: 2 additions & 2 deletions packages/vite/src/module-runner/sourcemap/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ function wrapCallSite(frame: CallSite, state: State) {
// from getScriptNameOrSourceURL() instead
const source = frame.getFileName() || frame.getScriptNameOrSourceURL()
if (source) {
const line = frame.getLineNumber() as number
const column = (frame.getColumnNumber() as number) - 1
const line = frame.getLineNumber() ?? 0
const column = (frame.getColumnNumber() ?? 1) - 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix does not prevent negative columns in all cases. When getColumnNumber() returns 0 (a valid non-nullish value), the result is still -1 because the nullish coalescing operator (??) only handles null or undefined, not 0. This means negative columns can still occur, contradicting the PR's stated purpose.

To actually prevent negative columns, the code should be:

const column = Math.max(0, (frame.getColumnNumber() ?? 1) - 1)
Suggested change
const column = (frame.getColumnNumber() ?? 1) - 1
const column = Math.max(0, (frame.getColumnNumber() ?? 1) - 1)

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.


const position = mapSourcePosition({
name: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { runInThisContext } from 'node:vm'
import { resolve } from 'node:path'
import { describe, expect } from 'vitest'
import type { ViteDevServer } from '../../..'
import type { ModuleRunnerContext } from '../../../../module-runner'
import { ESModulesEvaluator } from '../../../../module-runner'
import {
createFixtureEditor,
createModuleRunnerTester,
Expand Down Expand Up @@ -135,3 +139,39 @@ describe('module runner initialization', async () => {
`)
})
})

describe('module runner with node:vm executor', async () => {
class Evaluator extends ESModulesEvaluator {
async runInlinedModule(_: ModuleRunnerContext, __: string) {
// Mimics VitestModuleEvaluator
const initModule = runInThisContext(
'() => { throw new Error("example")}',
{
lineOffset: 0,
columnOffset: -100,
filename: resolve(import.meta.dirname, 'fixtures/a.ts'),
},
)

initModule()
}
}

const it = await createModuleRunnerTester(
{},
{
sourcemapInterceptor: 'prepareStackTrace',
evaluator: new Evaluator(),
},
)

it('should not crash when error stacktrace contains negative column', async ({
runner,
}) => {
const error = await runner.import('/fixtures/a.ts').catch((err) => err)

expect(() =>
error.stack.includes('.stack access triggers the bug'),
).not.toThrow()
})
})