question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolve prettier config and binary from the file to format, not from cwd

See original GitHub issue

Hey 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:

  1. Split the file path by / (in the example above, that would be home, ubuntu, myrepo, src, js, some-dir)
  2. Is there a cache entry (with prettier config and binary) for the whole path? If yes, go to point 4. If no, continue.
  3. 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.
  4. Try to resolve prettier config and prettier binary using existing methods (require.resolve), but starting from the file path, not cwd.
  5. 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Geliocommented, Sep 12, 2021

Many thanks for making this change so fast 👍

1reaction
Geliocommented, Sep 9, 2021

Hmm, it should use your config as we do pass the fullpath to prettier.resolveConfig. I think the problem is that we’re using a bad cache key, we should probably use dirname(filePath).

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

CLI - Prettier
Use the prettier command to run Prettier from the command line. To run your locally installed version of Prettier, prefix the command with...
Read more >
[RFC] Editor Integration · Issue #918 · prettier/prettier - GitHub
If it finds one, then use this prettier binary. ... locally installed module based on the config file (if user provides one) or...
Read more >
How to use the prettier.resolveConfig function in prettier - Snyk
resolveConfig function in prettier. To help you get started, we've selected a few prettier examples, based on popular ways it is used in...
Read more >
Prettier doesn't format based on my eslint config - Stack Overflow
move .prettierrc content in .eslint (not recommended because prettier plugins sometimes do not read this file); · run from command pallette in ...
Read more >
eslint couldn't find the config "prettier" to extend from. please ...
//terminal> npm run format > prettier --write . 'prettier' is not recognized as an internal or external command, operable program or batch file....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found