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: prevent crash when v8 incorrectly merges static_initializer's #2

Merged
merged 1 commit into from
Dec 10, 2024

Conversation

AriPerkkio
Copy link

Fixes vitest-dev/vitest#5329.

Reproduction:

ari ~/Git/reproductions (v8-merge) $ cat package.json 
{
  "name": "reproductions",
  "version": "1.0.0",
  "type": "module",
  "private": true,
  "scripts": {},
  "license": "MIT",
  "devDependencies": {
    "@bcoe/v8-coverage": "^0.2.3",
    "@types/node": "^20.11.24"
  }
}
ari ~/Git/reproductions (v8-merge) $ node -v; npm -v
v20.11.1
10.2.4
import { mergeProcessCovs } from "@bcoe/v8-coverage";
import { writeFileSync } from "node:fs";
import inspector from "node:inspector";

writeFileSync(
  "./source-file.js",
  `
export class Foo {
  static First = true ? 1 : 0;
  static Seoncd = 1;
}
`.trim(),
  "utf8"
);

const session = new inspector.Session();

session.connect();
session.post("Profiler.enable");
session.post("Profiler.startPreciseCoverage", {
  callCount: true,
  detailed: true,
});

await import("./source-file.js");

const coverage = await collectCoverage("source-file.js");
console.log(JSON.stringify(coverage, null, 2));

mergeProcessCovs([coverage]);

async function collectCoverage(filename) {
  return new Promise((resolve, reject) => {
    session.post("Profiler.takePreciseCoverage", async (error, coverage) => {
      if (error) return reject(error);

      const result = coverage.result.filter((entry) =>
        entry.url.includes(filename)
      );

      resolve({ result });
    });
  });
}
ari ~/Git/reproductions (v8-merge) $ node index.mjs 
{
  "result": [
    {
      "scriptId": "139",
      "url": "file:///rootdirs/reproductions/source-file.js",
      "functions": [
        {
          "functionName": "",
          "ranges": [
            {
              "startOffset": 0,
              "endOffset": 72,
              "count": 1
            }
          ],
          "isBlockCoverage": true
        },
        {
          "functionName": "<static_initializer>",
          "ranges": [
            {
              "startOffset": 52,
              "endOffset": 69,
              "count": 1
            },
            {
              "startOffset": 45,
              "endOffset": 48,
              "count": 0
            }
          ],
          "isBlockCoverage": true
        }
      ]
    }
  ]
}
/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/range-tree.js:27
                [parent, parentCount] = stack[stack.length - 1];
                                      ^

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
    at RangeTree.fromSortedRanges (/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/range-tree.js:27:39)
    at normalizeFunctionCov (/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:74:41)
    at Object.deepNormalizeScriptCov (/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:59:9)
    at mergeScriptCovs (/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:59:21)
    at mergeProcessCovs (/rootdirs/reproductions/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:34:21)
    at file:///rootdirs/reproductions/index.mjs:30:1

Node.js v20.11.1

@sagupta1001
Copy link

@AriPerkkio @bcoe would it be possible to merge this PR?

@ebrpol
Copy link

ebrpol commented May 2, 2024

I'd also greatly appreciate it if this MR could be merged

@AriPerkkio
Copy link
Author

If you are running into this issue, try upgrading your Node to latest version. Looks like there has been some fixes related to V8 coverage reporting.

@ebrpol
Copy link

ebrpol commented May 6, 2024

If you are running into this issue, try upgrading your Node to latest version. Looks like there has been some fixes related to V8 coverage reporting.

Thanks for the heads up!

Would it be possible to merge this MR nonetheless? I am working on a large project on Node 18, in which it's very costly to upgrade to Node 20.

@ebrpol
Copy link

ebrpol commented May 6, 2024

I've tested on Node 20.12.2 and it still has the same issue. Node 22.1.0 does not have this issue indeed!

I think a lot of people will still be on an LTS version, so I think it will be helpful if this MR can be merged.

chris48s added a commit to badges/shields that referenced this pull request Jun 17, 2024
chris48s added a commit to badges/shields that referenced this pull request Jun 17, 2024
This reverts commit 9b020a3.
@Wroud
Copy link

Wroud commented Aug 12, 2024

I would appreciate it if you would merge the PR

sebbo2002 added a commit to sebbo2002/js-template that referenced this pull request Sep 24, 2024
Should fix test coverage reports (Source: bcoe/v8-coverage#2)
@bcoe bcoe merged commit 1f5d671 into bcoe:master Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use useDefineForClassFields with coverage-v8 and vitest workspace cause error
5 participants