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

[Bug]: Wrong location of __mf__virtual in monorepo #254

Open
4 tasks done
Xeikim opened this issue Feb 12, 2025 · 2 comments
Open
4 tasks done

[Bug]: Wrong location of __mf__virtual in monorepo #254

Xeikim opened this issue Feb 12, 2025 · 2 comments
Labels
bug Something isn't working COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it good first issue Good for newcomers

Comments

@Xeikim
Copy link

Xeikim commented Feb 12, 2025

Describe the bug

My repository is a monorepo,and the project structure is roughly:

packages
   - vite
       - node_modules
       - vite.config.ts
   - vite
       - node_modules
       - vite.config.ts
   - ...
server
    - createServer.ts

server is used to create a vite service and dynamically start different vite applications based on the access route.

When @module-federation/vite creates __mf__virtual, it looks for node_modules based on process.cwd()

const nodeModulesDir = (function findNodeModulesDir(startDir = process.cwd()) {
let currentDir = startDir;
while (currentDir !== parse(currentDir).root) {
const nodeModulesPath = join(currentDir, 'node_modules');
if (existsSync(nodeModulesPath)) {
return nodeModulesPath;
}
currentDir = dirname(currentDir);
}

In this case, __mf__virtual will be placed in the node_modules of the server directory, not the node_modules in the packages, leading to potential conflicts

Is it a more reasonable solution to look for node_modules based on root of vite.config instead of process.cwd()

Version

v6.0.6

Reproduction

https://codesandbox.io/p/github/Xeikim/mf-monorepo/main?import=true

Relevant log output

Validations

@Xeikim Xeikim added the bug Something isn't working label Feb 12, 2025
@gioboa
Copy link
Collaborator

gioboa commented Feb 12, 2025

Thanks @Xeikim for your report. It seems reasonable to me. This is a great opportunity for a PR. If you move these files manually in the right location, is it working fine?

@gioboa gioboa added good first issue Good for newcomers COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it labels Feb 12, 2025
@Xeikim
Copy link
Author

Xeikim commented Feb 13, 2025

Thanks @Xeikim for your report. It seems reasonable to me. This is a great opportunity for a PR. If you move these files manually in the right location, is it working fine?

thanks for your reply.

I tried to solve it, but encountered some problems: #256

I set the root path in the [configResolved] hook:
https://github.com/Xeikim/vite/blob/f278ee575f9b4edeb9284b9468e3e87d5f9be14c/src/index.ts#L35-L41

The issue
We need virtual module paths when adding entry plugins:
https://github.com/Xeikim/vite/blob/f278ee575f9b4edeb9284b9468e3e87d5f9be14c/src/index.ts#L50-L53
When getHostAutoInitPath() is called (which internally uses VirtualModule.getPath()), the root path hasn't been set yet because the configResolved hook hasn't executed. This leads to incorrect path resolution. Transforming getPath into a dynamic function seems a bit difficult

Questions

  • What's the recommended way to handle this kind of initialization timing issue in Vite plugins?
  • Should we consider restructuring how virtual module paths are resolved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working COMMUNITY: PR is welcomed We think it's a good feature to have but would love for the community to help with the PR for it good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants