Resolve prettier config and binary from the file to format, not from cwd
See original GitHub issueHey hey! Thanks once again for making this awesome CLI tool!
Today I have encountered a problem related to my (possibly unconventional) project structure.
The structure is as follows
.
├── .git
└── src
└── js
├── .prettierrc
├── node_modules
│ ├── .bin
│ └── prettier
├── package-lock.json
└── some-dir
└── some-file.ts
Everything works well when running prettierd
on some-dir/some-file.ts
from the src/js
directory - both prettier config and prettier version are as expected - from the src/js
directory.
However, the problem happens when the same command is run from the root of the repository. Running cat src/js/some-dir/some-file.ts | prettierd src/js/some-dir/some-file.ts
results in prettierd using its built-in prettier version and the default prettier config. It does not respect that there is prettier
installed and configured in src/js
.
Normally that would not be a problem when using the CLI, but I am using neovim + null-ls that runs prettierd for formatting, and null-ls starts with its cwd
pointed to the root of the repository. Adding a special .nvimrc
configuration to make it start in src/js
would be quite cumbersome to get right in all cases.
As far as I have read the source code, this is because when trying to resolve prettier and the config, prettierd starts from $CWD
(which in this case is the root of the repository). If it started from the file location (src/js/some-dir/some-file.ts
), it would stop at src/js
and notice that prettier is installed there.
I am aware that changing this logic could introduce performance problems (resolving prettier would happen much more often, for different files in the repo). That could be solved by approaching caching a bit differently:
- Split the file path by
/
(in the example above, that would behome
,ubuntu
,myrepo
,src
,js
,some-dir
) - Is there a cache entry (with prettier config and binary) for the whole path? If yes, go to point 4. If no, continue.
- Remove the last part of the file path (essentially, use the parent directory). If reached root of the filesystem, to point 3. If there are still directories to traverse, go to point 2.
- Try to resolve prettier config and prettier binary using existing methods (
require.resolve
), but starting from the file path, not cwd. - Set the resolved prettier config and binary for each subpaths of the file path (in the example above, for both
src/js
,src/js/some-dir
).
Let me know what do you think about changing the approach. If you’d like, I can try to make a PR changing this logic.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
Many thanks for making this change so fast 👍
Ok, so maybe I was wrong here (I didn’t test with 2 different configs, just 2 different prettier versions), so don’t quote me on that 😅
But for the binary, yeah. I can try to tackle that over the weekend if you’d like. I don’t mind if you’ll be able to implement that sooner