Irene
Irene

Reputation: 181

VSC Apex-prettier-plugin-apex doesn't format - throws error

I use Visual Studio Code and have installed Prettier plugin as well as Prettier-plugin-apex according to https://developer.salesforce.com/tools/vscode/en/user-guide/prettier (also https://www.sfdcnotes.com/2019/12/16/vs-code-extension-prettier-code-formatter/).

It used to format my classes but lately has been failing, but all I see is this in the bottom right with no explanation of what's wrong when I save a class file (it does format on save/deploy on save).

enter image description here

When I try and manually format with npx prettier --write I get this error:

PS C:\Users\xx\Documents\VSC\SynDev> npx prettier --write force- 
app/main/default/classes/emailReportController_Test.cls     
force-app\main\default\classes\emailReportController_Test.cls
[error] force-app\main\default\classes\emailReportController_Test.cls: Error
[error]     at parseTextWithSpawn  (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier-plugin-apex\src\parser.js:36:11)
[error]     at Object.parse (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier-plugin-apex\src\parser.js:489:21)
[error]     at Object.parse$a [as parse] (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:14892:19)
[error]     at coreFormat (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:16425:16)
[error]     at formatWithCursor$1 (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:16665:14)
[error]     at Object.formatWithCursor (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\index.js:60922:12)
[error]     at format$1 (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:16107:14)
[error]     at Object.formatFiles$1 [as formatFiles] (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:16218:16)
[error]     at main (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:17989:10)
[error]     at Object.run (C:\Users\xx\Documents\VSC\SynDev\node_modules\prettier\bin-prettier.js:17932:5)

How do I fix this - what is wrong (I'm no expert on VSC or npm)?

This is my package.json file:

{
  "name": "salesforce-app",
  "private": true,
  "version": "1.0.0",
  "description": "Salesforce App",
  "scripts": {
    "lint": "npm run lint:lwc && npm run lint:aura",
    "lint:aura": "eslint **/aura/**",
    "lint:lwc": "eslint **/lwc/**",
    "test": "npm run test:unit",
    "test:unit": "sfdx-lwc-jest",
    "test:unit:watch": "sfdx-lwc-jest --watch",
    "test:unit:debug": "sfdx-lwc-jest --debug",
    "test:unit:coverage": "sfdx-lwc-jest --coverage",
    "prettier": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
    "prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\""
  },
  "devDependencies": {
    "@prettier/plugin-xml": "^0.12.0",
    "@salesforce/eslint-config-lwc": "^0.11.0",
    "@salesforce/eslint-plugin-aura": "^2.0.0",
    "@salesforce/sfdx-lwc-jest": "^0.10.4",
    "eslint": "^7.24.0",
    "eslint-config-prettier": "^6.11.0",
    "husky": "^4.2.1",
    "lint-staged": "^10.0.7",
    "prettier": "2.3.0",
    "prettier-plugin-apex": "1.9.1"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}": [
      "prettier --write"
    ],
    "**/{aura|lwc}/**": [
      "eslint"
    ]
  },
  "main": "jest.config.js",
  "author": "",
  "license": "ISC"
}

And .prettierrc

{
  "trailingComma": "none",
  "overrides": [
    {
      "files": "**/lwc/**/*.html",
      "options": { "parser": "lwc" }
    },
    {
      "files": "*.{cmp,page,component}",
      "options": { "parser": "html" }
    }
  ]
}

Upvotes: 4

Views: 4694

Answers (3)

R. van Diesen
R. van Diesen

Reputation: 869

Had the same issue and performed these steps to fix it:

Add the below plugin line to your .prettierrc

"plugins": ["prettier-plugin-apex"],

enter image description here

  • npm install prettier-plugin-apex@latest => resulted in version ^2.1.4
  • npm install prettier@latest => resulted in version ^3.3.3

Restart vsCode

Upvotes: 1

Ignas
Ignas

Reputation: 4338

I had to upgrade JDK from 8 to 11 to fix this.

Upvotes: 3

noullet
noullet

Reputation: 96

I had the same issue and found a workaround.

Install a previous version of prettier-plugin-apex by changing the version number in package.json:

"prettier-plugin-apex": "1.8.0"

Edit: Then run npm install prettier-plugin-apex to actually perform the downgrade.

Upvotes: 7

Related Questions