Nx: "Error: ENOENT: no such file or directory" for deleted file

Created on 20 Jul 2020  ·  36Comments  ·  Source: nrwl/nx

Current Behavior

This is happens with 10.0.0-rc.0 (not sure if it is the same with v9 stable, maybe caused by new version?)

After moving a model (class or interface) from project to lib there is an error with npm start (and with other commands most probably as well) "Error: ENOENT: no such file or directory {{old location of the file}}". The new model is used correctly from the typescript lib (generated lib with nx) and old file has no any references in code.

Expected Behavior

Should not be an error after file is removed in case it is not used in project source locally (it works correctly right after commit).

With a debugging trough node module I found that the core of the problem - file node_modules/@nrwl/workspace/src/core/hasher/file-hasher.js reads files not from local files but from git (and then as removed file is in git it is included and other functions tries to read it locally and crash because it is removed locally). I do not think it is expected behaviour, correct?

Steps to Reproduce

  1. Create an angular app with nx based on angular.
  2. Create a model (class or interface) somewhere inside main angular project src as a separate file (for example my-model.model.ts)
  3. Commit changes (not sure if push is needed)
  4. Create a library with nx generate @nrwl/workspace:library mylibrary
  5. Move my-model.model.ts to mylibrary, change imports to use a model from lib and remove old my-model.model.ts
  6. Run npm start or ng serve - and you should get an error related to removed file (logs are added below)

Failure Logs

Error: ENOENT: no such file or directory, open '/Users/my-user/PhpstormProjects/my-org/src/node/apps/main-app/src/app/core/store/models/entity.model.ts'
    at Object.openSync (fs.js:443:3)
    at Object.readFileSync (fs.js:343:35)
    at TypeScriptImportLocator.defaultFileRead [as fileRead] (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/file-utils.js:150:17)
    at TypeScriptImportLocator.fromFile (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/typescript-import-locator.js:21:30)
    at Object.values.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:11:27)
    at Array.forEach (<anonymous>)
    at Object.keys.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:10:44)
    at Array.forEach (<anonymous>)
    at buildExplicitTypeScriptDependencies (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:9:30)
    at buildDependenciesFns.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/project-graph.js:61:41)

Environment

@nrwl/angular : 10.0.0-rc.0
@nrwl/cli : 10.0.0-rc.0
@nrwl/cypress : 10.0.0-rc.0
@nrwl/eslint-plugin-nx : Not Found
@nrwl/express : Not Found
@nrwl/jest : 10.0.0-rc.0
@nrwl/linter : Not Found
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/react : Not Found
@nrwl/schematics : Not Found
@nrwl/tao : 10.0.0-rc.0
@nrwl/web : Not Found
@nrwl/workspace : 10.0.0-rc.0
typescript : 3.9.7

core bug

Most helpful comment

Not working in 10.0.6

All 36 comments

happening with v10 stable as well:

simple reproduction:

  • create new workspace via npx create-nx-workspace@latest with Angular app
  • run npm run lint
  • delete app.component.spec.ts
  • run npm run lint again
  • get error message

Same here, just migrated to 10.0.1 and then I renamed a module and I'm getting this now. Is there cache that we can manually invalidate to work around?

Same error for me after deleting a service.

Can confirm that it is likely related to hashing git files.

A potential workaround is to commit your changes on your local branch, or provide an older --base / --head

Btw IDE tslint error is not fixed for me even after commit and push (ok when run trough console, but there is an IDE error for background linting)
```
Error: ENOENT: no such file or directory, open '/Users/my-user/PhpstormProjects/my-org/src/node/apps/main-app/src/app/shared/animations/animations.ts'
at Object.openSync (fs.js:443:3)
at Object.readFileSync (fs.js:343:35)
at TypeScriptImportLocator.defaultFileRead [as fileRead] (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/file-utils.js:150:17)
at TypeScriptImportLocator.fromFile (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/typescript-import-locator.js:21:30)
at Object.values.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:11:27)
at Array.forEach ()
at Object.keys.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:10:44)
at Array.forEach ()
at buildExplicitTypeScriptDependencies (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:9:30)
at buildDependenciesFns.forEach (/Users/my-user/PhpstormProjects/my-org/src/node/node_modules/@nrwl/workspace/src/core/project-graph/project-graph.js:61:41)
````

SOLVED but unhappy with the outcome

I've deleted an angular component that was generated using the CLI.
Im getting the same error now even when I try to run a different app in the workspace.

node v14.3.0
yarn 1.22.4
nx 10.0.0

From the docs "By default, Nx uses a local computation cache. The cache is stored in node_modules/.cache/nx. [...] Nx stores the cached values only for a week, after which they are deleted. To clear the cache, delete the cache directory, and Nx will create a new one next time it tries to access it."

I've manually deleted the '.cache/nx' folder. No success
I've manually deleted all node_modules folder and reinstalled. No success
I've run multiple commands using --skip-nx-cache. No success
I've changed the cacheDirectory. No success

As the fellow reporter stated, this is NOT related to NX caching inside node_modules.

Eventually, I committed my changes in git. After this, everything started working again.

Now what if I did not want to commit, Should we follow any specific process to delete or rename a file?
Should we document this as Common Problems?

Nx use "git ls-tree HEAD -r" command for get list of files in last commit, then comparing this with actual state of repo, so from my point of view, if file is messing Nx need to skip this compare and sign module for rebuild.

had the same, tracked @nrwl/workspace->project-graph ... etc etc ... after a while i backed my changes with a commit

maybe you could add handling for staged files (marked as deleted, renamed, etc..)

Folks, sorry about this. I fixed the issue in Nx 10.0.2.

If you can still reproduce the issue in 10.0.2, please provide a repo that I can take a loot at.

That did it 👍 , thanks!

@vsavkin for scenario when some file is just removed it seems to be ok.
But when I move file to other location (in my case from app to lib) - it is still the same error.

Simple way to replicate:

  • Move some .spec file into another directory and error appears

My team is also having this issue after migrating to @nrwl/[email protected]. Deleting a file is fine but we are unable to rename a file. Seems like you can trigger the error quickly with nx workspace-lint.

I'm having the same issue @10.0.2, I tracked down to file-hasher/getHashesFromGit().
The allFiles() function returns an array containing both the old location and the new location for moved files. This only happens to files that are staged on git.

Repro steps

npx create-nx-workspace repro
cd repro
nx generate @nrwl/angular:app app1/app1
git add .
git commit -m "Message"
nx g remove app1/app1-e2e
nx g remove app1-app1
nx generate @nrwl/angular:app app1
git add .

Now, just run nx serve and a wild error appears.

Workaround

Just unstage the git files. I spent almost 4 hours to discover this

@mukaschultze could you try 10.0.4? Thank you!

In my case:
upgrade to 10.0.4 will be solved.

thanks. @vsavkin

10.0.4 works for me.
I had created and deleted a lib and it kept saying that it cannot find a component from the module. Now I can finally commit my work :tada:

@vsavkin working like a charm, thanks!

Same problem for me. Still there in 10.0.4. I moved and renamed some files and now the project-graph doesn't know where to find them.

@masonembry could you provide a repo with a repro? I mean some stable state + instructions or what to move etc.

@vsavkin looks like everything is working for us on @nrwl/[email protected]

Not working here in 10.0.6.

Only committing the file helps as workaround.

Not working in 10.0.6

Seems like the issue is gone for "@nrwl/workspace": "10.0.7"

I can still reproduce this on @nrwl/workspace 10.0.7.

Steps to reproduce:

  1. Committed changes to package.json and yarn.lock (to update nrwl to 10.0.7)
  2. Have uncommitted changes including a deleted (moved) file

Happens in @nrwl/workspace 10.0.10, I just moved and renamed a file and nx serve fails.

Same issue on 10.0.7, Windows 10 after moving a module in an Angular lib

$ ng serve project
internal/fs/utils.js:230
throw err;
^

Error: ENOENT: no such file or directory, open 'C:\directory/libs/cut/moved.module.ts'
at Object.openSync (fs.js:458:3)
at Object.readFileSync (fs.js:360:35)
at TypeScriptImportLocator.defaultFileRead [as fileRead] (C:\directory\node_modules\@nrwl\workspace\src\corefile-utils.js:150:17)
at TypeScriptImportLocator.fromFile (C:\directory\node_modules\@nrwl\workspace\src\coreproject-graph\build-dependencies\typescript-import-locator.js:21:30)
at C:\directory\node_modules\@nrwl\workspace\src\coreproject-graph\build-dependencies\explicit-project-dependencies.js:11:27
at Array.forEach ()
at C:\directory\node_modules\@nrwl\workspace\src\coreproject-graph\build-dependencies\explicit-project-dependencies.js:10:44
at Array.forEach ()
at buildExplicitTypeScriptDependencies (C:\directory\node_modules\@nrwl\workspace\src\coreproject-graph\build-dependencies\explicit-project-dependencies.js:9:30)
at C:\directory\node_modules\@nrwl\workspace\src\coreproject-graphproject-graph.js:61:41 {

I did it, but not is the correct solution.
I remove the instruction pre-commit (nx workspace-lint && ng lint), I did the commit and I restored the instruction and I did run push, running prepush(nx workspace-lint && ng lint) now.

@shaharkazaz @aikrez @riveroreyes Nx 10.0.12 handles "rename and modify" (or move and modify) case.

Could you try and tell me if the issue still occurs? If it does, could you provide detailed instructions on how to repro:

  1. Only use shell commands (e.g., there is no rename, only mv a b)
  2. Use git add where needed (stashing, unstashing, changes stuff)

I could not reproduce it with my current version (10.0.7) before I try the fixed version.
Tried moving modules, deleting, deleting after commit,...

@vsavkin seems to be ok in v10.0.12, I'll update if anything changes with a reproduction 👍

@vsavkin a colleague with the same codebase (on MacOs) had the problem and it was fixed after updating to 10.0.12

Thank you folks!

I hope all the issues are resolved now. But in case they haven't, I have also added a workaround to basically fall back to 'fs' when we detect that deleted/moved files aren't picked up correctly. It will print a warning. It land be released next week. This introduces a small perf penalty, so I'm going to keep it there till 10.2.

I'm going to close this issue. Thanks again, folks!

Still happening on nx 10.3.0

Same happens with nx 10.3.1 after deleting schematics template files and not committing.

still happens on 10.4.4

the framework could be a bit more pro-active in delivering pre configured paths and lint rules for the project

after project creation lands

@nhhockeyplayer Your imports are wrong and duplicated you should be using import * as userActions from '@energy/feature'
Also, the actions should be exported on the file libs/feature/src/index.ts

Thanks friend

im not a fan of elaborate barrel files

this framework uses them in the most minimal form so Im ok with it

and it works in this fashion

Was this page helpful?
0 / 5 - 0 ratings