salesforcedx-vscode
salesforcedx-vscode copied to clipboard
"SFDX Diff Folder against org" not formatting Apex code with prettier : "Parser not inferred, trying VS Code language"
Summary
In VS Code, when running "SFDX Diff Folder against org" on a "classes" folder of Apex code (from a SFDX-format source repository), the visualisation does not run prettier correctly on the file downloaded from the org
Steps To Reproduce:
- Get any local repo containing some Apex classes, and sync it with a Salesforce org
- Make a local change to a class, save it but do not deploy/push it to Salesforce
- Create a local .prettierrc file with this :
{
"trailingComma": "es5",
"tabWidth": 2,
"printWidth": 250,
"bracketSpacing": true,
"bracketSameLine": true,
"semi": false,
"arrowParens": "avoid",
"singleAttributePerLine": false,
"overrides": [
{
"files": "**/lwc/**/*.html",
"options": {
"parser": "lwc"
}
},
{
"files": "**/*.{cls,trigger,apex}",
"options": {
"apexInsertFinalNewline": true,
"parser": "apex"
}
},
{
"files": "*.{cmp,page,component}",
"options": {
"parser": "html"
}
}
]
}
- Restart VS Code
- Right-click on a "classes" folder and select "SFDX Diff against org"
- The "Org Differences :conflict" panel opens up with the modified Apex class. Click on it : the file on the right (local) is correctly formatted (2 space tabs); the file on the left is not (4 space tab)
- select all content fo the file on the left (distant, stored in tmp location) and choose "format whole document"
- Open VS Code terminal log for "Prettier" and read the error message :
["INFO" - 4:58:12 PM] Formatting file:///var/folders/zm/xxx/main/default/classes/MyClass.cls
["INFO" - 4:58:12 PM] File Info:
{
"ignored": false,
"inferredParser": null
}
["WARN" - 4:58:12 PM] Parser not inferred, trying VS Code language.
["ERROR" - 4:58:12 PM] Failed to resolve a parser, skipping file. If you registered a custom file extension, be sure to configure the parser.
Expected result
The distant file (on the left) should be formatted as the one on the right, immediately as it is created
Actual result
The distant file (on the left) should be formatted by prettier
Additional information
The reason for this bug looks quite simple (I think) : as the distant file is downloaded into a tmp location which is NOT under the current project, I suppose VS Code will not apply its local formatting rules as defined in .prettierrc If the file were downloaded into a tmp location in the project's folder, eg under .sfdx, then the formatter would probably correctly inferred
Salesforce Extension Version in VS Code:
Salesforce Extension Pack (Expanded)v 56.14.0
SFDX CLI Version:
sfdx-cli/7.182.1 darwin-x64 node-v18.12.1
OS and version:
macOS 11.7.3
@AnanyaJha I think there's a good use case for this, in order to isolate the changes between the files from formatting-only changes. I also think it would be good to explore using a tmp dir within the project to store the remote file used for the diff.
Hi @RupertBarrow thanks for sharing this feedback! I agree @klewis-sfdc, this sounds like a reasonable request. Let's add this to our Extensions backlog
Hi @AnanyaJha , are you making progress on this one ?