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.