dangmai / prettier-plugin-apex

Code formatter for the Apex Programming Language
https://apex.dangmai.net
MIT License
235 stars 43 forks source link

Apex - Prettier error spawnSync EINVAL when formatting Apex classes. #1563

Closed ScilentAssasin closed 1 month ago

ScilentAssasin commented 1 month ago

Summary

Prettier is not working to format apex files.

Steps To Reproduce:

Expected result

Format the file

Actual result

Prettier Error spawnSync

Duplicate Issues for more information:

salesforcedx-vscode#5732

prettier-vscode#3495

dangmai commented 1 month ago

This most likely means you're running an old version of Prettier Apex. Please upgrade to latest version, then restart VSCode (that last part is not optional). Let me if that works.

DiegoCanuto commented 1 month ago

@dangmai I updated for 2.4.1 but is not working too. It even recongnize the prettier as a formater. I downgrade vsCode but is not working anymore. Prettier Issue image image

dangmai commented 1 month ago

There are changes you need to make to the configuration when upgrading to v2, please follow this guide here: https://github.com/dangmai/prettier-plugin-apex/wiki/Upgrading-to-Prettier-Apex-v2

DiegoCanuto commented 1 month ago

@dangmai even doing this changes I still get the same msg above: Node.js v22.6.0 prettier-plugin-apex@2.1.4 prettier@3.3.3 Vscode: 1.91.1 image

dangmai commented 1 month ago

That needs to be a list, not a string. I'm not that familiar with configuring Prettier using YAML (which it looks like what you're doing), so I might not be of too much help here. Also, make sure you restart VSCode after making that change.

This guide from Salesforce is usually what I recommend people to follow: https://developer.salesforce.com/docs/platform/sfvscode-extensions/guide/prettier.html It still works now when I just tried it, so maybe that'd give you some debugging ideas.

DiegoCanuto commented 1 month ago

@dangmai I created a json file with the same attributes of the YAML file, restarted VsCode like I did before, but now nothing happen lol

image

["INFO" - 4:10:30 PM] Formatting file:///c%3A/Users/Project/Documents/Org/salesforce-source/src/libs/apex-scheduler/default/main/classes/ApexScheduler.cls ["INFO" - 4:10:30 PM] Using config file at c:\Users\Project\Documents\Org\salesforce-source.prettierrc.json ["INFO" - 4:10:30 PM] PrettierInstance: { "modulePath": "c:\Users\Project\Documents\Org\salesforce-source\node_modules\prettier\index.cjs", "messageResolvers": {}, "version": "3.3.3" } ["INFO" - 4:10:30 PM] Using ignore file (if present) at c:\Users\Project\Documents\Org\salesforce-source.prettierignore ["INFO" - 4:10:30 PM] File Info: { "ignored": true, "inferredParser": null } Any other thoughts?

dangmai commented 1 month ago

Do you mind posting the content of c:\Users\Project\Documents\Org\salesforce-source.prettierrc.json?

DiegoCanuto commented 1 month ago

Sure:

{ "plugins": ["prettier-plugin-apex"], "trailingComma": "es5", "tabWidth": 4, "singleQuote": true, "printWidth": 140, "apexInsertFinalNewline": false, "overrides": [ { "files": "/lwc//.html", "options": { "parser": "lwc" } }, { "files": ".{cmp,page,component}", "options": { "parser": "html" } } ] }

dangmai commented 1 month ago

Could you run this inside your project directory and paste the result here please?

npx envinfo --system --npmPackages 'prettier*' --binaries --npmGlobalPackages 'prettier*' --languages

Also, let's try formatting a file using the CLI instead of VSCode to see if we can rule that out (I typed this out from memory so there may be some issue with the syntax):

npx prettier c:\Users\Project\Documents\Org\salesforce-source\src\libs\apex-scheduler\default\main\classes\ApexScheduler.cls
DiegoCanuto commented 1 month ago

System: OS: Windows 10 10.0.19045 CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor Memory: 3.65 GB / 15.95 GB Binaries: Node: 22.6.0 - C:\Program Files\nodejs\node.EXE npm: 10.8.2 - C:\Program Files\nodejs\npm.CMD Languages: Java: 22.0.2 npmPackages: prettier: 3.3.3 => 3.3.3 prettier-plugin-apex: 2.1.4 => 2.1.4 npmGlobalPackages: prettier-plugin-apex: 2.1.4 prettier: 3.3.3

After running the first command, the formatting is working again, I'll test in more files to see if everything is ok and let you know, but thanks in advance :)

veereshkumarbabu commented 1 month ago

I've been following your tickets and comments, @DiegoCanuto. I'm also facing the same issue. I've tried all the above steps. No luck, though. Could you please help me fix this issue? @dangmai

DiegoCanuto commented 1 month ago

Could you please post your config files / plugin versions like I did ?

MaxHuet commented 1 month ago

Hey, I had the same issue. I did update to latest version both prettier and prettier-plugin-apex and it now works for me:

It had this effect on my package.json : image Versions :

Bellow is my .prettierrc file :

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

I hope this can help you

veereshkumarbabu commented 1 month ago

@MaxHuet Executing the above commands helped me fix the issue. Thanks for all the help! @DiegoCanuto @dangmai