prettier-cli icon indicating copy to clipboard operation
prettier-cli copied to clipboard

Support Configuration Overrides for the experimental cli.

Open Standard8 opened this issue 6 months ago • 23 comments

I've been trying out the new experimental CLI, and it is indeed seeming to be a lot faster.

Something I've noticed is that it appears there's no current support for Configuration Overrides. I suspect it just hasn't been added yet, which is fine, but as I couldn't see an existing issue for it, I thought I'd file one for helping with tracking.

STR

  1. Set up a repo with a .prettierrc.js such as the one below:
module.exports = {
  arrowParens: "avoid",
  endOfLine: "lf",
  printWidth: 80,
  tabWidth: 2,
  trailingComma: "es5",
  overrides: [
    {
      files: "*.css",
      options: {
        parser: "css",
        // Using a larger printWidth to avoid wrapping selectors.
        printWidth: 160,
      },
    },
  ],
};
  1. Run the formatter

Expected Results

The CSS files are formatted correctly, with line width 160.

Actual Results

The CSS files are formatted with line width 80.

Standard8 avatar Jun 23 '25 10:06 Standard8

Can you try

module.exports = {
  arrowParens: "avoid",
  endOfLine: "lf",
  printWidth: 80,
  tabWidth: 2,
  trailingComma: "es5",
  overrides: [
    {
-      files: "*.css",
+      files: "**/*.css",
      options: {
        parser: "css",
        // Using a larger printWidth to avoid wrapping selectors.
        printWidth: 160,
      },
    },
  ],
};

I think it should work.

fisker avatar Jun 23 '25 10:06 fisker

@43081j I thought this was fixed? Related PR https://github.com/prettier/prettier-cli/pull/34

fisker avatar Jun 23 '25 10:06 fisker

Using files: "**/*.css", does work. Thank you.

Standard8 avatar Jun 23 '25 11:06 Standard8

I have the same issue, my overrides are not working. Here's my config file:

{
  "printWidth": 80,
  "tabWidth": 2,
  "useTabs": false,
  "semi": true,
  "singleQuote": true,
  "quoteProps": "as-needed",
  "jsxSingleQuote": false,
  "trailingComma": "none",
  "bracketSpacing": true,
  "bracketSameLine": false,
  "arrowParens": "always",
  "proseWrap": "preserve",
  "endOfLine": "auto",
  "singleAttributePerLine": true,
  "plugins": ["@ianvs/prettier-plugin-sort-imports"],
  "importOrder": [
    "",
    "<BUILTIN_MODULES>",
    "",
    "<THIRD_PARTY_MODULES>",
    "",
    "^[.]"
  ],
  "importOrderParserPlugins": ["typescript", "jsx", "decorators-legacy"],
  "importOrderTypeScriptVersion": "5.8.0",
  "overrides": [
    {
      "files": ["**/*.scss", "**/*.css"],
      "options": {
        "printWidth": 120
      }
    }
  ]
}

With the experimental cli, my css files are formated with a 80 printWidth

Abreuvoir avatar Jun 23 '25 11:06 Abreuvoir

@fisker I think we have a TODO left about prefixing globs with **/ automatically unless they start with ./, assuming that's what the old CLI does, that hasn't been implemented yet. That said @Abreuvoir's config doesn't seem to be affected by that problem, so there might be another bug 🤔 I'll try to reproduce it once I get back home.

fabiospampinato avatar Jun 23 '25 12:06 fabiospampinato

Sorry for going a bit off-topic, but I also had an issue with the @ianvs/prettier-plugin-sort-imports plugin's importOrder mentioned in @Abreuvoir's config, so there may be a compatibility issue between the new CLI and that plugin (it seemed like the importOrder property wasn't read correctly, or possibly at all, with the new CLI).

Mathias-S avatar Jun 23 '25 12:06 Mathias-S

@Mathias-S Interestingly, thanks, I'll have a look 👍

fabiospampinato avatar Jun 23 '25 13:06 fabiospampinato

@Mathias-S @fabiospampinato It doesn't seem related to @ianvs/prettier-plugin-sort-imports at all. For me, even with experimental CLI turned off, and with an option like singleQuote: false it still doesn't pick up on the overrides. Even a glob for all files still doesn't seem to match anything: - files: '**/*'

oprypkhantc avatar Jun 23 '25 16:06 oprypkhantc

Are you using Prettier 3.6.0? That's the version that was working for me with files: "**/*.css",.

Standard8 avatar Jun 23 '25 16:06 Standard8

it seemed like the importOrder property wasn't read correctly, or possibly at all, with the new CLI

This should probably be its own issue. overrides currently only pass on normalized configuration directives, no plugin options: https://github.com/prettier/prettier-cli/blob/4b1a66a1b8d7091ba53a37b4762b7863eaf09585/src/utils.ts#L659-L660 This is in contrast to the top-level configuration object which keeps everything (therefore also unknown plugin options) and only normalizes known directives: https://github.com/prettier/prettier-cli/blob/4b1a66a1b8d7091ba53a37b4762b7863eaf09585/src/config_prettier.ts#L94 https://github.com/prettier/prettier-cli/blob/4b1a66a1b8d7091ba53a37b4762b7863eaf09585/src/utils.ts#L640-L643

tryone144 avatar Jun 23 '25 17:06 tryone144

On 3.6.0, with config:

{
	"arrowParens": "avoid",
	"endOfLine": "lf",
	"trailingComma": "all",
	"printWidth": 120,
	"singleQuote": true,
	"tabWidth": 4,
	"useTabs": true,
	"overrides": [
		{
			"files": ".prettierrc",
			"options": { "parser": "json" }
		},
		{
			"files": "**/*.md",
			"options": { "tabWidth": 2 }
		}
	]
}

Using --experimental-cli doesn't honor the markdown tabWidth.

I originally had "files": "*.md", and it didn't work, so I tried "files": "**/*.md", and don't see any difference

eamodio avatar Jun 23 '25 18:06 eamodio

A shot in the dark, but could be Windows paths be the common denominator? In contrast to the cases where adding **/ to the glob has fixed the issue.

EDIT: Can't reproduce the issue of **/-prefix not working on windows…

tryone144 avatar Jun 23 '25 23:06 tryone144

No, I have the issue in a Linux Docker container. I see the same issue as @eamodio, where even standard Prettier options are not being respected.

oprypkhantc avatar Jun 24 '25 14:06 oprypkhantc

does one of you experiencing exactly @eamodio's problem have a repo where it can be reproduced?

43081j avatar Jun 25 '25 11:06 43081j

I can no longer reproduce this with https://github.com/prettier/prettier/releases/tag/3.6.1.

Though if I don't use "files": "**/*.md", over "files": "*.md", the behavior is different than prior prettier -- "files": "*.md", only matches .md files in the root folder, where as before it would be all .md files.

eamodio avatar Jun 25 '25 12:06 eamodio

3.6.1 did not change anything for me

Abreuvoir avatar Jun 25 '25 15:06 Abreuvoir

@43081j Here you go: https://github.com/oprypkhantc/prettier-reproduction/tree/master

It's in Docker, but you can clearly see that 3.5.x versions do respect useTabs and tabWidth options I've overwritten, and 3.6.x does not.

oprypkhantc avatar Jun 26 '25 11:06 oprypkhantc

Thanks @oprypkhantc! This issue seems to be related to --config-path. When removing that directive from the docker run commands, the options are getting picked up.

If --config-path is used, the path matching against the overwrites is never performed.


getPrettierConfigResolved(), which handles the matching against overwrites, is never called when an explicit config file has been passed via --config-path: https://github.com/prettier/prettier-cli/blob/4b1a66a1b8d7091ba53a37b4762b7863eaf09585/src/index.ts#L133

tryone144 avatar Jun 26 '25 15:06 tryone144

Using Prettier 3.6.2 with the new cli flag I have to change my overwrites like this:

  overrides: [
    {
      files:
-        'src/app/regionen/[regionSlug]/_components/SidebarInspector/TagsTable/translations/*.const.ts',
+        '**/translations/*.const.ts',
      options: {
        printWidth: 1000,
      },
    },
  ],

tordans avatar Jul 18 '25 13:07 tordans

This overrides config works differently for me (versus the classic CLI):

overrides: [
    {
       files: ['**/tsconfig.json', '**/tsconfig.*.json'],
       options: {
        parser: 'jsonc',
      },
    },
  ],

The root tsconfig.json respects the parser: jsonc, but scripts/tsconfig.json file in my repo does not respect the overrides.

files: ['tsconfig.json', 'tsconfig.*.json'] doesn't work either

kirillgroshkov avatar Jul 28 '25 08:07 kirillgroshkov

I just reported a similar issue on https://github.com/prettier/prettier/discussions/17818. I believe the reproduction I put in place could help into the investigation.

I tried to debug it, but so far I have not found my way through a fix, so I prefer asking to knowledgeable persons.

dubzzz avatar Aug 27 '25 14:08 dubzzz

@dubzzz is your specific issue that plugin options in overrides are not honoured?

Please check if https://github.com/prettier/prettier-cli/pull/73 fixes your issue.

tryone144 avatar Aug 27 '25 14:08 tryone144

@tryone144 It works perfectly well 👍

dubzzz avatar Aug 27 '25 15:08 dubzzz