iguissouma / nx-console-idea-plugin

Apache License 2.0
39 stars 9 forks source link

Nx actions hang or do nothing when Nx workspace is in a sub-directory of a project #114

Open P4 opened 1 year ago

P4 commented 1 year ago

Describe the bug: I have an IntelliJ project where the Nx workspace is located in a sub-directory of a larger, repo.

project/
├─ nx-workspace/
   ├─ node_modules/
   ├─ package.json
   ├─ nx.json

The plugin does not seem to be working correctly in this configuration. When opening File | Settings | Nx DevTools, the Plugins list is stuck in a loading state, IntelliJ will show a Loading Packages List in the list of background tasks which seems to just hang. Cancelling the task from the UI is not possible, it switches to hanging at Stopping - Loading Packages List instead.

If I install nx globally, then the task will instead finish immediately but produces no output - the list is empty. Other tasks such as Nx Show Affected seem to be affected in a similar fashion.

My attempts at debugging the issue seem to indicate that the problem is caused by the npm exec commands running in the wrong directory - the project root does not contain a package.json, so npm is trying to install nx instead of using the one installed in the nx workspace

Steps to reproduce: Create an Nx workspace in a sub-directory of a Java project, and try to use the plugin

To debug the issue I started looking at the plugin's code to figure out what the hanging task is doing.

The task seems to be launching nx list, and ps aux | grep 'nx list' does show that npm exec nx list is running. Killing that process made the background task disappear which further confirmed that this was the correct command.

I tried to look at /proc/<PID> for the hanging process to maybe look at the command's output; that failed, but instead I found a file descriptor pointing to an npm debug log, which showed npm making an http request:

29 http fetch GET 200 https://registry.npmjs.org/nx 276ms (cache revalidated)

this seemed suspicious since Nx should already be installed, so I checked if the process was running in the correct directory, and it wasn't.

Looking at the plugin code again, it seems to use project.basePath as the working directory, regardless of where the workspace is actually located.

Expected behavior: Nx tasks run relative to the directory which contains the workspace - resolved based on either nx.json, package.json, or provided manually similar to the path to nx.json at the moment.

The plugin executes the version of Nx installed locally inside the workspace instead of one installed globally

When running commands via npm exec, the plugin does not hang indefinitely on trying to install nx, but fails with an error instead.

Cancelling the task from the UI is possible and kills the background process

Additional context:

IDE Version: IntelliJ IDEA 2022.2.3 (Ultimate Edition), Build #IU-222.4345.14 Plugin Version: 0.50.0

iguissouma commented 1 year ago

The plugin don't handle correctly this case where nx repo is a subdirectory of a project, in a lot of place I assume the working directory is project.baseDir

I will try to find a way where I handle this case for all plugin feature.

Please open the nx repo in a separate project window as a workaround..

P4 commented 1 year ago

I've updated the plugin to the latest version (0.52.4). After b6f4c3877cdb13d80658f0884dc52d39c3e1e1f2 the plugin now partially works.

The plugins list still gets stuck, same with Graph Diagram and Show Affected, but I can see the list of tasks and launch them without problems, which is great since that's the most useful feature to me at the moment. Thanks!

iguissouma commented 1 year ago

@P4 you are right the workaround solve only the tool window to run the tasks. I will try to solve the other issues