I know this is old but recently happened to me after upgrading to Monterey 12.6. The reason was because XCode was not properly installed and was fixed with:
xcodebuild -runFirstLaunch.
Source: https://github.com/Homebrew/discussions/discussions/3725#discussioncomment-3783261
Worked perfectly for me.