prettier icon indicating copy to clipboard operation
prettier copied to clipboard

Feature: handle .prettierignore location like .gitignore and .npmignore

Open bebbi opened this issue 7 years ago • 37 comments

Suggested feature: handle .prettierignore files like .gitignore and .npmignore files, allowing them at any level of the project, not only root.

Use-case: Using prettier in sub-projects and their "container" projects, where they are editable both from within a sub-projects repository, as well as the container repository. Examples: git submodules, git subtree, git subrepo, and mechanisms to sync mono- and sub-repos such as splitsh

In this case, we may need a .prettierignore file that is specific to a sub-project and is located at the sub-tree root rather than the project root. Otherwise, we need duplicate .prettierignore declarations with different paths in the sub-project repo and in the container repo.

From git docs:

Patterns read from a .gitignore file in the same directory as the path, or in any parent directory,
with patterns in the higher level files (up to the toplevel of the work tree) being overridden
by those in lower level files down to the directory containing the file.
These patterns match relative to the location of the .gitignore file.
A project normally includes such .gitignore files in its repository,
containing patterns for files generated as part of the project build.

From npm docs:

Like git, npm looks for .npmignore and .gitignore files in all subdirectories of your package,
not only the root directory.

bebbi avatar Mar 01 '18 08:03 bebbi

Great idea..

lipis avatar Mar 01 '18 10:03 lipis

This looks like a duplicate of #3274.

ikatyang avatar Mar 01 '18 10:03 ikatyang

It's not exactly a duplicate since #3274 is more of a question than a feature request..

I guess it's worth discussing if we should implement this

duailibe avatar Mar 01 '18 12:03 duailibe

Especially useful in monorepos.

lipis avatar Mar 01 '18 12:03 lipis

Usually with monorepos you have a single .prettierignore and Prettier is installed in the root of the packages. So it's not really useful in those cases.

The use case the OP posted though (sub-repos may be edited "isolated") it might be useful but it's more rare.

I'm not convinced we need this feature but I don't oppose.

duailibe avatar Mar 01 '18 13:03 duailibe

FWIW, we use git subtrees and similar in monorepos when externals collaborate on isolated sub-projects.

bebbi avatar Mar 01 '18 13:03 bebbi

Hmm, an extension of this might be to find the .prettierrc per file location. Thus:

child/
  src/
    file.js
  .prettierrc
src/
  file.js
.prettierrc

ericanderson avatar Mar 01 '18 16:03 ericanderson

@ericanderson I think that one works.. (not sure though)

lipis avatar Mar 01 '18 16:03 lipis

This feature would be nice to have, mostly for monorepos, but also for deeply nested build output directories. My first intuition was that this would be supported (because I was used to .gitignore), but it was not.

suchipi avatar Mar 01 '18 21:03 suchipi

One question that needs to be discussed before it is implementable is how this should interact with the --ignore-pathcli flag.

fahrradflucht avatar Mar 12 '18 06:03 fahrradflucht

Huge fan of this. Having both prettierrc and prettierignore follow the git/npm/yarn/eslint/etc. pattern would be really really useful in the monorepo world, where each sub-repo could ideally take care of itself (especially when it comes to ignores) instead of having to have a once-size-fits-all global.

betaorbust avatar Jul 21 '18 04:07 betaorbust

Is this feature still under consideration?

bebbi avatar Mar 21 '19 21:03 bebbi

Yes

suchipi avatar Mar 21 '19 22:03 suchipi

@bebbi, @suchipi I've wanted to fix this issue for a long time, so I took a go at fixing it: https://github.com/prettier/prettier/pull/6203

.prettierignore works just like I expected now, pretty much like .gitignore!

The one edge case is --ignore-path. I decided that if --ignore-path is specified, the ".prettierignore at any level" feature is disabled and we only use the given file. This gives users an escape hatch back to the old behavior by specifying --ignore-path .prettierignore.

Thoughts? What kind of behavior would you expect?

bmalehorn avatar Jun 09 '19 06:06 bmalehorn

By the way: until @bmalehorn work isn't merged, I'm currently adding a symbolic link to from the ./root/.prettierignore to ./root/sub-project/.prettierignore for this to work... quite ugly, but works.

lucasconstantino avatar Jul 17 '19 17:07 lucasconstantino

Could it be, that this is still not fixed?

Is this still under consideration? With tools like Turborepo getting more and more popular, it's very common to have a prettier ignore in each sub-package so prettier can be run in parallel with different configs and ignores.

In our case, we have the setup described above we also have a prettier ignore in the root of our monorepo since we run prettier on several root files with lint-staged and husky to catch some formatting issues. BUT... setting it up in this way is causing prettier to only ever use our root ignore and totally forget about our other sub-package prettier ignores in the scope of VSCode. So The VSCode experience is broken but using turbo execution works since it's scoped to each individual package. This is giving me 2 different linting results (VSCode vs parallel CLI execution).

If .prettierignorerc resolved upwards like .gitignore this wouldn't be an issue. Very frustrating.

It seems to detect sub-packages configs just fine, however -- so it's very odd that the ignore file doesn't behave in the same way.

It's been almost 4 years since this was brought up :/

runewolf7 avatar Feb 06 '22 23:02 runewolf7

Any updates on this one?

elisei-timofte avatar Apr 26 '22 12:04 elisei-timofte

Ugly but works for monorepos (I'm using turborepo):

"scripts": {
    "format": "prettier --ignore-path <(cat ../../.prettierignore) --write ."
}

Make sure to get the path right, in my case the structure is the following

|_ .prettierignore
   \_apps
       \_landing

Credits

wiredmatt avatar Jun 16 '22 03:06 wiredmatt

Nice workaround. If only that worked with vscode format on save.

sscotth avatar Jun 16 '22 14:06 sscotth

Another workaround when working with VSCode is working inside /.vscode/XXX.code-workspace-files instead. In this environment, VSCode & prettier behave as they should!

Btw this also has many more benefits (e.g. it splits the tree-view into different packages, you can quickly open new terminal sessions on specific package-levels etc.).

More info: https://code.visualstudio.com/docs/editor/workspaces

wottpal avatar Aug 02 '22 12:08 wottpal

I just plop a .prettierignore at every level. It's flexible.

httpete avatar Oct 14 '22 02:10 httpete

This would likely solve the problem when (in a monorepo) you run prettier in every package but you only have a config and ignore file at the repo root. (this is really apparent in turborepo managed monorepos)

Consider this scenario:

<reporoot>/.prettierrc.cjs
<reporoot>/.prettierignore
<reporoot>/packages/foo (cwd when turbo runs a task, this is where stuff is formatted & checked)

Prettier does pick up the config file two levels above but not the ignore file.

For now I'm forced to copy the ignore file in every package folder

AlexAegis avatar Jan 16 '23 22:01 AlexAegis

It's bummer how inconsistent this behavior is; it resolves the config file recursively upwards but not the ignore file

m3hari avatar Feb 01 '23 02:02 m3hari

Up-voting getting this resolved finally.

jmiller-nexient avatar Feb 13 '23 15:02 jmiller-nexient

Workaround:

  1. Install find-up-cli (https://www.npmjs.com/package/find-up-cli)
  2. Use the prettier argument --ignore-path $(find-up .prettierignore)

To enable this in npm scripts on Windows, run npm config set script-shell pwsh (uses PowerShell instead of cmd to allow the substitution).

find-up-cli, by the prolific @sindresorhus, finds a file by walking up parent directories.

andrewdbond avatar Apr 10 '23 16:04 andrewdbond

Any updates on this? This is a very perplexing issue to still be running into 5+ years after it was reported and this behavior is common for other tools.

matthewbordas avatar May 01 '23 17:05 matthewbordas

Any updates on this? This is a very perplexing issue to still be running into 5+ years after it was reported and this behavior is common for other tools.

@matthewbordas does the workaround in https://github.com/prettier/prettier/issues/4081#issuecomment-1502020203 help?

andrewdbond avatar May 17 '23 05:05 andrewdbond

@andrewdbond adding another package and making a config change would not be considered a workaround for many I suppose. I copy paste in monorepo packages when I setup thinking this is a senseless situation to exist in the first place.

bogacg avatar May 17 '23 07:05 bogacg

@andrewdbond @bogacg It also doesn't handle auto-formatting by editors like vscode

sscotth avatar May 17 '23 11:05 sscotth

Workaround:

  1. Install find-up-cli (https://www.npmjs.com/package/find-up-cli)
  2. Use the prettier argument --ignore-path $(find-up .prettierignore)

To enable this in npm scripts on Windows, run npm config set script-shell pwsh (uses PowerShell instead of cmd to allow the substitution).

find-up-cli, by the prolific @sindresorhus, finds a file by walking up parent directories.

If your .prettierignore contains paths, which are relative, then this doesn't work. Prettier doesn't consider the location of the ignore file.

niklasholm avatar Sep 05 '23 03:09 niklasholm