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

import.meta.url does not work for class methods #2649

Open
tri2820 opened this issue Nov 23, 2024 · 1 comment
Open

import.meta.url does not work for class methods #2649

tri2820 opened this issue Nov 23, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@tri2820
Copy link

tri2820 commented Nov 23, 2024

Describe the Bug

Using import.meta.url inside a class method results in a SyntaxError: Cannot use 'import.meta' outside a module. This error occurs during the module's import, even without instantiating the class or invoking the method.

The source code is valid & functional when used in a <script type="module">.

const compartment = new Compartment({
      __options__: true,
      globals: {
        console,
      },
      importHook: async (specifier: string): Promise<any> => {
        if (specifier == "test") {
          const module = {
            source: new ModuleSource(
              `console.log("Can use import.meta normally", import.meta.url);
                export class Test  {

                  constructor() {
                    console.log("This throws error", import.meta.url);
                  }
                }`
            ),
            specifier,
            compartment,
          };
          return module;
        }
      },
      importMetaHook: (_moduleSpecifier: string, meta: any) => {
        meta.url = 'https://example.com';
      },
    });

    compartment.import("test");

Steps to Reproduce

  1. Create a Compartment with importHook and importMetaHook.
  2. Use importHook to return a ModuleSource containing code with a class method that accesses import.meta.url.
  3. Observe the error.

Expected Behavior

The code snippet with import.meta.url inside a class method should execute without errors, as it does with standard <script type="module">.

Platform Environment

1.71.114 Chromium: 130.0.6723.58 (Official Build) (64-bit)

"@endo/module-source": "^1.1.2",
"ses": "^1.9.1",

Additional Context

Error Message:

VM6833:5 Uncaught (in promise) SyntaxError: Cannot use 'import.meta' outside a module
    at Object.eval (eval at makeEvaluate (make-evaluate.js:92:27), <anonymous>:12:36)
    at safeEvaluate (make-safe-evaluator.js:78:14)
    at compartmentEvaluate (compartment-evaluate.js:90:10)
    at makeModuleInstance (module-instance.js:445:18)
    at instantiate (module-link.js:124:22)
    at link (module-link.js:53:10)
    at compartmentImportNow (compartment.js:58:37)
    at compartment.js:121:27
@tri2820 tri2820 added the bug Something isn't working label Nov 23, 2024
@kriskowal
Copy link
Member

Thank you for reporting. This reveals an incompleteness in the @endo/module-source Babel transform, which evidently is not visiting every ImportMeta node in the AST.

@kriskowal kriskowal self-assigned this Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants