nx-labs icon indicating copy to clipboard operation
nx-labs copied to clipboard

@nx/rspack `withNx`, `withReact`, and `withWeb` are all broken with current `nx add` command

Open helgardferreira opened this issue 7 months ago • 0 comments

Current Behavior

If you follow the current @nx/rspack documentation, it's stated that users should execute nx add @nx/rspack to Add Rspack to an existing workspace. However, if you execute nx add @nx/rspack without NX_ADD_PLUGINS=false, then a "crystal" plugin setup will be generated in the user's nx.json file like so:

"plugins": [
  {
    "plugin": "@nx/rspack/plugin",
    "options": {
      "buildTargetName": "build",
      "serveTargetName": "serve",
      "previewTargetName": "preview"
    }
  }
],

If a "crystal" plugin setup for @nx/rspack is located in a user's nx.json file then the current implementations of withNx, withReact, and withWeb are guaranteed to fail since they assume context.projectGraph is defined - which it won't be for the initial run of composePlugins's combined function since composePlugin is invoked (indirectly) by readRspackOptions not runExecutor. In other words, withNx, withReact, and withWeb do not have a check for process.env['NX_TASK_TARGET_PROJECT'] to ensure that context.projectGraph is defined by runExecutor.

Expected Behavior

Either:

  • The @nx/rspack documentation should be updated as to not mention nx add @nx/rspack (not great)
  • The @nx/rspack generators should be updated to prevent updating the nx.json file (okayish)
  • The implementations for withNx, withReact, and withWeb should add a basic check for process.env['NX_TASK_TARGET_PROJECT'] and skip their steps until it is safe to do so (better)
  • The implementations for withNx, withReact, and withWeb should be updated to complete the crystal plugin integration (best)

Proposed Solution

Honestly, the quickest fix for the time being (until the crystal plugin integration is done) would be to something like this to withNx, withReact, and withWeb:

if (!process.env['NX_TASK_TARGET_PROJECT']) {
  return Object.assign({}, config)
}

helgardferreira avatar Jul 07 '24 16:07 helgardferreira