Skip to content

Loaders allow breaking JS spec invariants #171

Open
@nicolo-ribaudo

Description

@nicolo-ribaudo

Consider this file:

import * as staticImport from "a";

const dynamicImport = await import("a");

console.log(staticImport === dynamicImport);

Two imports in the same file must always resolve to the same module, so that console.log must always log true. However, the following loader allow breaking that language invariant and console.log will log false:

import { register } from "node:module"

register(import.meta.url);

let counter = 0;

export function resolve(specifier, context, nextResolve) {
  if (specifier === "a") {
    return nextResolve(counter++ ? "./a.js" : "./b.js", context);
  }
  return nextResolve(specifier, context);
}

For reference, the relevant specification is https://tc39.es/ecma262/#sec-HostLoadImportedModule:

Where referrer is the importer module, specifier is the string passed to import ("a" in both cases in this example), and "the result is a normal completion" means that loading the module does not throw.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions