1

We have tests that run in a scratch org if the "Compile all classes" Setup link is used between the code being pushed and the tests being run. But when using sfdx to deploy the components and then run the tests, we consistently get a gack (a "Internal Salesforce Error").

So I'm wondering the in 2021 there is an API available that was not present in 2016 when this was asked before: Compile All Classes? so we could add it into our build process as a workaround.

2 Answers 2

4

The enableCompileOnDeploy setting should make that happen. It should be applied to your scratch org definition. It is enabled by default when deploying to production, for other orgs the default is false.

Example:

{
    "orgName": "Default",
    "edition": "Developer",
    "settings": {
        "apexSettings": {
            "enableCompileOnDeploy": true
        }
    }
}
3
  • Does a push on its own work, or is a deploy needed as implied by the setting name and as mentioned by sfdcfox?
    – Keith C
    Mar 4, 2021 at 18:26
  • 1
    @KeithC, I've tested that and I couldn't cause issues after turning on the enableCompileOnDeploy, even when running exclusively a push. Also I'm no longer getting the Internal Salesforce Error messages when running the unit tests. We were redeploying all triggers with the deploy command, which was a workaround, but I've removed that and so far so good. Mar 5, 2021 at 10:57
  • Another way to compile classes, but on this for packages is a flag --apexcompile like sfdx force:package:install --apexcompile=all .... Mar 5, 2021 at 12:53
1

The only real way to "compile all classes" would be to deploy all the source code (e.g. sfdx force:source:deploy -p force-app/main/default/). Note that this is only a developer/sandbox problem, as production always compiles all code before deployment/unit tests. Also, note that Unlocked Packages always "compile all classes" during version creation, so that's another alternative.

3
  • 1
    So instead if or as well as a force:source:push? My colleague is also looking into setting the enableCompileOnDeploy setting to true which is documented like this "The default value is true for production orgs and false for others".
    – Keith C
    Mar 4, 2021 at 16:58
  • @KeithC It looks like you can enable "Compile on Deploy" for any org in Setup > Develop > Apex Classes. I've apparently missed this. I don't think it work for push, though, you need an actual deploy.
    – sfdcfox
    Mar 4, 2021 at 17:36
  • Thanks I'll mention the push/deploy distinction to my colleague and get him to post his results.
    – Keith C
    Mar 4, 2021 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.