prettier-vscode
prettier-vscode copied to clipboard
Can't find `.prettierrc` file when formatting `settings.json` on Windows.
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:
- Save
.prettierrc.json
noted above toC:\Users\[username]\.prettierrc.json
- Open VS Code user
settings.json
(Located inC:\Users\[username]\AppData\Roaming\Code\User\settings.json
) - 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.
This is still broken in v9.1.0. I was able to use git bisect
to determine that the breaking change occurs somewhere in c75c9d3.
I think I've narrowed this issue down to somewhere in the ModuleResolver.getResolvedConfig()
method. Some possibilities include:
-
Where
resolveConfigOptions
does not search for a config file in the user's home directory: https://github.com/prettier/prettier-vscode/blob/6badc7488b0948a459834fedc3e971d02fed5ffc/src/ModuleResolver.ts#L228-L235 -
Where Prettier itself fails to detect the config file in the user's home directory. This seems unlikely, but
configPath
isundefined
if I am in a project without a.prettierrc
file, so it's possible. https://github.com/prettier/prettier-vscode/blob/6badc7488b0948a459834fedc3e971d02fed5ffc/src/ModuleResolver.ts#L217
Another potential issue is that Prettier does not watch for changes to the .prettierrc
file in the user's home directory.
https://github.com/prettier/prettier-vscode/blob/9a17a7055943669cfc9040ea2b8b60362b1d71d3/src/PrettierEditService.ts#L89-L94
Can anyone comment on this? I realize this bug likely does not affect many people, but it makes the latest versions of this extension unusable for me and I would appreciate at least some recognition in trying to resolve this issue.
Looks like you are on track. Want to send a PR to fix it?
Well, I totally forgot about this for 4 months. Oops.
I think I've figured it out. If we take a look at the log file, we can see that the user (not project) settings.json
file has a vscode-userdata
URI scheme, not a file
scheme.
Prettier log
["INFO" - 6:12:43 PM] Extension Name: esbenp.prettier-vscode. ["INFO" - 6:12:43 PM] Extension Version: 9.5.0.["INFO" - 6:16:42 PM] Formatting vscode-userdata:/c%3A/Users/Andrew/AppData/Roaming/Code/User/settings.json ["INFO" - 6:16:42 PM] File Info: { "ignored": false, "inferredParser": "json5" } ["INFO" - 6:16:42 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration ["INFO" - 6:16:42 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:16:42 PM] Formatting completed in 0.043ms.
["INFO" - 6:17:25 PM] Formatting file:///c%3A/Users/Andrew/AppData/Roaming/Code/User/settings-sample.json ["INFO" - 6:17:25 PM] Using config file at 'c:\Users\Andrew.prettierrc.json' ["INFO" - 6:17:25 PM] File Info: { "ignored": false, "inferredParser": "json5" } ["INFO" - 6:17:25 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used ["INFO" - 6:17:25 PM] Prettier Options: { "filepath": "c:\Users\Andrew\AppData\Roaming\Code\User\settings-sample.json", "parser": "json5", "tabWidth": 4, "quoteProps": "preserve", "singleQuote": false, "trailingComma": "all" } ["INFO" - 6:17:25 PM] Formatting completed in 0.021ms.
This is an issue because on line 260 of ModuleResolver, the extension determines whether Prettier should attempt to resolve a config from the URI based on if the URI has a file
scheme.
https://github.com/prettier/prettier-vscode/blob/30c32808de8c159fa108e5521eb7b58fe0941724/src/ModuleResolver.ts#L256-L266
I propose the following fix (which I have confirmed works) by changing line 260:
- const isVirtual = uri.scheme !== "file";
+ const isVirtual = uri.scheme !== "file" && uri.scheme !== "vscode-userdata";
My only concern here is that I do now know what other files are considered vscode-userdata
; I haven't been able to find any documentation about it. I'm not sure if making this change would have negative effects on other internal files. Thoughts?
If this change looks good, I can go ahead and submit a PR.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.