Skip to content

[RFC] Implement cosmiconfig for workspace configuration - #2434

Merged
azz merged 9 commits into
prettier:masterfrom
azz:cosmiconfig
Jul 10, 2017
Merged

azz merged 9 commits into
prettier:masterfrom
azz:cosmiconfig

Conversation

@azz

@azz azz commented Jul 8, 2017 •

Copy link
Copy Markdown
Member

This RFC PR uses cosmiconfig to support configuration in the following formats:

  • .prettierrc (in JSON or YAML format)
  • prettier.config.js exporting an object
  • package.json's "prettier" key.

Configuration is resolved from the file being formatted, up the tree.

The order of precedence (lowest to highest) is:

  • Prettier defaults
  • Options from a config file
  • CLI options

Addresses most of Closes #918 (minus the JSON RPC)
Similar to #1086
Closes #62

@vjeux

vjeux commented Jul 8, 2017

Copy link
Copy Markdown
Contributor

Could you do some small performance tests to see what the overhead is?

@vjeux

vjeux commented Jul 8, 2017

Copy link
Copy Markdown
Contributor

For json-rpc, I found another way to do it so it's not needed anymore as part of prettier.

@CiGit

CiGit commented Jul 8, 2017

Copy link
Copy Markdown
Member

The question which came up some times was:
Is it possible to have different settings per language? (In an editor integration at least)
If I remember correctly @azz asked it :-)

@rattrayalex

Copy link
Copy Markdown
Collaborator

I have to say, .prettierrc doesn't sound very good. Does cosmiconfig support different filenames (eg; .prettier)?

Supporting a dynamic prettier.config.js also seems like overkill for prettier, for which settings presumably do not change between commits (unlike, say, webpack).

What are the advantages to this approach over #1086 ?

@vjeux

vjeux commented Jul 8, 2017 •

Copy link
Copy Markdown
Contributor

In order to find the files, we should do the queries in parallel. From the current path /a/b/c/, we can search for /a/b/c/.prettier, /a/b/.prettier, /a/.prettier and /.prettier in parallel so that we don't need to do a serial search.

Then, for the node version, we should watch all those files so that we don't need to do all those stats for every single format.

I'm not sure if cosmic does it, but if it doesn't, probably want to either roll our own or contribute those to the project.

@josephfrazier

Copy link
Copy Markdown
Collaborator

@CiGit:

Is it possible to have different settings per language? (In an editor integration at least)

I don't think there's been a specification proposed for this, but we could always extend the configuration format later if needed. Until then, you could work around it by keeping files with different languages in their own directories, with corresponding configuration files (I realize this isn't great).


@rattrayalex:

I have to say, .prettierrc doesn't sound very good.

Hmm, but .*rc is the convention for files like this (.eslintrc, .npmrc, .yarnrc, etc), so I think we should stick to it. It looks like cosmiconfig supports a --config flag, so you could pass a different filename in if you'd like.


@vjeux:

Then, for the node version, we should watch all those files so that we don't need to do all those stats for every single format.

Wouldn't this require implementing a client/server architecture? I think a good first step is to simply make it possible to do one-off formatting that uses configuration files without having to address the client/server issue, as this PR or #1086 would do. If performance is a concern, we could hide the config-reading behind a flag, so that users without config files don't have to wait for the unnecessary IO.

For json-rpc, I found another way to do it so it's not needed anymore as part of prettier.

I'm curious, what are you doing instead?

@vjeux

vjeux commented Jul 8, 2017

Copy link
Copy Markdown
Contributor

I'm just copying this file in the repo :p So it doesn't have to be within prettier anymore.
https://github.com/prettier/prettier-rpc/blob/master/prettier-rpc.js

@josephfrazier

Copy link
Copy Markdown
Collaborator

Oh yeah, that project :) I remember trying to figure out how to use it, but I couldn't quite crack it. See also prettier/prettier-rpc#1

@vjeux

vjeux commented Jul 8, 2017

Copy link
Copy Markdown
Contributor

The biggest reason I have waited so long to get configuration is performance and correctness.

There are many "easy" ways to deal with it:

  • Always do the search
  • Do it only once at the beginning
  • Push it to all the integrations which can ask the user to reload when it changes
  • Add a "caching" layer

All of them kind of work but are super confusing when someone upgrades prettier/config to your repo. This is probably not a huge deal for prettier itself because you're going to do it very infrequently, but at Facebook, this kind of issues is the most annoying for developers and the hardest to solve for people working on implementations.

If we add a way to configure prettier, we need to do it right. If you don't upfront, then it's never going to be seen as high pri enough and never going to be done. Also, based on which approach you take, it may be harder to do later.

We need two properties:

  • Performance: be close to a no-op the second time you call format()
  • Correctness: if you add/remove/update the config file, during the next format() call, it must be taken into account.

@CiGit

CiGit commented Jul 8, 2017 •

Copy link
Copy Markdown
Member

I should have given some use case our users (prettier-vscode) and by extension prettier's users. They mostly concerne trailing comma

    • build files (webpack, gulp, ...) at the root, JS files no trailing comma (node < 8)
    • Typescript project in a src folder.
    • Same as 1 but build files in a script folder.
    • 1 or 2
    • Mixed source files between JS / Typescript (project being migrated to typescript)

@josephfrazier 2 would be covered by the example you gave. For 1, is there any cascading rule ? Does it take into account only the first encountered config file?

3 is the most problematic here.
I've an idea to implement it in vscode, but I would prefer to have those configurations handled by prettier itself to avoid having different implementations across editors which is a bad thing to me. I would expect all of them to work the same way without having to configure your editor first.

@josephfrazier

Copy link
Copy Markdown
Collaborator

@CiGit:

For 1, is there any cascading rule ? Those it take into account only the first encountered config file?

Hmm, it looks like cosmiconfig explicitly does not merge configuration files together. See https://github.com/davidtheclark/cosmiconfig/tree/334065d9f7b557170a38b3f920bf5e90cecfab72#differences-from-rc:

  • Stops at the first configuration found, instead of finding all that can be found down the filetree and merging them automatically.

One way to fix this might be to switch from cosmiconfig to config-attendant, which appears to merge configurations together: https://github.com/GarthDB/config-attendant/blob/f435d03b32aab568b336e1b47942bcfefea2fc0f/src/lib/index.js#L104-L110

  1. Mixed source files between JS / Typescript (project being migrated to typescript)

Hmm, if you want to be able to specify different configurations for *.js and *.ts files in the same directory, I think you could use eslint with eslint-plugin-prettier, as it looks like eslint just recently merged support for glob-based configuration: eslint/eslint@a21dd32

Otherwise, we'd have to come up with a spec for supporting this in package.json/.prettierrc/etc.


(forgot this earlier) @rattrayalex:

What are the advantages to this approach over #1086 ?

For one, it supports configuration in files other than package.json. This has been suggested a few times:

@CiGit

CiGit commented Jul 8, 2017

Copy link
Copy Markdown
Member

The merging thing was not that important I think. There are not that much options :-)
May be worth exploring eslint's glob config. Even if typescript and eslint work pretty badly together (At least last time I tried something with both)

@lydell

lydell commented Jul 8, 2017

Copy link
Copy Markdown
Member

looks like eslint just recently merged support for glob-based configuration

Just as a quick aside, Prettier even uses that glob-based configuration.

@rattrayalex

rattrayalex commented Jul 8, 2017 •

Copy link
Copy Markdown
Collaborator

I'm 👍 to not-merging, and just using the closest config. More explicit and I think would play better with large, nested repos.

I made a proposal regarding perf in the wrong thread, which I will copy here....

@rattrayalex

rattrayalex commented Jul 8, 2017 •

Copy link
Copy Markdown
Collaborator

(copied from #918 (comment) )

Alright, so regarding perf of finding the config file...

I think there are two main uses of prettier (correct me if I'm wrong!): from the project root, eg in CI/precommit/manually/etc across a whole directory, or via an editor integration while writing. Obviously, the I/O overhead in the first case is probably-usually-fine, as long as it's done once per run instead of once per child file. The perf savings of finding the config file are only important (I take it) for the editor integrations.

In that case, for editor integrations, I propose we just add a --config-file option, which takes a filepath, and when it is provided, use it without checking for others. Editors can then be responsible for caching/providing it.

It would then presumably also be helpful to expose prettier find-config FILEPATH or similar, so that editors don't have to find the config files themselves (and thus keeping it consistent across editors).

I think this will address the perf concerns while keeping the code within prettier quite simple (eg; we can just use cosmiconfig or whatever).

EDIT: regarding correctness, since there is still a single file-read every time (just no crawling), it should always be correct.

Thoughts?

@josephfrazier

josephfrazier commented Jul 8, 2017 •

Copy link
Copy Markdown
Collaborator

@vjeux:

Could you do some small performance tests to see what the overhead is?

Out of curiousity, I decided to go ahead and do this upon prettier's own codebase on both master (commit 0fb332b) and this PR (commit 53c3e38) by running:

perf stat -r 10 -d ./bin/prettier.js --write src/*

On master, I got this result:

   1.274921339 seconds time elapsed                                          ( +-  0.43% )

Whereas with this PR, I got the following:

   1.348827530 seconds time elapsed                                          ( +-  0.80% )

Which represents a roughly 5% increase in runtime over the 271655 bytes of code across 25 files in src/. This could probably be optimized by only loading the configuration once per directory, rather than for each file.


(Edited to add) @rattrayalex:

I think there are two main uses of prettier (correct me if I'm wrong!): from the project root, eg in CI/precommit/manually/etc across a whole directory, or via an editor integration while writing. Obviously, the I/O overhead in the first case is probably-usually-fine, as long as it's done once per run instead of once per child file. The perf savings of finding the config file are only important (I take it) for the editor integrations.

That's a good point. For what it's worth, if you're already using eslint in conjunction with prettier, you can add eslint_d into the mix to get configuration caching without having to wait on prettier to implement it. There's some more discussion starting here: #918 (comment)

If you're not using eslint, or find eslint_d to be too slow, you could also try prettier_d, which is focused on prettier rather than eslint.

@rattrayalex

rattrayalex commented Jul 8, 2017 •

Copy link
Copy Markdown
Collaborator

@josephfrazier looks like prettier_d is yours (it looks great!). Would you be willing to:

  1. Adopt whatever config-file-finding/parsing protocol is adopted here
  2. Transfer the package to the prettier org

?

Not sure what @vjeux thinks of it.

EDIT: If both of those are true, then I might suggest we "just not worry about it" when it comes to perf, and say that editors should use prettier_d, prettier-rpc, or equivalent – and just be sure to provide a way to not run the config-file-search when it isn't needed.

@josephfrazier

josephfrazier commented Jul 8, 2017 •

Copy link
Copy Markdown
Collaborator

Thanks @rattrayalex! I've been meaning rewrite prettier_d as a fork of prettier, in order to make it easier to keep up with changes (new options, etc), as well as to fix the current limitations of prettier_d. For reference, here are some relevant issues:

This would make it easier to unify the configuration handling as well. If @vjeux, etc. are on board, I'd be willing to consider moving it to the prettier organization once the rewrite is done (or before, if that's ok). Note that prettier_d is already listed in the README under Related Projects (and I just edited it to mention the configuration support: 1e5ae50), so hopefully it's still somewhat discoverable.

EDIT: Maybe we should move prettier_d discussion to an issue on its repo, to avoid derailing this one much more.

@azz

azz commented Jul 9, 2017 •

Copy link
Copy Markdown
Member Author

How do we feel about extending the configuration format to include extname support:

trailingComma: es5

.ts:
  trailingComma: all
{
  "trailingComma": "es5",
  ".ts": {
    "trailingComma": "all"
  }
}

And a method to the API:

prettier.resolveConfig(file, { useCache }): Promise<PrettierOptions>

Returns a promise resolving to an object to be passed to prettier.format().

  • When file is omitted, uses the current working directory.
  • useCache defaults to true.

cc. @CiGit, @robwise


As a side note I am a fan of not inheriting from config up the file system tree, especially with how few options prettier has.

@robwise

robwise commented Jul 9, 2017

Copy link
Copy Markdown

I'm slightly confused here. Are we saying prettier is going to automatically get its own options when I try to run it or is it going to have a resolveOptions method that lets me get them first, modify them if I choose, and then make another call to prettier, passing in the options, to actually format?

The reason I ask is that when developing prettier-atom, I have often had to override certain settings in special cases. For example, sometimes users choose to have their line length or tabs-vs-spaces preference inferred from Atom's setting for the given scope they are in. If I can't interject these settings in the case that these rules cannot be inferred from a config, then the prettier-atom package is going to lose a lot of that functionality.

@azz

azz commented Jul 9, 2017 •

Copy link
Copy Markdown
Member Author

@robwise So the flow would be:

let options = await prettier.resolveConfig(fileBeingSaved); // traverse filesystem to find .prettierrc, etc
options = applyPrettierAtomOverrides(options); // add special case options as you mentioned
const output = prettier.format(read(fileBeingSaved), options);

@azz

azz commented Jul 9, 2017 •

Copy link
Copy Markdown
Member Author

@rattrayalex

I think there are two main uses of prettier (correct me if I'm wrong!): from the project root, eg in CI/precommit/manually/etc across a whole directory, or via an editor integration while writing. Obviously, the I/O overhead in the first case is probably-usually-fine, as long as it's done once per run instead of once per child file. The perf savings of finding the config file are only important (I take it) for the editor integrations.

It is not once-per-run, as we want to be able to run prettier on a glob:

packages/
  foo/
    .prettierrc
    index.js
  bar/
    .prettierrc
    index.js

prettier --write 'package/**/*.js' and have that work as a expected.

In that case, for editor integrations, I propose we just add a --config-file option, which takes a filepath, and when it is provided, use it without checking for others. Editors can then be responsible for caching/providing it.

Cosmiconfig supports a --config CLI option out of the box! 🎉

It would then presumably also be helpful to expose prettier find-config FILEPATH or similar, so that editors don't have to find the config files themselves (and thus keeping it consistent across editors).

Done, as an API, see above, I can also add a CLI version: prettier --resolve-config <filepath>.

@robwise

robwise commented Jul 9, 2017

Copy link
Copy Markdown

@azz Oh okay awesome, that sounds good to me!

@azz

azz commented Jul 9, 2017 •

Copy link
Copy Markdown
Member Author

Added:

  • --resolve-config <path> CLI option
    Prints the path to the resolved configuration file, relative to the current working directory. If no file is found, nothing is written and the exit code is 1.

  • --config <config-file> CLI option
    Shortcuts the config resolution logic.

So an editor integration using the CLI can do:

# cache $config_file
config_file=$(prettier --resolve-config "$input_file")
prettier --config "$config_file" --write "$input_file"

If using the API, #2434 (comment) applies instead.

@rattrayalex

Copy link
Copy Markdown
Collaborator

Cool! I think we should wait for @vjeux but this all looks good to me!

@CiGit

CiGit commented Jul 11, 2017

Copy link
Copy Markdown
Member

Will take a look at it. I didn't know SchemaStore :-)
Would it be eventually bundled with prettier?
I could also provide a typescript .d.ts file if this is usefull.

@azz

azz commented Jul 11, 2017

Copy link
Copy Markdown
Member Author

How would you usually go about consuming the schema?

@CiGit

CiGit commented Jul 11, 2017 •

Copy link
Copy Markdown
Member

I thought I would provide it with the extension and validate those files. I've not explored this yet. It seems to be possible to provide an URL.

Edit: found it https://code.visualstudio.com/docs/extensionAPI/extension-points#_contributesjsonvalidation URL is fine :-)

@azz

azz commented Jul 11, 2017

Copy link
Copy Markdown
Member Author

@azz azz mentioned this pull request Jul 11, 2017
yangchristian added a commit to CrossLead/crosslytics that referenced this pull request Jul 12, 2017
- Fix current errors
- Add singleQuote rule to prettier
- TODO: Remove CLI flag once there is a release with prettier/prettier#2434
@gaui

gaui commented Jul 20, 2017

Copy link
Copy Markdown

When will this be released? 👍

@danawoodman

Copy link
Copy Markdown

@vjeux is there a release scheduled that would include this change?

@vjeux

vjeux commented Aug 3, 2017

Copy link
Copy Markdown
Contributor

@gaui @danawoodman Sorry about that. I switched team at Facebook and took a lot of vacation in the past month, so didn't have a lot of time to work on prettier. I just opened an issue to start tracking the work needed to get this out to the world: #2589

@danawoodman

Copy link
Copy Markdown

Thanks @vjeux!

@tunnckoCore tunnckoCore mentioned this pull request Aug 5, 2017
4 of 6 tasks
MoOx added a commit to MoOx/react-native-web that referenced this pull request Jan 8, 2018
This allow IDE plugins that rely on prettier config (introduced in [1.6.0](prettier/prettier#2434)) to detect prettier and run it automatically with the correct config!
necolas pushed a commit to necolas/react-native-web that referenced this pull request Jan 8, 2018
This allows IDE plugins that rely on prettier config (introduced in
[1.6.0](prettier/prettier#2434)) to detect
prettier and run it automatically with the correct config.

Close #753
necolas pushed a commit to necolas/react-native-web that referenced this pull request Jan 8, 2018
This allows IDE plugins that rely on prettier config (introduced in
[1.6.0](prettier/prettier#2434)) to detect
prettier and run it automatically with the correct config.

Close #757
@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] Editor Integration Configuration file support

10 participants