In that case it sounds like something the Xcode team need to take a look at. Go to https://feedbackassistant.apple.com/ and file a report there (you want Developer Tools, then Xcode). Describe exactly what happens (there's lots of specific prompts to fill in), include the exact versions of your OS and Xcode, and attach a zip file of the project (or of a simple project that reproduces the issue). Along with that, attach a system diagnosis report, which is what all good bugs need—it includes all the logs and other bits of useful information about what the system is doing at the time; if there's something different about your system that's causing the error, chances are the sysdiagnose will contain everything they need to root out the issue.
To generate a system diagnosis:
- Reproduce the issue. Note the time at which you do so (or a range: started at X, tried Y times, ended at Z) to include in the bug report.
- Press the following keys together: Control-Option-Command-Shift-Period (⌃⌥⌘⇧.) and the system will start assembling an archive for you.
- After a couple of minutes, a Finder window will open and display a file named sysdiagnose_XXXX.tar.gz, where XXXX is the time & date the diagnosis was collected.
- Drag that file into the attachments list on your bug report, then click Save to send the report to Apple.
It's probably worth including a link to this forum post in the bug report. If the solution is straightforward, the engineer may well reply here telling you how to resolve the issue. This sounds a little more complex, though, since I'm not able to reproduce it on a clean installation here.