Prettier in VS Code uses the wrong indentation, even after I changed all the places I can think of to a width of "4".
Here are my file contents (some are maybe not necessary, but I added them while trying to fix it):
c:\Users\jp\Documents\Repositories\Game\Client\.prettierrc.js
module.exports = {
semi: true,
trailingComma: "none",
singleQuote: false,
printWidth: 120,
tabWidth: 4,
endOfLine: "auto",
trailingComma: "none"
};
c:\Users\jp\Documents\Repositories\Game\Client\.editorconfig
indent_size = 4
c:\Users\jp\Documents\Repositories\Game\Client\.eslintrc.js
module.exports = {
parser: "#typescript-eslint/parser", // Specifies the ESLint parser
parserOptions: {
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
sourceType: "module", // Allows for the use of imports
ecmaFeatures: {
jsx: true // Allows for the parsing of JSX
}
},
settings: {
react: {
version: "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
}
},
extends: [
"plugin:react/recommended", // Uses the recommended rules from #eslint-plugin-react
"plugin:#typescript-eslint/recommended", // Uses the recommended rules from the #typescript-eslint/eslint-plugin
"plugin:prettier/recommended", // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
"prettier"
],
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "#typescript-eslint/explicit-function-return-type": "off",
"no-var": "error", // preference for let and const only
"prefer-const": "error",
"react/react-in-jsx-scope": "off",
"#typescript-eslint/no-empty-function": "off",
"react/prop-types": "off",
"prettier/prettier": [
"warn",
{
semi: true,
trailingComma: "none",
singleQuote: false,
printWidth: 120,
tabWidth: 4,
endOfLine: "auto",
trailingComma: "none"
}
]
}
};
c:\Users\jp\Documents\Repositories\Game\Client\.vscode\settings.json
{
"editor.formatOnSave": true,
"editor.formatOnType": true,
"prettier.tabWidth": 4,
"editor.tabSize": 4,
"jestrunner.jestCommand": "npm run test -- --watchAll=false"
}
My VS Code configuration is set to "4,", and in the bottom bar in VS Code it's set to "4". I also set "detect indendation" to false.
Here is what the Prettier extension output says when I format the document:
["INFO" - 17:18:30] Formatting file:///c%3A/Users/jp/Documents/Repositories/Game/Client/src/App.tsx
["INFO" - 17:18:30] Using config file at 'c:\Users\jp\Documents\Repositories\Game\Client\.prettierrc.js'
["INFO" - 17:18:30] Using ignore file (if present) at c:\Users\jp\Documents\Repositories\Game\Client\.prettierignore
["INFO" - 17:18:30] File Info:
{
"ignored": false,
"inferredParser": "typescript"
}
["INFO" - 17:18:30] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 17:18:30] Prettier Options:
{
"filepath": "c:\\Users\\jp\\Documents\\Repositories\\Game\\Client\\src\\App.tsx",
"parser": "typescript",
"semi": true,
"trailingComma": "none",
"singleQuote": false,
"printWidth": 120,
"tabWidth": 3,
"endOfLine": "auto"
}
["INFO" - 17:18:30] Formatting completed in 0.027ms.
It even says it found the right configuration file, yet uses the wrong indentation.
I restarted VS Code while making the changes to make sure nothing was cached.
In the parent path no .editorconfig is present.
I just don't have any idea where Prettier may take the wrong indentation from...
Edit: When I use the "Quick Fix" with "Fix all prettier/prettier problems", it uses the correct indentation. Formatting on save or Using "Format document" uses the wrong one. Output of the extension output window is the same.
Check if your detect indentation has been enabled. Disable it.
It's not obvious at first, but prettier has order of reading custom configs and is this one according to documentation:
Prettier uses cosmiconfig for configuration file support. This means you can configure Prettier via (in order of precedence):
"prettier" key in your package.json file.
prettierrc file written in JSON or YAML.
.prettierrc.json, .prettierrc.yml, .prettierrc.yaml, or .prettierrc.json5 file.
.prettierrc.js, .prettierrc.cjs, prettier.config.js, or prettier.config.cjs file that exports an object using module.exports.
.prettierrc.toml file.
I can only assume you forgot to check configuration in package.json, and remove these settings from there.
Related
I was trying out the customizations from this post (
Change highlight text color in Visual Studio Code) but it broke my VS Code settings.json.
I followed the first answer, the highlighting now works.
But when I click on the top to bring up the "Menu Bar", I get the message:
Unable to write into user settings. Please open the user settings to correct errors/warnings in it and try again.
{
"workbench.colorTheme": "One Monokai",
"security.workspace.trust.untrustedFiles": "open",
"files.autoSave": "afterDelay",
"editor.minimap.enabled": false,
"editor.wordWrap": "on",
"window.commandCenter": false,
"workbench.layoutControl.enabled": false,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.tabSize": 2,
"editor.mouseWheelZoom": true,
"html.hover.references": false,
"css.hover.references": false,
"less.hover.references": false,
"scss.hover.references": false,
"window.menuBarVisibility": "compact",
"window.zoomLevel": 1,
"editor.tokenColorCustomizations": {
},
"workbench.colorCustomizations": {
"editor.selectionBackground": "#e788ff7c",
"editor.selectionHighlightBackground": "#ff00005b",
"editor.selectionHighlightBorder": "#fbf300e0", ##border when you select
"editor.findMatchBackground": "#f352fe8f",
"editor.findMatchHighlightBackground": "#8e52fe9e",
"editor.findMatchHighlightBorder": "#fbf300e0" ##border when you search for something
}
}
I'm not sure what should I do to fix this issue.
Comments should start with //, not with ##.
VS Code's settings.json follows JSON with Comments (jsonc) mode:
https://code.visualstudio.com/docs/languages/json#_json-with-comments
In addition to the default JSON mode following the JSON specification, VS Code also has a JSON with Comments (jsonc) mode. This mode is used for the VS Code configuration files such as settings.json, tasks.json, or launch.json. When in the JSON with Comments mode, you can use single line (//) as well as block comments (/* */) as used in JavaScript.
In addition, the editor should have shown you that something was wrong.
After reading and applying the suggestions on
How do you format code in Visual Studio Code (VSCode)
and
Why does Prettier does not format code in VS Code?
I still wasn't able to format the code on VS Code when I press Shift + Alt + F. Here's what I've tried so far:
Screenshot 1
Screenshot 2
What else should I try to make it work as expected?
UPDATE: Here's the screenshot of the HTML code I'm trying to format. I've circled some of the elements that are not being formatted.
Screenshot 3
UPDATE 2:
Here's also the output message I get from the (!) Prettier link at the bottom right of the toolbar after pressing Shift + Alt + F:
["INFO" - 21:23:59] Formatting c:\Users\Home\OneDrive\Programming\Colt Udemy Course\chickens.html
["INFO" - 21:23:59] Using ignore file (if present) at c:\Users\Home\OneDrive\Programming\Colt Udemy Course.prettierignore
["INFO" - 21:23:59] File Info:
{
"ignored": false,
"inferredParser": "html"
}
["INFO" - 21:23:59] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 21:23:59] Prettier Options:
{
"arrowParens": true,
"bracketSpacing": true,
"endOfLine": "lf",
"htmlWhitespaceSensitivity": "css",
"insertPragma": false,
"jsxBracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 120,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"requirePragma": false,
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "none",
"useTabs": true,
"vueIndentScriptAndStyle": false,
"filepath": "c:\Users\Home\OneDrive\Programming\Colt Udemy Course\chickens.html",
"parser": "html"
}
["ERROR" - 21:23:59] Error formatting document.
["ERROR" - 21:23:59] Invalid arrowParens value. Expected "always" or "avoid", but received true.
Error: Invalid arrowParens value. Expected "always" or "avoid", but received true.
at Normalizer._applyNormalization (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:8419:59)
at applyNormalization (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:8368:48)
at Normalizer.normalize (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:8374:7)
at normalizeOptions (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:10903:33)
at Object.normalizeApiOptions (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:11036:10)
at normalize$1 (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:13718:28)
at format (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:15111:46)
at c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:57542:12
at Object.format (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\node_modules\prettier\index.js:57562:12)
at t.default. (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\dist\extension.js:1:16946)
at Generator.next ()
at s (c:\Users\Home.vscode\extensions\esbenp.prettier-vscode-6.3.1\dist\extension.js:1:9119)
["INFO" - 21:23:59] Formatting completed in 38.575899ms.
I installed Svelte for VS Code. When I saved .svelte file all comments get deleted. This does not happen for .html, .js or .jsx files. I also use this Prettier plugin. Sometimes I just want to temporarily comment them. How do I disable this feature?
Below is the markup and vs-config
<div
on:mouseup={event => {
// console.log('This line gets deleted on save for .svelte file');
}} />
{
"workbench.colorTheme": "Default Light+",
"workbench.editor.enablePreviewFromQuickOpen": false,
"workbench.editor.enablePreview": false,
"prettier.singleQuote": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
},
"prettier.printWidth": 80,
"json.schemas": [
],
"editor.tabSize": 2
}
I use prettier to format my typescript code with the following config. However, prettier doesn't fix spacing/blank line issues as you can see below in the code snippet. There is supposed to be a blank line between userSignupSchema and userLoginSchema and only one blank line between userLoginSchema and tokenSchema. I am using VS Code. Any idea how to fix this?
{
"extends": ["airbnb", "prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"no-unused-vars": "warn",
"no-console": "off",
"func-names": "off",
"no-process-exit": "off",
"object-shorthand": "off",
"class-methods-use-this": "off"
},
"singleQuote": true
}
export const userSignupSchema = z.object({
name: z.string()
})
export const userLoginSchema = z.object({
name: z.string()
})
export const tokenSchema = z.object({
access_token: z.string()
})
Check the output of the ESLint extension: Ctrl + J to open the panel if it's not open → Output → ESLint. Your ESLint configuration is invalid. You can't have "singleQuote": true there. It's Prettier's option, so it should be specified in Prettier's configuration file.
I have been using vscode prettier for few months now. I always used it to auto format my codes with vscode shortcut Shift + Alt + F or type >Format Document in command palette
But all of a sudden, vscode gave me this error this error msg: "invalid prettier configuration file detected. See log for details.". This happend after update vscode to v1.53
When I click on "show Log". It show me this:. (It is much much more longer of cause, but I think here is the most import part)
["ERROR" - 2:50:11 PM] Invalid prettier configuration file detected.
["ERROR" - 2:50:11 PM] Must use import to load ES Module: /home/koonfoon/git-repos/koonfoon/someRepo/.prettierrc.js
require() of ES modules is not supported.
require() of /home/koonfoon/git-repos/koonfoon/someRepo/.prettierrc.js from /home/koonfoon/.vscode-server/extensions/esbenp.prettier-vscode-5.9.1/node_modules/prettier/third-party.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename .prettierrc.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/koonfoon/git-repos/koonfoon/someRepo/package.json.
Inside my package.json has value "type": "module".
This is how my .prettierrc.js looks like:
// .prettierrc.js
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4
};
.eslintrc.js:
// .eslintrc.js
module.exports = {
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": [
//"eslint:recommended",
"plugin:#typescript-eslint/recommended",
"prettier/#typescript-eslint",
"plugin:prettier/recommended"
],
"parser": "#typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
}
};
Please note: it was working fine until vscode updated to v1.53
My repo is written in typescript.
I have no ideal what cause this error.
Please help.
Thank you.