nx icon indicating copy to clipboard operation
nx copied to clipboard

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

Open kevinolivar opened this issue 2 years ago • 11 comments

This bug is already described here https://github.com/nrwl/nx/issues/3370 and https://github.com/nrwl/nx/issues/6370. By now, the frustration of still having this issue around is growing more and more.

This bug is detrimental to the developer's workflow. Therefore, questioning the use of NX. The next information in this ticket is mostly repeated from https://github.com/nrwl/nx/issues/3370.

Note

In my case, it was resolved after making sure that the nx root folder was the git root folder.

Not working:

project
     - .git/
     - nx folder
     - documentation
     - security
     - ...

Working:

project
     - nx folder
        - .git/
        - documentation
        - security
        - ...

However, there is a need for the sub-folder structure for other developers and sometimes, it is not possible to change the folder structure. In addition, the different mentioned work arounds (git stash, git -ma) are not considered the proper solutions as NX should correctly handle the deleted, renamed or moved files.

Current Behavior

After deleting any file in the repository there is an error with yarn start (also with other "affected" commands such as yarn affected:lint, yarn affected:test)

"Error: ENOENT: no such file or directory {{old location of the file}}".

Note

This is also affecting the components importing those files as well as modification to the files itself in some cases (!). It is not related to NX cache but rather this https://github.com/nrwl/nx/issues/3370#issuecomment-858391601

Expected Behavior

Should not be an error after file is removed in case it is not used in project source locally. It also happens when renaming or moving any file around.

Steps to Reproduce

  1. create new workspace via npx create-nx-workspace@latest with Angular or React app
  2. run yarn start or affected:lint
  3. delete any component (eg. app.component.spec.ts)
  4. run yarn start or affected:lint again
  5. get error message

Failure logs

Error: ENOENT: no such file or directory, open {{old location of the file}} at Object.openSync (fs.js:458:3) at Object.readFileSync (fs.js:360:35) [...]

Environment

macOS Big Sur 11.2.3
git version 2.30.1
"@nrwl/cli": "12.5.8",
"@nrwl/cypress": "12.5.8",
"@nrwl/eslint-plugin-nx": "12.5.8",
"@nrwl/express": "12.5.8",
"@nrwl/jest": "12.5.8",
"@nrwl/linter": "12.5.8",
"@nrwl/next": "12.5.8",
"@nrwl/node": "12.5.8",
"@nrwl/react": "12.5.8",
"@nrwl/storybook": "12.5.8",
"@nrwl/tao": "12.5.8",
"@nrwl/web": "12.5.8",
"@nrwl/workspace": "12.5.8",
"typescript": "~4.3.5",
"next": "11.0.1",
"react": "17.0.2",

kevinolivar avatar Sep 30 '21 05:09 kevinolivar

@JamesHenry hey. I'm assigning this to you cause you probably have the most expertise now in this area.

vsavkin avatar Sep 30 '21 16:09 vsavkin

I have found the cause in version 11.6.3 but looking at the latest code in the repo I can see the same problem is still present.

Here's the problem - see packages/workspace/src/core/hasher/git-hasher.ts

function parseGitStatus(path: string): Map<string, string> {
  const output = spawnProcess('git', ['status', '-s', '-u', '-z', '.'], path);
  const changes: Map<string, string> = new Map<string, string>();
  if (!output) {
    return changes;
  }

  // Because `-z` doesn't respect the relative paths configuration,
  // we need to manually strip the root path from the filenames.
  const prefix = spawnProcess('git', ['rev-parse', '--show-prefix'], path);

  const chunks = output.split('\0');
  for (let i = 0; i < chunks.length; i++) {
    const chunk = chunks[i];
    if (chunk.length) {
      // The XY is the two-letter status code.
      // See: https://git-scm.com/docs/git-status#_short_format
      const X = chunk[0].trim();
      const Y = chunk[1].trim();
      const filename = chunk.substring(3).replace(new RegExp(`^${prefix}`), '');
      if (X === 'R') {

The next statement should remove the prefix from chunks[++i]

          changes.set(chunks[++i], 'D'); 
      }
      // If both present, Y shows the status of the working tree
      changes.set(filename, Y || X);
    }
  }
  return changes;
}

willpritchard avatar Jan 07 '22 13:01 willpritchard

This is still happening in version 12.10.1, I moved a file from one folder to another, and then tried to run linting, and got the error below:

UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '...../.spec.ts' at Object.openSync (fs.js:497:3) at Object.readFileSync (fs.js:393:35) at Object.defaultFileRead (//node_modules/@nrwl/workspace/src/core/file-utils.js:161:17) at TypeScriptImportLocator.fromFile (//node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/typescript-import-locator.js:22:38) at //node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:11:27 at Array.forEach () at //node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:10:51 at Array.forEach () at Object.buildExplicitTypeScriptDependencies (//node_modules/@nrwl/workspace/src/core/project-graph/build-dependencies/explicit-project-dependencies.js:9:37) at buildProjectGraph (/***/node_modules/@nrwl/workspace/src/core/project-graph/project-graph.js:182:26)

alvaro450 avatar Feb 02 '22 14:02 alvaro450

@kevinolivar I'm sorry for how long it has taken for us to pick this up again.

I tried following your instructions on the absolute latest Nx and I was not able to reproduce the issue:

WORKSPACE CREATION

❯ npx create-nx-workspace@next
Need to install the following packages:
  create-nx-workspace@next
Ok to proceed? (y)
✔ Workspace name (e.g., org name)     · issuesevenonesevenseven
✔ What to create in the new workspace · angular
✔ Application name                    · angy
✔ Default stylesheet format           · css
✔ Use Nx Cloud? (It's free and doesn't require registration.) · No

 >  NX   Nx is creating your v13.8.0-beta.1 workspace.

   To make sure the command works reliably in all environments, and that the preset is applied correctly,
   Nx will run "npm install" several times. Please wait.

✔ Installing dependencies with npm
✔ Nx has successfully created the workspace.

COMMANDS WITHIN WORKSPACE

npx nx affected:lint --all
rm apps/angy/src/app/app.component.spec.ts
npx nx affected:lint --all

With the above I did not encounter any errors.

JamesHenry avatar Feb 07 '22 15:02 JamesHenry

@alvaro450 You encountered this very recently, but you say still happening in version 12.10.1 - that is not the latest major version of Nx (at the time of writing Nx is at 13.7.3 (latest) and 13.8.0-beta.1 (next))and there have been many important additions and changes relating to project graph creation and management that have gone in in recent versions.

Please could you try upgrading to the latest Nx and provide a reproduction if you encounter it again?

Many thanks! 🙏

JamesHenry avatar Feb 07 '22 15:02 JamesHenry

@kevinolivar Given I cannot reproduce on the latest based on your steps above, please could you also try and do the same (upgrading and providing an updated repo if the error persists)?

Again, so sorry for the delay in getting back to you on this, but hopefully the work that has been going on in the interim has lead to this being resolved 🤞

JamesHenry avatar Feb 07 '22 16:02 JamesHenry

@willpritchard sorry I missed your comment before. If you have a reliable reproduction (and fix) please could you share the reproduction and/or create a PR with the fix? Either one would be greatly appreciated!

JamesHenry avatar Feb 07 '22 16:02 JamesHenry

A suggested fix is to remove the file in the workspace.json file

TemitopeAgbaje avatar Feb 15 '22 10:02 TemitopeAgbaje

Happens for me with 13.8.1. I moved file into another library. And now nx linter does not want to catch this. It keeps thinking the file is in old location. And even stash / commit does not help. Weird...

hudzenko avatar Feb 15 '22 13:02 hudzenko

The issue I encountered (for which I offered a fix on Jan 7th) occurred when I had uncommitted file relocations (via git mv ...) and it now seems that issue is resolved. See below for the proof of that.

I created a fresh workspace with the latest (v13.10.0-beta.1) thus:

% npx create-nx-workspace@next npx: installed 48 in 34.291s ✔ Workspace name (e.g., org name) · GitRenameBug ✔ What to create in the new workspace · angular ✔ Application name · GitRename ✔ Default stylesheet format · scss ✔ Use Nx Cloud? (It's free and doesn't require registration.) · No

NX Nx is creating your v13.10.0-beta.1 workspace.

To make sure the command works reliably in all environments, and that the preset is applied correctly, Nx will run "npm install" several times. Please wait.

✔ Installing dependencies with npm ✔ Nx has successfully created the workspace.

Followed by git add ./git-rename-bug git commit -m 'New workspace' git mv git-rename-bug/apps/git-rename/src/app git-rename-bug/apps/git-rename/src/my-app nx test

The test runs and passes. So it looks like the original problem has been resolved.

There have been recent code updates which have removed the code on which the fix I offered was based.

To verify those steps did expose the issue before it was resolved I created a fresh workspace using the last release before the commit which removed the code I had identified as problematic:

% npx [email protected] npx: installed 48 in 43.341s ✔ Workspace name (e.g., org name) · GitRenameNXBug ✔ What to create in the new workspace · angular ✔ Application name · GitRenameNX ✔ Default stylesheet format · scss ✔ Use Nx Cloud? (It's free and doesn't require registration.) · No

NX Nx is creating your v13.4.3 workspace.

To make sure the command works reliably in all environments, and that the preset is applied correctly, Nx will run "npm install" several times. Please wait.

✔ Installing dependencies with npm ✔ Nx has successfully created the workspace.

Followed by git add ./git-rename-nxbug git commit -m 'New workspace' git mv git-rename-nxbug/apps/git-rename-nx/src/app git-rename-nxbug/apps/git-rename-nx/src/my-app nx test

> NX ERROR ENOENT: no such file or directory, open '[full path here]/git-rename-nxbug/apps/git-rename-nx/src/app/app.component.spec.ts'

Notice the reference is to the old file name.

willpritchard avatar Mar 23 '22 13:03 willpritchard

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] avatar Sep 20 '22 00:09 github-actions[bot]