Skip to content

Non-unique V8 scriptUrl when using dynamic import in CJS #23783

@demurgos

Description

@demurgos
  • Version: 10.12.0
  • Platform: Linux 64 bit
  • Subsystem: Modules

This bug is a regression in 10.12.0, it worked in 10.11.0.
I am currently working on Node tooling and code coverage using the V8 profiler. When requesting profiling data, V8 returns an object per script. This object has a url value (the value passed to V8 when creating the module).
To support coverage, the URL should be unique: for each file, there should be unique profiling data.

The latest Node version breaks this unicity when dealing with dynamic imports in CJS.

If I have the following main.js (commonJS) file:

import('./hello-world')
  .then(helloWorld => helloWorld.helloWorld())

And execute it (with --experimental-modules), V8 receives the following modules with the same URL:

// scriptId: 76
// url: file:///data/projects/web/istanbulize/src/test/fixtures/esm-hello-world/main.js
  import {
    executor,
    $default
  } from "";
  export {
    $default as default
  }
  if (typeof executor === "function") {
    // add await to this later if top level await comes along
    executor()
  }
// scriptId: 77
// url: file:///data/projects/web/istanbulize/src/test/fixtures/esm-hello-world/main.js
(function (exports, require, module, __filename, __dirname) { import('./hello-world')
  .then(helloWorld => helloWorld.helloWorld())

});

The first module should not use this url since it does not really correspond to this file but is an internal module to bridge between CJS and ESM. When importing CJS modules from ESM, they get the cjs-facade:// protocol instead of file://. This should be applied here too to fix the ambiguity.

Older versions of Node (10.11.0 and before) used the system path instead of the file URL for the CJS module. You had file:///main.js and /main.js so it was possible to detect this case and ignore the file URL because it corresponds to the wrapper. Now that file URLs are used everywhere (a good thing), it is no longer possible to distinguish the internal wrapper from the real file unless you look at the source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.moduleIssues and PRs related to the module subsystem.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions