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

Daemon shouldn't spawn server when analyzing Vite config file #28892

Closed
aaronadamsCA opened this issue Nov 11, 2024 · 3 comments
Closed

Daemon shouldn't spawn server when analyzing Vite config file #28892

aaronadamsCA opened this issue Nov 11, 2024 · 3 comments

Comments

@aaronadamsCA
Copy link

aaronadamsCA commented Nov 11, 2024

Current Behavior

With @nx/vite/plugin installed and vite.config.ts referencing the Remix Cloudflare plugin, every time the Nx daemon reads the Vite config file, it spawns a workerd server runtime process that never exits.

As you continue working, the Nx daemon does this again and again until system resources are exhausted.

Image

Expected Behavior

Analyzing a Vite config shouldn't start a dev server.

Steps to Reproduce

  1. Run npx create-remix@latest ./my-app --template remix-run/remix/templates/cloudflare to create a Remix + Vite + Cloudflare app.
  2. Install @nx/vite/plugin.
  3. Run the Nx Console (i.e. VS Code extension).

Nx config:

{ "plugins": [{ "plugin": "@nx/vite/plugin", "options": {} }] }

Vite config:

import { cloudflareDevProxyVitePlugin, vitePlugin } from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
  root: import.meta.dirname,
  plugins: [cloudflareDevProxyVitePlugin(), vitePlugin()],
});

Nx Report

Node           : 22.11.0
OS             : linux-x64
Native Target  : x86_64-linux
pnpm           : 9.12.3

nx             : 20.0.12
@nx/js         : 20.0.12
@nx/eslint     : 20.0.12
@nx/workspace  : 20.0.12
@nx/devkit     : 20.0.12
@nx/vite       : 20.0.12
typescript     : 5.6.3
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/js/typescript
@nx/vite/plugin

Additional Information

I suspect this might be an issue with the Remix Vite plugin itself, but I'm not sure so I'm starting here.

I tried removing the Vite plugin and using run-command but the problem persisted.

@aaronadamsCA
Copy link
Author

I temporarily uninstalled the Nx Console to work around the memory leak, but I noticed Wrangler servers are still being started every time I invoke the Nx CLI, too. Thankfully those server processes seem to exit cleanly, so there's no memory leak at the moment.

I noticed because I was still hitting the 200-port limit on Codespaces. Thankfully I can probably work around that.

So I suspect there are really two issues here:

  • When Nx reads the Vite config, it inadvertently starts a Wrangler dev server.
  • When invoked by the Nx daemon, those Wrangler servers hang, causing a massive memory leak.

I wonder if this would also explain why so many Nx users experience vite.config.*.timestamp* files littering the workspace, which would also seem to suggest a hung Vite process.

I don't know why Nx executes the Vite config as opposed to parsing it, but it seems like you need to be able to guarantee that there are no side effects when doing so.

@Coly010
Copy link
Contributor

Coly010 commented Jan 31, 2025

@aaronadamsCA We don't "execute" the confg, we do load it however, using Vite's API for loading the config.
Therefore if a plugin is executing something before a task is actually run that would be something for the plugin itself potentially to manage.

That said, we do have a workaround:

import { cloudflareDevProxyVitePlugin, vitePlugin } from "@remix-run/dev";
import { defineConfig } from "vite";

export default defineConfig({
  root: import.meta.dirname,
  plugins: [
    ...(!global.NX_GRAPH_CREATION 
        ? [cloudflareDevProxyVitePlugin()] 
        : []), 
    vitePlugin()
  ],
});

@aaronadamsCA
Copy link
Author

Thanks for the response @Coly010. I filed an issue with the Remix (now React Router) team.

It looks like they start the Wrangler server in their Vite configureServer hook, and it seems like Vite's resolveConfig() API calls that hook internally.

Thanks for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants