If the target directory wasn't in a project, this command would throw a type error (see #8032). This also adds more checks and informative error handling to the command. Fix: #8032