webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
6.76k stars 159 forks source link

Knip doesn't work with a fresh NX(19.4.2) Angular(18) Monorepo #727

Open AvioLT opened 3 months ago

AvioLT commented 3 months ago

Reproduction url

https://nx.dev/getting-started/tutorials/angular-monorepo-tutorial

Description of the issue

So, I was trying to build up a config slowly to match my NX monorepo structure. I wanted to do this by making a plain new NX project and adding knip to it. However, it fails.

So starting from https://nx.dev/getting-started/tutorials/angular-monorepo-tutorial I get a plain repo with some basic config (Angular, Jest, Cypress, Webpack, ...) image

This gives me a project with the following versions

    "nx": "19.4.2",
    "@angular/core": "~18.0.0",
    "cypress": "^13.13.0",
    "jest": "^29.4.1",

Then run the basic commands of npm init @knip/config & npm run knip and run into the following issue. image

Here is the daemon.log

[NX Daemon Server] - 2024-07-12T11:44:43.731Z - Started listening on: /tmp/1c3b5d0f404b5134dd76/d.sock
[NX Daemon Server] - 2024-07-12T11:44:43.733Z - [WATCHER]: Subscribed to changes within: ../knip/angular-monorepo (native)
[NX Daemon Server] - 2024-07-12T11:44:43.737Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-07-12T11:44:43.738Z - Established a connection. Number of open connections: 2
[NX Daemon Server] - 2024-07-12T11:44:43.739Z - Closed a connection. Number of open connections: 1
[NX Daemon Server] - 2024-07-12T11:44:43.740Z - [REQUEST]: Client Request for Project Graph Received
[NX Daemon Server] - 2024-07-12T11:44:43.826Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-07-12T11:44:43.827Z - [REQUEST]:
[NX Daemon Server] - 2024-07-12T11:44:43.827Z - [REQUEST]:
[NX Daemon Server] - 2024-07-12T11:44:45.961Z - [REQUEST]: Responding to the client. project-graph
[NX Daemon Server] - 2024-07-12T11:44:45.963Z - Time taken for 'total for creating and serializing project graph' 2221.054794ms
[NX Daemon Server] - 2024-07-12T11:44:45.964Z - Done responding to the client project-graph
[NX Daemon Server] - 2024-07-12T11:44:45.964Z - Handled REQUEST_PROJECT_GRAPH. Handling time: 2221. Response time: 3.
[NX Daemon Server] - 2024-07-12T11:44:45.969Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-07-12T11:44:45.970Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-07-12T11:44:45.984Z - Closed a connection. Number of open connections: 0

At which point I'm lost, not sure why the connection is being closed prematurely or something? Maybe somebody with more NX experience can shed some light on it.

webpro commented 3 months ago

So not sure what's tripping up the Nx daemon here. Knip does not use or interfere with it.

When you do npm run knip then Knip executes config files like jest.config.js or vite.config.ts. But Nx itself has only JSON config files. And without --fix or --cache Knip only reads, never writes.

unemx commented 2 months ago

Hello,

I've encountered the same issue with my nx repo (v18), i've upgraded to v19 to check if it would solve the issue : it didn't. I've tried to add knip to both, a new nx project and an old project i had lying around under nx v16 : There was no issue.

I've dug a bit more and ran yarn run knip --debug :

[ 'nx' ]
[.] Glob options
{
  patterns: [ 'jest.config.{js,ts,mjs,cjs,json}', 'package.json' ],
  cwd: '/home/MyUser/work/MyProject',
  dir: '/home/MyUser/work/MyProject',
  gitignore: false,
  absolute: true
}
[.] Jest config file paths (1)
[ '/home/MyUser/work/MyProject/jest.config.ts' ]

 NX   Daemon process terminated and closed the connection

So i've tried to delete the jest.config.ts, and now knip is running correctly. For context, here is the content of the file :

import { getJestProjectsAsync } from '@nx/jest';

export default async () => ({
  projects: await getJestProjectsAsync(),
});

What I tried next was to prevent Knip from scanning this file using negated pattern, but with no success. At this point i'm unsure of why this file is scanned. Maybe it's the nx plugin ?

In any case the workaround for now is to use knip --production to avoid this file. @webpro I'm available if you want more info just lmk, thanks for this great tool :heart_hands:

webpro commented 2 months ago

You can try to disable the Jest plugin by setting jest: false in the Knip config. Or exclude that Jest config file e.g. with an empty array "jest": { "config": [] }

unemx commented 2 months ago

This work but it's still a work around. Why does Knip analyzing this file runs the Nx daemon ?

webpro commented 1 month ago

Any interesting output perhaps when using --debug?

webpro commented 3 days ago

Just had this issue myself. Try this:

NX_DAEMON=false knip