I just encountered the exact same issue here in Monterey 12.1 on a Mac Mini M1 (2020).
This is pretty annoying and also for me unexpected behaviour, especially if that script file was created by yourself on your own computer and had been executing just fine before editing it with TextEdit...
I filed a bug report in the Feedback Assistant: bug number FB9815707.