prettier-plugin-apex icon indicating copy to clipboard operation
prettier-plugin-apex copied to clipboard

VSCode Prettier Plugin required settings modification to continue working

Open daveerickson opened this issue 1 year ago • 8 comments

This plugin has been working, but earlier this week it stopped. As far as I can tell, nothing has changed in the .prettierrc or package.json files. Prettier will format json and js files, but states There is no formatter for 'apex' files installed.

package.json

{
  "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}\"",
    "postinstall": "husky install",
    "precommit": "lint-staged"
  },
  "devDependencies": {
    "@lwc/eslint-plugin-lwc": "^1.7.2",
    "@prettier/plugin-xml": "^3.2.2",
    "@salesforce/eslint-config-lwc": "^3.5.2",
    "@salesforce/eslint-plugin-aura": "^2.1.0",
    "@salesforce/eslint-plugin-lightning": "^1.0.0",
    "@salesforce/sfdx-lwc-jest": "^3.1.1",
    "eslint": "^8",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-compat": "^4.2.0",
    "eslint-plugin-import": "^2.29.1",
    "eslint-plugin-jest": "^27.6.2",
    "husky": "^8.0.3",
    "lint-staged": "^15.2.0",
    "prettier": "3.1.1",
    "prettier-plugin-apex": "2.0.1"
  },
  "lint-staged": {
    "original": "**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}",
    "**/*.{cls,component,html,json,md,page,trigger,xml,yaml,yml}": [
      "prettier --write"
    ],
    "removed": "**/{aura,lwc}/**\": [\"eslint\"]"
  },
  "dependencies": {
    "nvm": "^0.0.4",
    "salesforce-app": "^1.0.0"
  }
}

.prettierrc

{
  "apexInsertFinalNewline": false,
  "printWidth": 120,
  "trailingComma": "none",
  "plugins": ["prettier-plugin-apex"],
  "overrides": [
    {
      "files": "**/lwc/**/*.html",
      "options": { "parser": "lwc" }
    },
    {
      "files": "*.{cmp,page,component}",
      "options": { "parser": "html" }
    }
  ]
}

Thank you for your assistance.

Additional information (please fill this out):

daveerickson avatar Jan 12 '24 16:01 daveerickson

Hello, thanks for opening this bug ticket.

I think I need more info on your particular case, I've tried using your package.json and .prettierrc in this sample repo: https://github.com/dangmai/ppa-1168 and it works fine for me. Here are the steps for you to try it:

git clone https://github.com/dangmai/ppa-1168.git
cd ppa-1168
npm ci
npx prettier src/AnonymousApex.apex

Some thing I do notice: it looks like you're trying to format .apex files, which are by default Anonymous Apex code, but I don't see that extension in your prettier or prettier:verify scripts. Is that intentional?

dangmai avatar Jan 12 '24 19:01 dangmai

Thanks for taking the time to help troubleshoot. I didn't fully explain I am having issues within vscode, configured to format upon save. The error I received occurs if I run Format Document from the command pallet. When I test your ppa and attempt the same on the AnonymousApex.apex file I receive There is no formatter for 'apex-anon' files installed.

If I clone your ppa repo and run the last command I don't receive any error, but if I edit the AnonymousApex.apex file with additional lines and run the command the extra lines are not removed.

Again, much appreciated for your help.

daveerickson avatar Jan 12 '24 20:01 daveerickson

Thank you very much for the context, I can reproduce that in VSCode now.

This is related to this bug ticket on the Prettier VSCode extension: https://github.com/prettier/prettier-vscode/issues/3235 Could you try the workaround there and see if it resolves your issue? (either downgrading to Prettier 3.1.0 or configuring prettier.configPath)

There's not much I can do about this particular issue in this repo, since it's a VSCode thing, but I'll keep this issue opened for now in case other folks run into the same thing. I'm hoping that it will be fixed in the VSCode plugin repo.

dangmai avatar Jan 12 '24 21:01 dangmai

This is related to this bug ticket on the Prettier VSCode extension: prettier/prettier-vscode#3235 Could you try the workaround there and see if it resolves your issue? (either downgrading to Prettier 3.1.0 or configuring prettier.configPath)

Bingo, that fixed it! Thank you for tracking down and sharing the bug/fix.

daveerickson avatar Jan 12 '24 21:01 daveerickson

The fix with adding path to prettier config to .vscode/settings.json worked for me. This works for Prettier 3.2.2.

Thank you very much for the context, I can reproduce that in VSCode now.

This is related to this bug ticket on the Prettier VSCode extension: prettier/prettier-vscode#3235 Could you try the workaround there and see if it resolves your issue? (either downgrading to Prettier 3.1.0 or configuring prettier.configPath)

There's not much I can do about this particular issue in this repo, since it's a VSCode thing, but I'll keep this issue opened for now in case other folks run into the same thing. I'm hoping that it will be fixed in the VSCode plugin repo.

Thanks this worked for me! So that future viewers with this issue don't have to navigate to the Prettier VSCode repo for workaround -- here is the line:

Try adding { "prettier.configPath": "./.prettierrc" } to .vscode/settings.json file (replace .prettierrc with the file name that you use)

mplunket avatar Jan 31 '24 05:01 mplunket

For whatever reason, @mplunket 's solution didn't work for me until I added this to my .prettierrc file:

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

msouther-lisc avatar Feb 22 '24 18:02 msouther-lisc

For whatever reason, @mplunket 's solution didn't work for me until I added this to my .prettierrc file:

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

Wanted to point that on the Prettier Plugin for Apex page you can find this line is required. Screenshot 2024-02-23 at 08 59 36

Looks like the upstream ticket in the Prettier VSCode repo has now been fixed, so I'm closing this issue as well. Thanks for all the workarounds everyone!

dangmai avatar Mar 21 '24 17:03 dangmai