I just fixed the same issue!
I was installing Xcode 15 on my new machine M2 ( Apple Silicon ) so first of all, this is the general error message but you would need more details to understand the root cause of the issue.
Use the Xcode report navigator 🧐
To show the report navigator, either (1) click the rightmost button in the navigator bar at the top of the navigator area, which is on the left side of the workspace window, (2) select View > Navigators > Reports from the menu bar at the top of the screen, or press ⌘9.
After showing the report, I was able to identify the root cause which is Bad CPU type in executable
as shown below:
So as I am using the M2 machine, I found the reason because when macOS tries to run an app that is not built for Apple silicon, macOS will prompt to install Rosetta 2 ( It enables a Mac with Apple silicon to use an app built for a Mac with an Intel processor. ) to automatically translate the app to Apple silicon but this did not automatically happen with my machine and maybe yours and also in Terminal, there is no automatic detection for missing Rosetta to run older architecture command line tools.
If you haven't been prompted for any other app already, manually install Rosetta 2:
softwareupdate --install-rosetta
Installing Rosetta 2:
Getting back to my Xcode and I was finally able to build the project successfully! It is simply because Xcode is one of the apps/tools that were originally designed for Intel-based Macs and are now translated by Rosetta 2 on Apple silicon Macs.
Good luck 🤞