Final newline causes conflict on push
Summary
By default prettier apex will add a new line at the end of the file when formatting. Seems like the new line at the end of files is removed on push by salesforce. The push to an org from vscode will then fail as it detects a conflict.
It is a common convention that files end with a new line https://thoughtbot.com/blog/no-newline-at-end-of-file
There is a setting in apex prettier to disable the new line which solves the problem. However since a new line is fairly standard practice and not having it can cause issues with other tools and utilities, It would be ideal to preserve it in files in source control.
Steps To Reproduce:
- Create a new sfdx project
sfdx force:project:create --projectname 'myproject' - Ensure prettier plugin is installed and working in vscode
-
npm installin the project folder (I had to remove eslint stuff to get npm install to work) - Create a new org
- Create a new apex classes, add some basic content, then save (ensure it formatted and pushed successfully to the org)
- Edit the class e.g add a new method then save again (ensure it formatted and push starts)
You will get the error We couldn’t push your source due to conflicts. Make sure that you want to overwrite the metadata in your org with your local files, then run "SFDX: Push Source to Default Scratch Org and Override Conflicts".
Expected result
I believe the ideal solution would be for the files to be preserved with the final new line so that if they have it in source control it will not cause issues on push.
With this approach if some users omit the final new line the push works and if some want to include it then the push works for them too.
Actual result
The push errors due to a conflict (the final new line)
Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.
@lukecotter Using your repo steps I am unable to reproduce the push conflict. I was able to cause a conflict by changing a class both locally (with end of file newline) and directly in the org and then do a source push. Although I believe this to be conflict due to changes on both sides.
It appears that any save of an Apex class (either via push or save in UI) will strip the trailing newline from the file. That functionality belongs a different team.
We have determined that the issue you reported exists in code owned by another team that uses only the official support channels. To ensure that your issue is addressed, open an official Salesforce customer support ticket with a link to this issue. We encourage anyone experiencing this issue to do the same to increase the priority. We will keep this issue open for the community to collaborate on.
We have determined that the issue you reported exists in code owned by another team that uses only the official support channels. To ensure that your issue is addressed, open an official Salesforce customer support ticket with a link to this issue. We encourage anyone experiencing this issue to do the same to increase the priority. We will keep this issue open for the community to collaborate on.
Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.
Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.
@peternhale I took another look at this and got it fail by doing this
- Create a new sfdx project
sfdx force:project:create --projectname 'myproject' - Ensure prettier plugin is installed and working in vscode
-
npm installin the project folder (I had to remove eslint stuff to get npm install to work) - Create a new org
- Create a new apex classes, add some basic content, then save (ensure it formatted and pushed successfully to the org)
- Edit the class e.g add a new method then save again (ensure it formatted and push starts)
Now you will see the failure.
Here is the new method I added
public string getValue() {
return null;
}
Hi, any updates on this. If this is the standard behaviour, then prettier apex should set apexInsertFinalNewline to false by default to avoid this issue. Thanks
@dangmai
I don't think Salesforce will be able to update metadata-api to preserve the eol new line but at the least sfdx should add the new line after it has been retrieved the files from sfdc, possibly based on a config in the sfdx-project.json.
This new line issue is causing unnecessary issues and conflicts in the cicd flow.
We're all stuck with how the metadata API behaves on retrieve. To stay compatible with tooling that doesn't use the CLI or its libraries and the use case of "download zip from mdapi, unzip using the tool of your choice" we're going to align templates with the metadata API behavior.
We also want the support for VSCode extensions diff checking (does this file look the same in the org).
If you have behavior you don't like, please open a support case (for example, get the apex team to standardize EOF newlines).
FWIW, I also think prettier-apex should align with whatever the server is doing