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

Errors Thrown When Two Nested Modules Import the Same Module #634

Closed
Tracked by #506
NXY666 opened this issue Apr 27, 2024 · 2 comments
Closed
Tracked by #506

Errors Thrown When Two Nested Modules Import the Same Module #634

NXY666 opened this issue Apr 27, 2024 · 2 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@NXY666
Copy link

NXY666 commented Apr 27, 2024

Describe the bug

There are three modules involved: a.ts references both b.ts and c.ts, whereas b.ts references c.ts. All of them utilize a module import syntax similar to import * as x from "@/modules/x".

a.ts:

import * as b from "./b";
import * as c from "./c";

export function a() {
	return b.b() + c.c();
}

b.ts:

import * as c from "./c";

export function b() {
	return "b" + c.c();
}

c.ts:

export function c() {
	return "c";
}

At this point, import it in a certain file (for example, background.ts) will result in an error being thrown:

import * as a from '@/modules/a';

export default defineBackground(() => {
	console.log('Hello background!', {id: browser.runtime.id}, a.a());
});
WXT 0.17.12                                                                                                   22:05:26
√ Started dev server @ http://localhost:3000                                                                  22:05:27
i Pre-rendering chrome-mv3 for development with Vite 5.2.10                                                   22:05:27

 ERROR  Cannot find module '@/modules/a'                                                                      22:05:28
Require stack:
- E:\Projects\demo\entrypoints\background.ts

  Require stack:
  - entrypoints\background.ts
  at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
  at Function.resolve (node:internal/modules/helpers:187:19)
  at _resolve (node_modules\jiti\dist\jiti.js:1:251148)
  at jiti (node_modules\jiti\dist\jiti.js:1:253746)
  at E:/Projects/demo/entrypoints/background.ts:35:17
  at evalModule (node_modules\jiti\dist\jiti.js:1:256443)
  at jiti (node_modules\jiti\dist\jiti.js:1:254371)
  at importEntrypointFile (/E:/Projects/demo/node_modules/wxt/dist/cli.js:2278:23)
  at async getBackgroundEntrypoint (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1502:27)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:1306:18

× Command failed after 1.638 s                                                                                22:05:28

 ERROR  Failed to load entrypoint: entrypoints\background.ts                                                  22:05:28

  at importEntrypointFile (/E:/Projects/demo/node_modules/wxt/dist/cli.js:2290:13)
  at async getBackgroundEntrypoint (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1502:27)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:1306:18
  at async Promise.all (index 0)
  at async findEntrypoints (/E:/Projects/demo/node_modules/wxt/dist/cli.js:1294:23)
  at async internalBuild (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3165:23)
  at async buildAndOpenBrowser (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3494:28)
  at async Object.start (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3459:9)
  at async /E:/Projects/demo/node_modules/wxt/dist/cli.js:3980:5
  at async CAC.<anonymous> (/E:/Projects/demo/node_modules/wxt/dist/cli.js:3911:22)

If I remove the import in the background before starting the dev server, and then add it back after the server has started, it does not throw an error.

I am sure the path is correct because import {a} from "@/modules/a" works.

To Reproduce

demo.zip

Steps to reproduce the bug using the reproduction:

  1. Install dependencies: npm i
  2. Start dev mode: npm run dev

Expected behavior

no error

Environment

  System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i9-13900H
    Memory: 1.83 GB / 15.73 GB
  Binaries:
    Node: 20.11.1 - D:\Program Files\nodejs\node.EXE
    Yarn: 1.22.21 - D:\Program Files\nodejs\yarn.CMD
    npm: 10.5.2 - D:\Program Files\nodejs\npm.CMD
    pnpm: 8.15.5 - D:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (123.0.2420.97)
    Internet Explorer: 11.0.22621.1
@NXY666 NXY666 added the triage Bug or problem that needs to be looked into label Apr 27, 2024
@aklinker1
Copy link
Collaborator

Interesting, this isn't anything special, I must have done this in several of my extensions... I haven't tried out your repro yet, but I will soon. Thanks for the report

@aklinker1
Copy link
Collaborator

Ahh, OK. This is happening during the build, not at runtime. Currently, entrypoints are loaded with jiti during the build to get the entrypoint's options, and this method must not support this for some reason... Not sure why TBH.

For now, I'd just not use import * as, and use named imports instead. Like you said, it works.

I'm gonna add this issue to #506 and mark this as a duplicate, it should be fixed once we use vite to load the entrypoint options instead of jiti. But I'll make sure to re-test this once #506 is merged.

@aklinker1 aklinker1 closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2024
@aklinker1 aklinker1 added bug Something isn't working duplicate This issue or pull request already exists and removed triage Bug or problem that needs to be looked into labels May 4, 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 duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants