Skip to content

Can't find .prettierrc file when formatting settings.json on Windows. #2226

@Antyos

Description

@Antyos

Summary

Formatting the user settings.json on Windows with Prettier does not detect the .prettierrc.json file correctly. If there are overrides specified in the .prettierrc.json file located in the home directory, Prettier ignores all settings specified in the override except for parser.

Github Repository to Reproduce Issue

As the two files in question need to be placed in specific locations on the system, I have opted to include them as text below.

Sample .prettierrc.json
{
    "tabWidth": 4,
    "overrides": [
        {
            "files": [
                "foo.json",
                "**/.vscode/*.json",
                "**/tsconfig.json",
                "**/tsconfig.*.json",
                "**/AppData/Roaming/Code/User/*.json"
            ],
            "options": {
                "tabWidth": 4,
                "parser": "json5",
                "quoteProps": "preserve",
                "singleQuote": false,
                "trailingComma": "all"
            }
        }
    ]
}
Sample settings.json
{
    "markdownlint.config": {
        "MD007": {
            "indent": 4,
        },
        "MD024": false,
        "MD033": {
            "allowed_elements": ["kbd", "br", "details", "summary"]
        }
    }
}

Steps To Reproduce:

  1. Save .prettierrc.json noted above to C:\Users\[username]\.prettierrc.json
  2. Open VS Code user settings.json (Located in C:\Users\[username]\AppData\Roaming\Code\User\settings.json)
  3. Run Format Document with Prettier.

Expected result

The sample settings.json should be formatted as follows:

{
    "markdownlint.config": {
        "MD007": {
            "indent": 4,
        },
        "MD024": false,
        "MD033": {
            "allowed_elements": ["kbd", "br", "details", "summary"],
        },
    },
}

Actual result

Actual settings.json:

{
    "markdownlint.config": {
        MD007: {
            "indent": 4,
        },
        MD024: false,
        MD033: {
            allowed_elements: ["kbd", "br", "details", "summary"],
        },
    },
}

Additional information

Looking through the logs, it initially appears that the .prettierrc.json is not recognized when formatting the user settings.json file, however, that is not entirely the case.
The parser property is overriden by the .prettierrc.json, other properties, such as quoteProps and trailingComma are not recognized.

I have tried the formatting test under various conditions. Here are the logs for some things I tried that formatted as expected.

Renaming the sample settings.json to foo.json, placing it in the current project folder, and formatting it.
["INFO" - 6:12:04 PM] Extension Name: esbenp.prettier-vscode.
["INFO" - 6:12:04 PM] Extension Version: 9.0.0.
["INFO" - 6:12:21 PM] Formatting file:///c%3A/Users/Andrew/Documents/Sandbox/foo.json
["INFO" - 6:12:21 PM] Using config file at 'c:\Users\Andrew\.prettierrc.json'
["INFO" - 6:12:21 PM] Using ignore file (if present) at c:\Users\Andrew\Documents\Sandbox\.prettierignore
["INFO" - 6:12:21 PM] File Info:
{
  "ignored": false,
  "inferredParser": "json5"
}
["INFO" - 6:12:21 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 6:12:21 PM] Prettier Options:
{
  "filepath": "c:\\Users\\Andrew\\Documents\\Sandbox\\foo.json",
  "parser": "json5",
  "tabWidth": 4,
  "quoteProps": "preserve",
  "singleQuote": false,
  "trailingComma": "all"
}
["INFO" - 6:12:21 PM] Formatting completed in 0.046ms.
Formatting the user settings.json after removing the line "**/AppData/Roaming/Code/User/*.json" from the .prettierrc.json, causing the default formatting to take over.
["INFO" - 6:14:47 PM] Formatting vscode-userdata:/c%3A/Users/Andrew/AppData/Roaming/Code/User/settings.json
["INFO" - 6:14:47 PM] File Info:
{
  "ignored": false,
  "inferredParser": "json5"
}
["INFO" - 6:14:47 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 6:14:47 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 4,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "c:\\Users\\Andrew\\AppData\\Roaming\\Code\\User\\settings.json",
  "parser": "json5"
}
["INFO" - 6:14:47 PM] Formatting completed in 0.036ms.

VS Code Version:

Version: 1.60.2 (user setup)
Commit: 7f6ab5485bbc008386c4386d08766667e155244e
Date: 2021-09-22T12:00:31.514Z
Electron: 13.1.8
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Windows_NT x64 10.0.19042

Prettier Extension Version: 9.0.0

IMPORTANT ❗: This bug is not present in version 8.2.0 of the Prettier extension.

OS and version: Windows 10, build 20H2.

Prettier Log Output

["INFO" - 6:07:55 PM] Extension Name: esbenp.prettier-vscode.
["INFO" - 6:07:55 PM] Extension Version: 9.0.0.
["DEBUG" - 6:07:55 PM] Enabling Prettier globally
{
  "languageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "mongo"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "jsonc"
    },
    {
      "language": "json5"
    },
    {
      "language": "css"
    },
    {
      "language": "postcss"
    },
    {
      "language": "less"
    },
    {
      "language": "scss"
    },
    {
      "language": "handlebars"
    },
    {
      "language": "graphql"
    },
    {
      "language": "markdown"
    },
    {
      "language": "mdx"
    },
    {
      "language": "html"
    },
    {
      "language": "vue"
    },
    {
      "language": "yaml"
    },
    {
      "language": "ansible"
    },
    {
      "language": "home-assistant"
    },
    {
      "language": "jsonc",
      "scheme": "vscode-userdata"
    }
  ],
  "rangeLanguageSelector": [
    {
      "language": "javascript"
    },
    {
      "language": "javascriptreact"
    },
    {
      "language": "typescript"
    },
    {
      "language": "typescriptreact"
    },
    {
      "language": "json"
    },
    {
      "language": "graphql"
    }
  ]
}
["INFO" - 6:09:08 PM] Formatting vscode-userdata:/c%3A/Users/Andrew/AppData/Roaming/Code/User/settings.json
["DEBUG" - 6:09:08 PM] Using bundled version of prettier.
["INFO" - 6:09:08 PM] File Info:
{
  "ignored": false,
  "inferredParser": "json5"
}
["INFO" - 6:09:08 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 6:09:08 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 4,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "c:\\Users\\Andrew\\AppData\\Roaming\\Code\\User\\settings.json",
  "parser": "json5"
}
["INFO" - 6:09:08 PM] Formatting completed in 0.055ms.

Note that json5 is not the default parser used for the settings.json file; Prettier is getting that from the .prettierrc.json file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglockedLocked due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions