nx
nx copied to clipboard
Project disappears from the graph after editing project.json
Current Behavior
Every time I edit a project.json
file in my repo it disappears from the project graph and I cannot execute any more targets against it. Running nx reset
restores the project.
Here's an example:
Expected Behavior
I would expect the daemon to process the update.
GitHub Repo
https://github.com/JakeDern/nx-test
Steps to Reproduce
- Run an nx target for any project in the repo
- Edit the project.json for the target. Even if it's just removing and adding the same text.
- Try to run the target again.
Nx Report
> NX Report complete - copy this into the issue template
Node : 20.11.0
OS : linux-x64
npm : 10.2.4
nx (global) : 18.0.2
nx : 18.0.3
@nx/js : 18.0.3
@nx/workspace : 18.0.3
@nx/devkit : 18.0.3
@nrwl/tao : 18.0.3
Failure Logs
No response
Package Manager Version
No response
Operating System
- [ ] macOS
- [X] Linux
- [ ] Windows
- [ ] Other (Please specify)
Additional Information
I suspect this is environment specific as it's such an obvious bug. I can reproduce this in a fresh repo on my machine, which is what I've linked and is what the screenshots are from. Any tips to debug would be greatly appreciated.
Took the time this morning to try and reproduce this on my personal desktop and the same thing happens.
I'm using wsl2 environment with ubuntu 20.04 in both cases. I hadn't used nx before on this machine at all, so I simply did
npm install -g nx@latest
, npm install
, and then did the same test as above.
Here's some basic version information from the second environment:
As another data point, I cloned/installed on windows and cannot reproduce the issue. Perhaps is something with wsl2 environment specifically:
In between each of these commands I made miscellaneous project.json edits:
Some last diagnostic points if it helps. I was able to reproduce this with nx 17.3.2 and nx 16.9.1 on wsl2 ubuntu 2004.
Here are the daemon logs from when I was running the test, with nx 18.0.4, though it doesn't look like there's anything interesting in there. Any tips for debugging would be greatly appreciated:
nx-test git:(main) ✗ tail -f /home/jakedern/repos/nx-test/.nx/cache/d/daemon.log
[NX Daemon Server] - 2024-02-10T17:58:13.215Z - Started listening on: /tmp/b20c1ae11e58f59ef244/d.sock
[NX Daemon Server] - 2024-02-10T17:58:13.216Z - [WATCHER]: Subscribed to changes within: /home/jakedern/repos
/nx-test (native)
[NX Daemon Server] - 2024-02-10T17:58:13.217Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:58:13.218Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:58:39.285Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:58:39.285Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:58:39.286Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:58:39.287Z - [REQUEST]: Client Request for Project Graph Received
[NX Daemon Server] - 2024-02-10T17:58:39.289Z - [REQUEST]: Updated workspace context based on watched changes
, recomputing project graph...
[NX Daemon Server] - 2024-02-10T17:58:39.289Z - [REQUEST]:
[NX Daemon Server] - 2024-02-10T17:58:39.289Z - [REQUEST]:
[NX Daemon Server] - 2024-02-10T17:58:39.324Z - [REQUEST]: Responding to the client. project-graph
[NX Daemon Server] - 2024-02-10T17:58:39.325Z - Done responding to the client project-graph
[NX Daemon Server] - 2024-02-10T17:58:39.325Z - Handled REQUEST_PROJECT_GRAPH. Handling time: 37. Response ti
me: 1.
[NX Daemon Server] - 2024-02-10T17:58:39.325Z - Time taken for 'total for creating and serializing project gr
aph' 37.39802399999826ms
[NX Daemon Server] - 2024-02-10T17:58:39.329Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:58:48.462Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:58:48.463Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:58:48.463Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:58:48.464Z - [REQUEST]: Client Request for Project Graph Received
[NX Daemon Server] - 2024-02-10T17:58:48.464Z - [REQUEST]: Responding to the client. project-graph
[NX Daemon Server] - 2024-02-10T17:58:48.465Z - Done responding to the client project-graph
[NX Daemon Server] - 2024-02-10T17:58:48.465Z - Handled REQUEST_PROJECT_GRAPH. Handling time: 0. Response tim
e: 1.
[NX Daemon Server] - 2024-02-10T17:58:48.465Z - Time taken for 'total for creating and serializing project gr
aph' 0.18960900000092806ms
[NX Daemon Server] - 2024-02-10T17:58:48.477Z - [REQUEST]: Responding to the client. handleHashTasks
[NX Daemon Server] - 2024-02-10T17:58:48.477Z - Done responding to the client handleHashTasks
[NX Daemon Server] - 2024-02-10T17:58:48.478Z - Handled HASH_TASKS. Handling time: 4. Response time: 1.
[NX Daemon Server] - 2024-02-10T17:58:48.568Z - [REQUEST]: Responding to the client. recordOutputsHash
[NX Daemon Server] - 2024-02-10T17:58:48.568Z - Done responding to the client recordOutputsHash
[NX Daemon Server] - 2024-02-10T17:58:48.568Z - Handled RECORD_OUTPUTS_HASH. Handling time: 0. Response time:
0.
[NX Daemon Server] - 2024-02-10T17:58:48.573Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:59:09.311Z - [WATCHER]: 0 file(s) created or restored, 0 file(s) modified,
2 file(s) deleted
[NX Daemon Server] - 2024-02-10T17:59:09.311Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-10T17:59:09.410Z - [REQUEST]: Updated workspace context based on watched changes
, recomputing project graph...
[NX Daemon Server] - 2024-02-10T17:59:09.411Z - [REQUEST]:
[NX Daemon Server] - 2024-02-10T17:59:09.411Z - [REQUEST]: 4913,project.json
[NX Daemon Server] - 2024-02-10T17:59:09.418Z - Time taken for 'hash changed files from watcher' 0.045859999998356216ms
[NX Daemon Server] - 2024-02-10T17:59:37.684Z - [WATCHER]: 0 file(s) created or restored, 0 file(s) modified, 2 file(s) deleted
[NX Daemon Server] - 2024-02-10T17:59:37.684Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-10T17:59:37.884Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-02-10T17:59:37.885Z - [REQUEST]:
[NX Daemon Server] - 2024-02-10T17:59:37.885Z - [REQUEST]: 4913,project.json
[NX Daemon Server] - 2024-02-10T17:59:37.891Z - Time taken for 'hash changed files from watcher' 0.05219000000215601ms
[NX Daemon Server] - 2024-02-10T17:59:38.992Z - [WATCHER]: 0 file(s) created or restored, 0 file(s) modified, 2 file(s) deleted
[NX Daemon Server] - 2024-02-10T17:59:38.992Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-10T17:59:39.393Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-02-10T17:59:39.393Z - [REQUEST]:
[NX Daemon Server] - 2024-02-10T17:59:39.393Z - [REQUEST]: 4913,project.json
[NX Daemon Server] - 2024-02-10T17:59:39.400Z - Time taken for 'hash changed files from watcher' 0.04992000000493135ms
[NX Daemon Server] - 2024-02-10T17:59:42.432Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:59:42.432Z - Closed a connection. Number of open connections: 0
[NX Daemon Server] - 2024-02-10T17:59:42.433Z - Established a connection. Number of open connections: 1
[NX Daemon Server] - 2024-02-10T17:59:42.434Z - [REQUEST]: Client Request for Project Graph Received
[NX Daemon Server] - 2024-02-10T17:59:42.434Z - [REQUEST]: Responding to the client. project-graph
[NX Daemon Server] - 2024-02-10T17:59:42.434Z - Done responding to the client project-graph
[NX Daemon Server] - 2024-02-10T17:59:42.434Z - Handled REQUEST_PROJECT_GRAPH. Handling time: 0. Response time: 0.
[NX Daemon Server] - 2024-02-10T17:59:42.434Z - Time taken for 'total for creating and serializing project graph' 0.14513000000442844ms
[NX Daemon Server] - 2024-02-10T17:59:42.439Z - Closed a connection. Number of open connections: 0
I made an interesting discovery. It seems that if you edit the file using vim or vi then this issue happens, if you don't then everything works fine. Things that I've tried (all in the same wsl2 environment):
- Editing with
vi
: BROKEN - Editing with
nvim
: BROKEN - Editing with VSCode: WORKS
- Editing with
sed -i
: WORKS
I have not the slightest clue why still, but progress is progress.
Some more findings. Here's the log output from the daemon when editing the file with vscode (WORKS):
[NX Daemon Server] - 2024-02-24T04:14:19.933Z - Started listening on: /tmp/b20c1ae11e58f59ef244/d.sock
[NX Daemon Server] - 2024-02-24T04:14:19.934Z - [WATCHER]: Subscribed to changes within: /home/jakedern/repos/nx-test (native)
[NX Daemon Server] - 2024-02-24T04:14:25.848Z - [WATCHER]: project.json was modified
[NX Daemon Server] - 2024-02-24T04:14:25.848Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-24T04:14:25.948Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-02-24T04:14:25.948Z - [REQUEST]: project.json
[NX Daemon Server] - 2024-02-24T04:14:25.948Z - [REQUEST]:
Here is the output from the daemon when editing the file with neovim (BROKEN) :
[NX Daemon Server] - 2024-02-24T04:15:50.709Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-24T04:15:50.710Z - [WATCHER]: 0 file(s) created or restored, 0 file(s) modified, 2 file(s) deleted
[NX Daemon Server] - 2024-02-24T04:15:50.910Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-02-24T04:15:50.911Z - [REQUEST]:
[NX Daemon Server] - 2024-02-24T04:15:50.911Z - [REQUEST]: project.json,4913
Here is the output from the daemon when editing the file with sed -i
(WORKS):
[NX Daemon Server] - 2024-02-24T04:19:03.434Z - [WATCHER]: 1 file(s) created or restored, 0 file(s) modified, 1 file(s) deleted
[NX Daemon Server] - 2024-02-24T04:19:03.435Z - [WATCHER]: Processing file changes in outputs
[NX Daemon Server] - 2024-02-24T04:19:03.535Z - [REQUEST]: Updated workspace context based on watched changes, recomputing project graph...
[NX Daemon Server] - 2024-02-24T04:19:03.535Z - [REQUEST]: project.json
[NX Daemon Server] - 2024-02-24T04:19:03.535Z - [REQUEST]: sed5L9Sut
@AgentEnder I've identified the root cause of this problem. Basically everyone who uses an editor that creates a temp file and replaces the old one as a save strategy (vim/neovim users) will have this issue.
Vim performs writes as creating a temp file and replacing the entire old file rather than doing an in-place update: https://vi.stackexchange.com/questions/25030/vim-not-firing-inotify-events-when-writing-file.
I wrote a small script to test this out and we do in fact get a few events on write, including DELETE:
#!/bin/bash
inotifywait -m -e close_write,moved_to,delete "./" |
while read -r dir action file; do
echo "FILE: '$file', ACTION: '$action'"
done
Editing from neovim:
Setting up watches.
Watches established.
FILE: '4913', ACTION: 'CLOSE_WRITE,CLOSE'
FILE: '4913', ACTION: 'DELETE'
FILE: 'project.json~', ACTION: 'MOVED_TO'
FILE: 'project.json', ACTION: 'CLOSE_WRITE,CLOSE'
FILE: 'project.json~', ACTION: 'DELETE'
Compare that with the saves from VSCode:
Setting up watches.
Watches established.
FILE: 'project.json', ACTION: 'CLOSE_WRITE,CLOSE'
Eventually these updates makes it over to this:https://github.com/nrwl/nx/blob/26b266faf46b1e72ba79b1bdbed8a66d896fa864/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts#L202
From there we go to:https://github.com/nrwl/nx/blob/26b266faf46b1e72ba79b1bdbed8a66d896fa864/packages/nx/src/daemon/server/project-graph-incremental-recomputation.ts#L150
And eventually update the project files. Once that processFilesAndCreateAndSerializeProjectGraph
function returns the project is no longer in the graph. Since every update will trigger a delete then we're left without the file until we save it from an editor that doesn't cause a delete event when writing.
Something that does not make sense is why the DELETE
event is showing up in the watcher code for the project.json
file and not project.json~
like I see with my script:
I wonder if there is some pre-processing that strips it out. Anyways any help would be greatly appreciated!
Thank you for bringing this issue to our attention! I want to assure you that I will take a look as soon as I can.
I encounter the same issue when editing a package.json
file on Windows using Far Manager's built-in text editor. However, using VSCode, as mentioned earlier, works fine.
Closing as duplicate of: https://github.com/nrwl/nx/issues/22945
The problem appears to be with our Rust File Watcher. We're looking into ways to solve this.
In vim and neovim, the quickest workaround is to use set nowritebackup
. However, be cautious as this setting might lead to data loss in rare instances where the file fails to write successfully.
Here is a detailled explanation of why this happens: https://github.com/nrwl/nx/issues/22945#issuecomment-2101048749
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.