This used to work at the very least on 2021 & earlier which is where we’re upgrading from, you could configure your build by interacting with PlayerSettings & EditorUserBuildSettings etc, then you could use BuildPipeline.BuildPlayer to create a build. But now calling SetScriptingDefineSymbols causes an async recompilation of the player scripts, and there seems to be no way to safely wait it out to ultimately call BuildPipeline.BuildPlayer.
If you don’t wait at all, as used to be possible, then it fails with an error that scripts are compiling, if you try to leverage EditorApplication.update to wait it out together with EditorApplication.isCompiling you will correctly notice that the application is compiling, but at some point EditorApplication.update is cleared by the editor & you will never reach the point where you can call BuildPipeline.BuildPlayer.
We fixed this by making sure that there’s no frames which are yielded in between SetScriptingDefineSymbols & BuildPlayer. There has been some undocumented change here which causes it to behave differently. So if you want to enable / disable packages as part of the build which requires waiting those out one needs to do that before SetScriptingDefineSymbols, and EditorApplication.isCompiling is useless so don’t look at that one.