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

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

Open
AvioLT opened this issue Jul 12, 2024 · 5 comments
Open

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

AvioLT opened this issue Jul 12, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@AvioLT
Copy link

AvioLT commented Jul 12, 2024

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.

@AvioLT AvioLT added the bug Something isn't working label Jul 12, 2024
@webpro
Copy link
Collaborator

webpro commented Jul 13, 2024

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
Copy link

unemx commented Aug 5, 2024

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 🫶

@webpro
Copy link
Collaborator

webpro commented Aug 6, 2024

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
Copy link

unemx commented Aug 6, 2024

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

@webpro
Copy link
Collaborator

webpro commented Aug 21, 2024

Any interesting output perhaps when using --debug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants