Repository navigation
[RFC] Implement cosmiconfig for workspace configuration - #2434
Conversation
|
Could you do some small performance tests to see what the overhead is? |
|
For json-rpc, I found another way to do it so it's not needed anymore as part of prettier. |
|
The question which came up some times was: |
|
I have to say, Supporting a dynamic What are the advantages to this approach over #1086 ? |
|
In order to find the files, we should do the queries in parallel. From the current path /a/b/c/, we can search for 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. |
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).
Hmm, but
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.
I'm curious, what are you doing instead? |
|
I'm just copying this file in the repo :p So it doesn't have to be within prettier anymore. |
|
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 |
|
The biggest reason I have waited so long to get configuration is performance and correctness. There are many "easy" ways to deal with it:
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:
|
|
I should have given some use case our users (prettier-vscode) and by extension prettier's users. They mostly concerne trailing comma
@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. |
Hmm, it looks like cosmiconfig explicitly does not merge configuration files together. See https://github.com/davidtheclark/cosmiconfig/tree/334065d9f7b557170a38b3f920bf5e90cecfab72#differences-from-rc:
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
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:
For one, it supports configuration in files other than package.json. This has been suggested a few times: |
|
The merging thing was not that important I think. There are not that much options :-) |
Just as a quick aside, Prettier even uses that glob-based configuration. |
|
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.... |
|
(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? |
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: On master, I got this result: Whereas with this PR, I got the following: 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:
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. |
|
@josephfrazier looks like
? 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 |
|
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. |
|
How do we feel about extending the configuration format to include trailingComma: es5
.ts:
trailingComma: all{
"trailingComma": "es5",
".ts": {
"trailingComma": "all"
}
}And a method to the API:
|
|
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 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. |
|
@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); |
It is not once-per-run, as we want to be able to run prettier on a glob:
Cosmiconfig supports a
Done, as an API, see above, I can also add a CLI version: |
|
@azz Oh okay awesome, that sounds good to me! |
|
Added:
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. |
|
Cool! I think we should wait for @vjeux but this all looks good to me! |
|
Will take a look at it. I didn't know SchemaStore :-) |
|
How would you usually go about consuming the schema? |
|
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 :-) |
- Fix current errors - Add singleQuote rule to prettier - TODO: Remove CLI flag once there is a release with prettier/prettier#2434
|
When will this be released? 👍 |
|
@vjeux is there a release scheduled that would include this change? |
|
@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 |
|
Thanks @vjeux! |
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!
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
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
This RFC PR uses cosmiconfig to support configuration in the following formats:
.prettierrc(in JSON or YAML format)prettier.config.jsexporting an objectpackage.json's"prettier"key.Configuration is resolved from the file being formatted, up the tree.
The order of precedence (lowest to highest) is:
Addresses most ofCloses #918(minus the JSON RPC)Similar to #1086
Closes #62