nrwl / precise-commits

:sparkles: Painlessly apply Prettier by only formatting lines you have modified anyway!
MIT License
471 stars 20 forks source link

Non-root package.json (working directory) causes duplicate directories in filepaths #3

Closed erik-inkapool closed 6 years ago

erik-inkapool commented 6 years ago

When running precise-commits from a subdirectory to the git directory the diff file paths will include some of the directories in the working directory causing ENOENT: no such file or directory errors.

I'll try to make a PR to fix this, it should be a simple change.

JamesHenry commented 6 years ago

Thanks for reporting, @erik-lundgren! Please could you provide an example file tree for what you are describing? Just want to make sure I understand correctly.

erik-inkapool commented 6 years ago

The issue occurs because git diff, which is used by getModifiedFilenames in git-utils.ts (here) reports file paths from the root directory and working directory is wherever the package.json is located.

With the following file tree:

[root]
-- .git
-- myfolder
   -- package.json
   -- index.ts

You would get [root]/myfolder as working directory and myfolder/index.ts as filename on index.ts line 113. Joining those produces [root]/myfolder/myfolder/index.ts. I believe it could be fixed with this change by using the git root instead of the workingDirectory:

-    fullPath: join(workingDirectory, filename),
+    fullPath: join(gitDirectoryParent, filename),

I decided to hold off on using this as I couldn't quite get it to work locally even with my changes and npm link (likely because of user error).

~I attempted to make a PR to fix it but tests were failing for me locally because of my auto-crlf settings and I couldn't get my typescript 2.7 to compile so I hope the information given above is enough for you to fix it at some point and then I can have another go at making it work for our repo. :) Sorry about the lack of the promised PR.~

Strike that, here's a PR: https://github.com/nrwl/precise-commits/pull/4

JamesHenry commented 6 years ago

Thanks @erik-lundgren!