You cannot import Cocoa in Playgrounds. It should give you the error "No such module Cocoa". Try removing it. The code snippet you provided doesn't need modules, it can run with just the standard library.
If you need to import Cocoa, show the file inspector via View > Inspector > Show Inspector and under Playground settings select macOS as the platform.
Could you file a bug report on feedbackassistant.apple.com with the appropriate configuration information (e.g. what OS and Xcode versions you are using)? This seems like a serious issue.
I've reported this as rdar://73169109. In the meantime, it might be worthwhile to try downloading an alternate version of Xcode and seeing if that fixes the issue. The latest version available on Apple Developer seems to be Xcode 12.3, you can download that from Sign In - Apple or try an older release from Sign In - Apple.
If you are still seeing the issue and are able to file a feedback, then one suggestion I received was that you can collect System Diagnostics (run sysdiagnose on the command line) immediately after the issue occurs (while Xcode is still running) and attach the sysdiagnose file to the feedback.
And if you don't want to use the command line, you can also press Control-Option-Command-Shift-Period, and wait for the "sysdiagnose_*" file to pop up in Finder, and then share that file with us.