Skip to content

ModuleRunner crashing when negative columns in stacktraces #21587

@AriPerkkio

Description

@AriPerkkio

Describe the bug

Seeing module runner crashing in Vitest feature when creating error stacktraces inside node:vm where columnOffset causes negative columns.

Stack traces with negative column? Here's 0 deps example in Node:

Details
import { createHook } from "async_hooks";
import { runInThisContext } from "vm";

const resources = new Map();

const hook = createHook({
  init(asyncId, type, _triggerAsyncId) {
    if (type !== "PROMISE") return;

    const stack = new Error("VITEST_DETECT_ASYNC_LEAKS").stack || "";
    resources.set(asyncId, { type, stack });
  },
  destroy(asyncId) {
    resources.delete(asyncId);
  },
  promiseResolve(asyncId) {
    resources.delete(asyncId);
  },
});

hook.enable();

const codeDefinition = `'use strict';async ()=>{{`;
const code = `\nawait new Promise((resolve) => { console.log('running') });`;

const wrappedCode = `${codeDefinition}${code}\n}}`;

const initModule = runInThisContext(wrappedCode, {
  lineOffset: 0,
  columnOffset: -codeDefinition.length,
  filename: "virtual.mjs",
});

initModule();

hook.disable();

for (const resource of resources.values()) {
  console.log(resource.type, resource.stack, "\n");
}
Error: VITEST_DETECT_ASYNC_LEAKS
    at AsyncHook.init (file:///Users/ari/Git/scripts/async-hooks.mjs:10:19)
    at emitInitNative (node:internal/async_hooks:206:43)
    at emitInitScript (node:internal/async_hooks:513:3)
    at promiseInitHook (node:internal/async_hooks:332:3)
    at promiseInitHookWithDestroyTracking (node:internal/async_hooks:336:3)
    at virtual.mjs:1:-11
    at file:///Users/ari/Git/scripts/async-hooks.mjs:34:1
    at ModuleJob.run (node:internal/modules/esm/module_job:343:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:665:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5) 

Reproduction

https://stackblitz.com/edit/vitejs-vite-gqrvb5ta

Steps to reproduce

This cannot be reproduced on stackblitz.com due to WebContainer's Node handling stacktraces differently. Download the code and run it locally.

$ node index.mjs

System Info

System:
    OS: macOS 26.2
    CPU: (8) arm64 Apple M2
    Memory: 723.86 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 22.22.0 - /Users/ari/.nvm/versions/node/v22.22.0/bin/node
    npm: 10.9.4 - /Users/ari/.nvm/versions/node/v22.22.0/bin/npm
    pnpm: 10.28.2 - /Users/ari/.nvm/versions/node/v22.22.0/bin/pnpm
  Browsers:
    Safari: 26.2
  npmPackages:
    vite: 7.1.5 => 7.1.5

Used Package Manager

pnpm

Logs

Error: `column` must be greater than or equal to 0 (columns start at column 0)
    at originalPositionFor (file:///Users/ari/Git/repros/vite-server/node_modules/.pnpm/vite@7.1.5/node_modules/vite/dist/node/module-runner.js:193:24)
    at getOriginalPosition (file:///Users/ari/Git/repros/vite-server/node_modules/.pnpm/vite@7.1.5/node_modules/vite/dist/node/module-runner.js:237:15)
    at mapSourcePosition (file:///Users/ari/Git/repros/vite-server/node_modules/.pnpm/vite@7.1.5/node_modules/vite/dist/node/module-runner.js:806:26)
    at wrapCallSite (file:///Users/ari/Git/repros/vite-server/node_modules/.pnpm/vite@7.1.5/node_modules/vite/dist/node/module-runner.js:863:18)
    at Function.prepareStackTrace (file:///Users/ari/Git/repros/vite-server/node_modules/.pnpm/vite@7.1.5/node_modules/vite/dist/node/module-runner.js:894:78)
    at prepareStackTraceCallback (node:internal/errors:144:29)
    at AsyncHook.init (file:///Users/ari/Git/repros/vite-server/index.mjs:45:58)
    at emitInitNative (node:internal/async_hooks:206:43)
    at emitInitScript (node:internal/async_hooks:513:3)
    at promiseInitHook (node:internal/async_hooks:332:3)

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: ssrp3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions