Prettier-vscode: Can't format user settings.json in vscode

Created on 19 Aug 2019  路  15Comments  路  Source: prettier/prettier-vscode

Summary

When the option "editor.defaultFormatter": "esbenp.prettier-vscode" in user settings is set is set, one cannot format the settings.json file itself.
Strangely, other json files and the workspace settings file are formatted correctly.

Steps To Reproduce:

Add "editor.defaultFormatter": "esbenp.prettier-vscode" to your user settings.json file and try to format the document after saving.

Expected result

The file should be formatted according to prettier settings like other

Actual result

Following error message is shown in the status bar:
Extension 'Prettier Code formatter' cannot format '~/Library/Application Support/Code/User/settings.json'

VS Code Version:
1.37.1

Prettier & Prettier Plugin Version:
Prettier v1.18.2 && Plugin Version: 1.9.0

OS and version:
macOS 10.14.6

bug locked

Most helpful comment

The issue with the settings.json is a problem with VSCode (microsoft/vscode#79038). Essentially, VSCode doesn鈥檛 register that file as a regular JSON file and doesn鈥檛 call registered formatters. They have said they don't plan on fixing it because they now have the visual editor. The solution you found of opening the file directly is the workaround.

All 15 comments

I tried formatting it also with HookyQR.beautify (which also supports JSON formatting) and the same problem gets reproduced. So, I can surely say it's VSCode fault and not specific to this extension.

Can anyone test it now? It seems to be working right now.

Got the latest insiders build and the bug is still present. I didn't delete any folders though.

Version: 1.38.0-insider
Commit: 0f73473c08055054f317c1c94502f7f39fdbb164
Date: 2019-08-23T05:36:33.571Z
Electron: 4.2.9
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Darwin x64 18.7.0

Update: https://github.com/microsoft/vscode/issues/79038#issuecomment-526475411.
Summary: Formatting doesn't work if a folder is open.

I also have the error "Prettier cannot format file 'global' settings.json".
But I found that a similar error occurs when I try formatting *.vue files using Vetur plugin which uses Prettier internally as a formatter.

However local project files like .js or .json are formatting correctly.

Tools versions:

  • VSCode: 1.38.1
  • vscode-prettier plugin: 2.2.2
  • Vetur: 0.22.3

UPDATE:

The problem with formatting Vue files is solved by adding a new property to settings.json

{
  "[vue]": {
    "editor.defaultFormatter": "octref.vetur"
  }
}

But an error while formatting settings.json still exists.

Same thing happens for me.

  • *.vue files and global settings.json doesn't format:
    image
  • Workspace .vscode/settings.json, keybindings.json and other files (.js, .ts, .html). Even eslint/tslint integration works for me as we still use linters in v5.x.x in the project.

@dyakovk
It formats, but the formatting differs. Seems like it's not respecting the settings from .prettierrc and/or misses eslint-integration. Dunno.

@McLisak try adding a rule as in my previous updated comment. It works for me.
I also added a rule to format html blocks with Prettier inside Vue files. By default it uses pretty-html

{
  "vetur.format.defaultFormatter.html": "prettier"
}

Update:

There is my full config for working all the tools (Prettier, ESLint and Vetur ext) together

{
  "editor.formatOnSave": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "eslint.autoFixOnSave": false,
  "eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "vue"
  ],
  "vetur.format.defaultFormatter.html": "prettier",
  "[vue]": {
    "editor.defaultFormatter": "octref.vetur"
  },
  "prettier.singleQuote": true,
  "prettier.endOfLine": "lf"
}

@dyakovk Have done that right after installing vetur sir :) It was working before, respecting the rules we had set in the projects, which is important for us. I've tried a lot of configurations, yours works, but it's rather a workaround as prettier-vscode was able to format *.vue files properly.

I am using prettier for everything I can. I prefer using it integrated with eslint/tslint - which is not suggested way of doing so. Prettier suggests to disable formatting rules in linters and let him do the job. Had no troubles till now, but ESLint6 breaks the integration, so we'll have to migrate and reformat our projects with the suggested approach.

I've tried a lot of configurations, yours works, but it's rather a workaround as prettier-vscode was able to format *.vue files properly.

I suggest that it's somehow related with prettier-vscode property prettier.disableLanguages. Which by default disables the formatting of the whole Vue file by Prettier parser. As written in the Readme. But something went wrong. Something broke :(

Im not really following the conversation around the other vue issues, so please open separate issues with detailed repro steps if needed.

@ntotten ,

I'm running version 3.5.0. Definitely not fixed.

Attempting to format settings.json causes the following to appear in the VS Code status bar:

Extension 'Prettier Code Formatter' cannot format 'C:\Users\Me\AppData\Roaming\Code\Usersettings.json'

I'm running the latest VS Code on the latest Windows 10.

I'm also having the issue of not being able to format my settings.json with prettier. However, I discovered that I _am_ able to format when I open a new empty VS Code window (File > New Window), then open settings.json, then format.

I have no idea why it only works in an empty workspace. It doesn't seem to matter if a non-empty workspace has a local prettier config or not.

macOS 10.14.6
VS Code 1.40.2
prettier-vscode 3.11.0

The issue with the settings.json is a problem with VSCode (microsoft/vscode#79038). Essentially, VSCode doesn鈥檛 register that file as a regular JSON file and doesn鈥檛 call registered formatters. They have said they don't plan on fixing it because they now have the visual editor. The solution you found of opening the file directly is the workaround.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielHabenicht picture DanielHabenicht  路  4Comments

bluemoehre picture bluemoehre  路  3Comments

bardware picture bardware  路  4Comments

finalclass picture finalclass  路  4Comments

GantMan picture GantMan  路  3Comments