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

Error: Couldn't resolve parser "apex" #822

Open
test01bodacious opened this issue Oct 12, 2024 · 2 comments
Open

Error: Couldn't resolve parser "apex" #822

test01bodacious opened this issue Oct 12, 2024 · 2 comments

Comments

@test01bodacious
Copy link

test01bodacious commented Oct 12, 2024

I am using the following settings and it's not formatting .cls apex class files and giving the error "Couldn't resolve parser "apex""

Logs:
["INFO" - 2:08:41 AM] Formatting file:///c%3A/Users/myusername/Desktop/Workspace/VS%20Code/DevEdProj1/force-app/main/default/classes/Demo1.cls
["DEBUG" - 2:08:41 AM] Local prettier module path: C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier
["DEBUG" - 2:08:41 AM] Using prettier version 3.3.3
["INFO" - 2:08:41 AM] Using config file at C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier-plugin-apex\node_modules\wait-on.prettierrc.js
["DEBUG" - 2:08:41 AM] Local prettier module path: C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier
["INFO" - 2:08:41 AM] PrettierInstance:
{
"modulePath": "C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier",
"messageResolvers": {},
"version": "3.3.3"
}
["INFO" - 2:08:41 AM] Using ignore file (if present) at c:\Users\myusername\Desktop\Workspace\VS Code\DevEdProj1.prettierignore
["INFO" - 2:08:41 AM] File Info:
{
"ignored": false,
"inferredParser": "apex"
}
["INFO" - 2:08:41 AM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 2:08:41 AM] Prettier Options:
{
"filepath": "c:\Users\myusername\Desktop\Workspace\VS Code\DevEdProj1\force-app\main\default\classes\Demo1.cls",
"parser": "apex",
"tabWidth": 4,
"singleQuote": true,
"printWidth": 150,
"trailingComma": "none"
}
["ERROR" - 2:08:41 AM] Error formatting document.
["ERROR" - 2:08:41 AM] Couldn't resolve parser "apex".
ConfigError: Couldn't resolve parser "apex".
at getParserPluginByParserName (file:///C:/Users/myusername/AppData/Roaming/nvm/v20.18.0/node_modules/prettier/index.mjs:20571:9)
at normalizeFormatOptions (file:///C:/Users/myusername/AppData/Roaming/nvm/v20.18.0/node_modules/prettier/index.mjs:20645:24)
at formatWithCursor (file:///C:/Users/myusername/AppData/Roaming/nvm/v20.18.0/node_modules/prettier/index.mjs:21333:52)
at file:///C:/Users/myusername/AppData/Roaming/nvm/v20.18.0/node_modules/prettier/index.mjs:22728:12
at async Module.format2 (file:///C:/Users/myusername/AppData/Roaming/nvm/v20.18.0/node_modules/prettier/index.mjs:22733:25)
["INFO" - 2:08:41 AM] Formatting completed in 16ms.

My settings:

I have installed plugin globally

node version: v20.18.0 (latest LTS)
prettier version: 3.3.3
prettier-plugin-apex version: 2.1.5
@prettier/plugin-xml version: 3.4.1

Java version: java 17.0.4.1 2022-08-18 LTS (VS Code Java Home path configured)

VS Code Extension "Prettier - Code formatter" version: v11.0.0

Using Using native executables:
npx install-apex-executables

.prettierrc.js Path:
"C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier-plugin-apex\node_modules\wait-on.prettierrc.js"

.prettierrc.js File
module.exports = {
tabWidth: 4,
singleQuote: true,
printWidth: 150,
trailingComma: "none",
//plugins: ["prettier-plugin-apex", "@prettier/plugin-xml"],
overrides: [{
files: "/lwc//.html",
options: {
parser: "lwc",
tabWidth: 4
}
},
{
files: "**/
.{cls,trigger}",
options: {
parser: "apex",
apexInsertFinalNewline: true,
printWidth: 150,
tabWidth: 4
}
},
{
files: "**/.{apex}",
options: {
parser: "apex-anonymous",
apexInsertFinalNewline: true,
printWidth: 150,
tabWidth: 4
}
},
{
files: "
.{cmp,page,component}",
options: {
parser: "html",
tabWidth: 4
}
}
]
};

Note: I had to comment //plugins: ["prettier-plugin-apex", "@prettier/plugin-xml"] as it is giving error: Unable to resolve "prettier-plugin-apex"

package.json

{
"name": "salesforce-app",
"private": true,
"version": "1.0.0",
"description": "Salesforce App",
"scripts": {
"lint": "eslint /{aura,lwc}//.js",
"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 --apex-standalone-parser native --write "**/
.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}"",
"prettier:verify": "prettier --apex-standalone-parser nativex --check "/*.{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.1.2",
"@prettier/plugin-xml": "3.4.1",
"@salesforce/eslint-config-lwc": "^3.2.3",
"@salesforce/eslint-plugin-aura": "^2.0.0",
"@salesforce/eslint-plugin-lightning": "^1.0.0",
"@salesforce/sfdx-lwc-jest": "^5.1.0",
"eslint": "^9.9.1",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jest": "^28.8.1",
"husky": "^9.1.5",
"lint-staged": "^15.1.0",
"prettier": "3.3.3",
"prettier-plugin-apex": "2.1.5"
},
"lint-staged": {
"
/.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}": [
"prettier --apex-standalone-parser native --write"
],
"/{aura,lwc}//
.js": [
"eslint"
]
}
}

VS Code Settings:

Prettier: Config Path
C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier-plugin-apex\node_modules\wait-on.prettierrc.js

Prettier: Prettier Path
C:\Users\myusername\AppData\Roaming\nvm\v20.18.0\node_modules\prettier

@Damecek
Copy link

Damecek commented Oct 31, 2024

Parser of type apex does not exist. Here is a .prettierrc we are using which formats the apex files just fine.

{
  "trailingComma": "none",
  "singleQuote": true,
  "tabWidth": 4,
  "printWidth": 150,
  "plugins": [
    "prettier-plugin-apex",
    "@prettier/plugin-xml"
  ],
  "overrides": [
    {
      "files": "**/lwc/**/*.html",
      "options": {
        "parser": "lwc"
      }
    },
    {
      "files": "*.{cmp,page,component}",
      "options": {
        "parser": "html"
      }
    }
  ]
}

with this part of package.json

"devDependencies": {
    "@prettier/plugin-xml": "^3.4.1",
    "prettier": "^3.3.3",
    "prettier-plugin-apex": "^2.1.4"
  },

@Alfystar
Copy link

Alfystar commented Jan 22, 2025

hi @test01bodacious

I'm a DevOps manager for my company, we use prettier and salesforce.

For your usecase, i think you have to change your package.json to:

{
  "name": "<name>",
  "version": "1.0.0",
  "author": "<Email>",
  "devDependencies": {
    "@prettier/plugin-xml": "^3.4.1",
    "prettier": "^3.4.2",
    "prettier-plugin-apex": "^2.2.2"
  },
  "repository": {
    "type": "http",
    "url": "....."
  },
  "scripts": {
    "prettier": "prettier"
  },
  "dependencies": {
    "sfdx-plugin-source-read": "^1.4.0"
  }
}

I suggest to you to use the sfdx-plugin-source-read to retrieve easy and correct Profile

After the package.json update, execute in terminal:

npm install -y

to apply changes

and change the .prettierrc to:

{
	"plugins": ["prettier-plugin-apex", "@prettier/plugin-xml"],
	"printWidth": 600,
	"singleQuote": true,
	"trailingComma": "all",
	"bracketSpacing": false,
	"useTabs": true,
	"bracketSameLine": true,
	"apexInsertFinalNewline": false,
	"overrides": [
		{
			"files": "*.{trigger,cls}",
			"options": {"parser": "apex"}
		},
		{
			"files": "*.{apex}",
			"options": {"parser": "apex-anonymous"}
		},
		{
			"files": "**/lwc/**/*.html",
			"options": {"parser": "lwc"}
		},
		{
			"files": "*.{cmp,page,component,evt}",
			"options": {
				"parser": "html",
				"trailingComma": "none"
			}
		},
		{
			"files": "**/aura/**/*.js",
			"options": {"trailingComma": "none"}
		},
		{
			"files": "**/aura/**/*.{auradoc,design,app}",
			"options": {"parser": "angular"}
		},
		{
			"files": "**/*.xml",
			"options": {
				"parser": "xml",
				"useTabs": false,
				"tabWidth": 4,
				"xmlWhitespaceSensitivity": "preserve",
				"embeddedLanguageFormatting": "off",
				"xmlSelfClosingSpace": false
			}
		}
	]
}

Note the 1° Line:

! "plugins": ["prettier-plugin-apex", "@prettier/plugin-xml"]

Than should work 😁

Tell me if work

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

3 participants