Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode Prettier Plugin required settings modification to continue working #1168

Closed
daveerickson opened this issue Jan 12, 2024 · 9 comments
Closed

Comments

@daveerickson
Copy link

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):

  • OS: Windows 11
  • Version: prettier-plugin-apex@2.0.1
  • Prettier Version: prettier@3.1.1
  • Java Version: "21" 2023-09-19 LTS
@dangmai
Copy link
Owner

dangmai commented Jan 12, 2024

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?

@daveerickson
Copy link
Author

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.

@dangmai
Copy link
Owner

dangmai commented Jan 12, 2024

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.

@daveerickson
Copy link
Author

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.

@constantine-prokopenko-vrp
Copy link

constantine-prokopenko-vrp commented Jan 15, 2024

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

@mplunket
Copy link

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)

@dangmai dangmai changed the title Plugin stopped working VSCode Prettier Plugin required settings modification to continue working Feb 18, 2024
@dangmai dangmai pinned this issue Feb 18, 2024
@msouther-lisc
Copy link

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

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

@constantine-prokopenko-vrp

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

@dangmai
Copy link
Owner

dangmai commented Mar 21, 2024

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 dangmai closed this as completed Mar 21, 2024
@dangmai dangmai unpinned this issue Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants