We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
run_target Permission Denied error should be clearer
Minimal example - 
[folder.zip](https://github.com/mesonbuild/meson/files/1530489/folder.zip)

I ran - `mkdir build && cd build && meson .. && ninja` and everything works. Now I run - `ninja myscript` and it throws errors -

```
[0/1] Running external command myscript.
Traceback (most recent call last):
  File "/usr/bin/meson", line 37, in <module>
    sys.exit(main())
  File "/usr/bin/meson", line 34, in main
    return mesonmain.run(sys.argv[1:], launcher)
  File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 311, in run
    sys.exit(run_script_command(args[1:]))
  File "/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py", line 278, in run_script_command
    return cmdfunc(cmdargs)
  File "/usr/lib/python3.6/site-packages/mesonbuild/scripts/commandrunner.py", line 60, in run
    pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)
  File "/usr/lib/python3.6/site-packages/mesonbuild/scripts/commandrunner.py", line 39, in run_command
    return subprocess.Popen(command_array, env=child_env, cwd=cwd)
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
PermissionError: [Errno 13] Permission denied: '/home/agauniyal/temp/scripts/script.sh'
FAILED: meson-myscript 
/usr/bin/python /usr/bin/meson --internal commandrunner /home/agauniyal/temp/ /home/agauniyal/temp/build '' /usr/bin/python /usr/bin/meson /home/agauniyal/temp/scripts/script.sh
ninja: build stopped: subcommand failed.
```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mesonbuild/scripts/commandrunner.py`
Content:
```
1 # Copyright 2014 The Meson development team
2 
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 """This program is a wrapper to run external commands. It determines
16 what to run, sets up the environment and executes the command."""
17 
18 import sys, os, subprocess, shutil, shlex
19 
20 def run_command(source_dir, build_dir, subdir, meson_command, command, arguments):
21     env = {'MESON_SOURCE_ROOT': source_dir,
22            'MESON_BUILD_ROOT': build_dir,
23            'MESON_SUBDIR': subdir,
24            'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in meson_command + ['introspect']]),
25            }
26     cwd = os.path.join(source_dir, subdir)
27     child_env = os.environ.copy()
28     child_env.update(env)
29 
30     # Is the command an executable in path?
31     exe = shutil.which(command)
32     if exe is not None:
33         command_array = [exe] + arguments
34         return subprocess.Popen(command_array, env=child_env, cwd=cwd)
35     # No? Maybe it is a script in the source tree.
36     fullpath = os.path.join(source_dir, subdir, command)
37     command_array = [fullpath] + arguments
38     try:
39         return subprocess.Popen(command_array, env=child_env, cwd=cwd)
40     except FileNotFoundError:
41         print('Could not execute command "%s".' % command)
42         sys.exit(1)
43 
44 def run(args):
45     if len(args) < 4:
46         print('commandrunner.py <source dir> <build dir> <subdir> <command> [arguments]')
47         return 1
48     src_dir = args[0]
49     build_dir = args[1]
50     subdir = args[2]
51     meson_command = args[3]
52     if 'python' in meson_command: # Hack.
53         meson_command = [meson_command, args[4]]
54         command = args[5]
55         arguments = args[6:]
56     else:
57         meson_command = [meson_command]
58         command = args[4]
59         arguments = args[5:]
60     pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)
61     pc.wait()
62     return pc.returncode
63 
64 if __name__ == '__main__':
65     sys.exit(run(sys.argv[1:]))
66
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mesonbuild/scripts/commandrunner.py b/mesonbuild/scripts/commandrunner.py
--- a/mesonbuild/scripts/commandrunner.py
+++ b/mesonbuild/scripts/commandrunner.py
@@ -38,7 +38,10 @@
     try:
         return subprocess.Popen(command_array, env=child_env, cwd=cwd)
     except FileNotFoundError:
-        print('Could not execute command "%s".' % command)
+        print('Could not execute command "%s". File not found.' % command)
+        sys.exit(1)
+    except PermissionError:
+        print('Could not execute command "%s". File not executable.' % command)
         sys.exit(1)
 
 def run(args):

{"golden_diff": "diff --git a/mesonbuild/scripts/commandrunner.py b/mesonbuild/scripts/commandrunner.py\n--- a/mesonbuild/scripts/commandrunner.py\n+++ b/mesonbuild/scripts/commandrunner.py\n@@ -38,7 +38,10 @@\n     try:\n         return subprocess.Popen(command_array, env=child_env, cwd=cwd)\n     except FileNotFoundError:\n-        print('Could not execute command \"%s\".' % command)\n+        print('Could not execute command \"%s\". File not found.' % command)\n+        sys.exit(1)\n+    except PermissionError:\n+        print('Could not execute command \"%s\". File not executable.' % command)\n         sys.exit(1)\n \n def run(args):\n", "issue": "run_target Permission Denied error should be clearer\nMinimal example - \r\n[folder.zip](https://github.com/mesonbuild/meson/files/1530489/folder.zip)\r\n\r\nI ran - `mkdir build && cd build && meson .. && ninja` and everything works. Now I run - `ninja myscript` and it throws errors -\r\n\r\n```\r\n[0/1] Running external command myscript.\r\nTraceback (most recent call last):\r\n  File \"/usr/bin/meson\", line 37, in <module>\r\n    sys.exit(main())\r\n  File \"/usr/bin/meson\", line 34, in main\r\n    return mesonmain.run(sys.argv[1:], launcher)\r\n  File \"/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py\", line 311, in run\r\n    sys.exit(run_script_command(args[1:]))\r\n  File \"/usr/lib/python3.6/site-packages/mesonbuild/mesonmain.py\", line 278, in run_script_command\r\n    return cmdfunc(cmdargs)\r\n  File \"/usr/lib/python3.6/site-packages/mesonbuild/scripts/commandrunner.py\", line 60, in run\r\n    pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)\r\n  File \"/usr/lib/python3.6/site-packages/mesonbuild/scripts/commandrunner.py\", line 39, in run_command\r\n    return subprocess.Popen(command_array, env=child_env, cwd=cwd)\r\n  File \"/usr/lib/python3.6/subprocess.py\", line 709, in __init__\r\n    restore_signals, start_new_session)\r\n  File \"/usr/lib/python3.6/subprocess.py\", line 1344, in _execute_child\r\n    raise child_exception_type(errno_num, err_msg, err_filename)\r\nPermissionError: [Errno 13] Permission denied: '/home/agauniyal/temp/scripts/script.sh'\r\nFAILED: meson-myscript \r\n/usr/bin/python /usr/bin/meson --internal commandrunner /home/agauniyal/temp/ /home/agauniyal/temp/build '' /usr/bin/python /usr/bin/meson /home/agauniyal/temp/scripts/script.sh\r\nninja: build stopped: subcommand failed.\r\n```\n", "before_files": [{"content": "# Copyright 2014 The Meson development team\n\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n#     http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"This program is a wrapper to run external commands. It determines\nwhat to run, sets up the environment and executes the command.\"\"\"\n\nimport sys, os, subprocess, shutil, shlex\n\ndef run_command(source_dir, build_dir, subdir, meson_command, command, arguments):\n    env = {'MESON_SOURCE_ROOT': source_dir,\n           'MESON_BUILD_ROOT': build_dir,\n           'MESON_SUBDIR': subdir,\n           'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in meson_command + ['introspect']]),\n           }\n    cwd = os.path.join(source_dir, subdir)\n    child_env = os.environ.copy()\n    child_env.update(env)\n\n    # Is the command an executable in path?\n    exe = shutil.which(command)\n    if exe is not None:\n        command_array = [exe] + arguments\n        return subprocess.Popen(command_array, env=child_env, cwd=cwd)\n    # No? Maybe it is a script in the source tree.\n    fullpath = os.path.join(source_dir, subdir, command)\n    command_array = [fullpath] + arguments\n    try:\n        return subprocess.Popen(command_array, env=child_env, cwd=cwd)\n    except FileNotFoundError:\n        print('Could not execute command \"%s\".' % command)\n        sys.exit(1)\n\ndef run(args):\n    if len(args) < 4:\n        print('commandrunner.py <source dir> <build dir> <subdir> <command> [arguments]')\n        return 1\n    src_dir = args[0]\n    build_dir = args[1]\n    subdir = args[2]\n    meson_command = args[3]\n    if 'python' in meson_command: # Hack.\n        meson_command = [meson_command, args[4]]\n        command = args[5]\n        arguments = args[6:]\n    else:\n        meson_command = [meson_command]\n        command = args[4]\n        arguments = args[5:]\n    pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)\n    pc.wait()\n    return pc.returncode\n\nif __name__ == '__main__':\n    sys.exit(run(sys.argv[1:]))\n", "path": "mesonbuild/scripts/commandrunner.py"}], "after_files": [{"content": "# Copyright 2014 The Meson development team\n\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n\n#     http://www.apache.org/licenses/LICENSE-2.0\n\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"This program is a wrapper to run external commands. It determines\nwhat to run, sets up the environment and executes the command.\"\"\"\n\nimport sys, os, subprocess, shutil, shlex\n\ndef run_command(source_dir, build_dir, subdir, meson_command, command, arguments):\n    env = {'MESON_SOURCE_ROOT': source_dir,\n           'MESON_BUILD_ROOT': build_dir,\n           'MESON_SUBDIR': subdir,\n           'MESONINTROSPECT': ' '.join([shlex.quote(x) for x in meson_command + ['introspect']]),\n           }\n    cwd = os.path.join(source_dir, subdir)\n    child_env = os.environ.copy()\n    child_env.update(env)\n\n    # Is the command an executable in path?\n    exe = shutil.which(command)\n    if exe is not None:\n        command_array = [exe] + arguments\n        return subprocess.Popen(command_array, env=child_env, cwd=cwd)\n    # No? Maybe it is a script in the source tree.\n    fullpath = os.path.join(source_dir, subdir, command)\n    command_array = [fullpath] + arguments\n    try:\n        return subprocess.Popen(command_array, env=child_env, cwd=cwd)\n    except FileNotFoundError:\n        print('Could not execute command \"%s\". File not found.' % command)\n        sys.exit(1)\n    except PermissionError:\n        print('Could not execute command \"%s\". File not executable.' % command)\n        sys.exit(1)\n\ndef run(args):\n    if len(args) < 4:\n        print('commandrunner.py <source dir> <build dir> <subdir> <command> [arguments]')\n        return 1\n    src_dir = args[0]\n    build_dir = args[1]\n    subdir = args[2]\n    meson_command = args[3]\n    if 'python' in meson_command: # Hack.\n        meson_command = [meson_command, args[4]]\n        command = args[5]\n        arguments = args[6:]\n    else:\n        meson_command = [meson_command]\n        command = args[4]\n        arguments = args[5:]\n    pc = run_command(src_dir, build_dir, subdir, meson_command, command, arguments)\n    pc.wait()\n    return pc.returncode\n\nif __name__ == '__main__':\n    sys.exit(run(sys.argv[1:]))\n", "path": "mesonbuild/scripts/commandrunner.py"}]}