We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Exit code relay fails when run command is a script
<!--
    Hi there! Thank you for submitting a bug report!

    Before you submit your issue, please review and follow the instructions at
    https://python-poetry.org/docs/contributing/#reporting-bugs. Fully fill out the template below; insufficient
    information or bad reproduction instructions will impair the ability of others to help you.
-->

<!-- All the below information must be provided for others to understand and help with your issue. -->
- **Poetry version**: `1.2.2`
- **Python version**: `3.10.6`
- **OS version and name**: macOS 12.5.1
- **pyproject.toml**: Relevant details below

```
[tool.poetry.scripts]
toolname = "toolname.__main__:main"
```

<!-- All the below steps should be completed before submitting your issue. Checked checkbox should look like this: [x] -->
- [x] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) stable Poetry version, installed using a recommended method.
- [x] I have searched the [issues](https://github.com/python-poetry/poetry/issues) of this repo and believe that this is not a duplicate.
- [x] I have consulted the [FAQ](https://python-poetry.org/docs/faq/) and [blog](https://python-poetry.org/blog/) for any relevant entries or release notes.
- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option) and have included the output below.

## Issue
<!-- Now feel free to write your issue, and please be as descriptive as possible! Make sure to include detailed reproduction steps. -->
<!-- Thanks again 🙌 ❤ -->

Hi there,

When using `poetry run` to run a script defined in the [`scripts`](https://python-poetry.org/docs/pyproject/#scripts) section of `pyproject.toml` the exit code of the script is not relayed as the final exit code of `poetry` itself. I originally noticed https://github.com/python-poetry/poetry/issues/5773 and https://github.com/python-poetry/poetry/issues/2369 and was wondering why it wasn't working for me.

Consider the following cases:

```
$ poetry run python -m toolname --version
0.5.0
$ echo $?
1
```

```
$ poetry run toolname --version
0.5.0
$ echo $?
0
```

It looks like https://github.com/python-poetry/poetry/pull/4456 did not fix https://github.com/python-poetry/poetry/issues/2369 because these are different execution paths. Looking at [`commands/run.py`](https://github.com/python-poetry/poetry/blob/16046d9ac9b72a49e1bc4618fb686695cc64821c/src/poetry/console/commands/run.py#L61), running a script (as defined in `pyproject.toml`) is a different execution path than running non-scripts. As mentioned in https://github.com/python-poetry/poetry/issues/2369, I believe the fix would be:

`f"import_module('{module}').{callable_}()"` -> `f"sys.exit(import_module('{module}').{callable_}())"`

I made this change locally and it fixed this issue for me.

Alternatively, I believe another fix would be removing the `RunCommand.run_script` special-case code. Since `poetry` knows where the script is (`$VIRTUALENV/bin/`), it could include this directory in the `$PATH` when executing the command. `poetry` would then execute the script generated by [`builders/editable.py`](https://github.com/python-poetry/poetry/blob/master/src/poetry/masonry/builders/editable.py#L31).

There may be reasons these two code paths are distinct that I don't understand, but if they can be unified to a single path that would ease maintenance burden and help alleviate situations like this where one path is updated but the other is forgotten.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/poetry/console/commands/run.py`
Content:
```
1 from __future__ import annotations
2 
3 from typing import TYPE_CHECKING
4 
5 from cleo.helpers import argument
6 
7 from poetry.console.commands.env_command import EnvCommand
8 
9 
10 if TYPE_CHECKING:
11     from poetry.core.masonry.utils.module import Module
12 
13 
14 class RunCommand(EnvCommand):
15     name = "run"
16     description = "Runs a command in the appropriate environment."
17 
18     arguments = [
19         argument("args", "The command and arguments/options to run.", multiple=True)
20     ]
21 
22     def handle(self) -> int:
23         args = self.argument("args")
24         script = args[0]
25         scripts = self.poetry.local_config.get("scripts")
26 
27         if scripts and script in scripts:
28             return self.run_script(scripts[script], args)
29 
30         try:
31             return self.env.execute(*args)
32         except FileNotFoundError:
33             self.line_error(f"<error>Command not found: <c1>{script}</c1></error>")
34             return 1
35 
36     @property
37     def _module(self) -> Module:
38         from poetry.core.masonry.utils.module import Module
39 
40         poetry = self.poetry
41         package = poetry.package
42         path = poetry.file.parent
43         module = Module(package.name, path.as_posix(), package.packages)
44 
45         return module
46 
47     def run_script(self, script: str | dict[str, str], args: str) -> int:
48         if isinstance(script, dict):
49             script = script["callable"]
50 
51         module, callable_ = script.split(":")
52 
53         src_in_sys_path = "sys.path.append('src'); " if self._module.is_in_src() else ""
54 
55         cmd = ["python", "-c"]
56 
57         cmd += [
58             "import sys; "
59             "from importlib import import_module; "
60             f"sys.argv = {args!r}; {src_in_sys_path}"
61             f"import_module('{module}').{callable_}()"
62         ]
63 
64         return self.env.execute(*cmd)
65
```
--- 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/src/poetry/console/commands/run.py b/src/poetry/console/commands/run.py
--- a/src/poetry/console/commands/run.py
+++ b/src/poetry/console/commands/run.py
@@ -58,7 +58,7 @@
             "import sys; "
             "from importlib import import_module; "
             f"sys.argv = {args!r}; {src_in_sys_path}"
-            f"import_module('{module}').{callable_}()"
+            f"sys.exit(import_module('{module}').{callable_}())"
         ]
 
         return self.env.execute(*cmd)

{"golden_diff": "diff --git a/src/poetry/console/commands/run.py b/src/poetry/console/commands/run.py\n--- a/src/poetry/console/commands/run.py\n+++ b/src/poetry/console/commands/run.py\n@@ -58,7 +58,7 @@\n             \"import sys; \"\n             \"from importlib import import_module; \"\n             f\"sys.argv = {args!r}; {src_in_sys_path}\"\n-            f\"import_module('{module}').{callable_}()\"\n+            f\"sys.exit(import_module('{module}').{callable_}())\"\n         ]\n \n         return self.env.execute(*cmd)\n", "issue": "Exit code relay fails when run command is a script\n<!--\r\n    Hi there! Thank you for submitting a bug report!\r\n\r\n    Before you submit your issue, please review and follow the instructions at\r\n    https://python-poetry.org/docs/contributing/#reporting-bugs. Fully fill out the template below; insufficient\r\n    information or bad reproduction instructions will impair the ability of others to help you.\r\n-->\r\n\r\n<!-- All the below information must be provided for others to understand and help with your issue. -->\r\n- **Poetry version**: `1.2.2`\r\n- **Python version**: `3.10.6`\r\n- **OS version and name**: macOS 12.5.1\r\n- **pyproject.toml**: Relevant details below\r\n\r\n```\r\n[tool.poetry.scripts]\r\ntoolname = \"toolname.__main__:main\"\r\n```\r\n\r\n<!-- All the below steps should be completed before submitting your issue. Checked checkbox should look like this: [x] -->\r\n- [x] I am on the [latest](https://github.com/python-poetry/poetry/releases/latest) stable Poetry version, installed using a recommended method.\r\n- [x] I have searched the [issues](https://github.com/python-poetry/poetry/issues) of this repo and believe that this is not a duplicate.\r\n- [x] I have consulted the [FAQ](https://python-poetry.org/docs/faq/) and [blog](https://python-poetry.org/blog/) for any relevant entries or release notes.\r\n- [x] If an exception occurs when executing a command, I executed it again in debug mode (`-vvv` option) and have included the output below.\r\n\r\n## Issue\r\n<!-- Now feel free to write your issue, and please be as descriptive as possible! Make sure to include detailed reproduction steps. -->\r\n<!-- Thanks again \ud83d\ude4c \u2764 -->\r\n\r\nHi there,\r\n\r\nWhen using `poetry run` to run a script defined in the [`scripts`](https://python-poetry.org/docs/pyproject/#scripts) section of `pyproject.toml` the exit code of the script is not relayed as the final exit code of `poetry` itself. I originally noticed https://github.com/python-poetry/poetry/issues/5773 and https://github.com/python-poetry/poetry/issues/2369 and was wondering why it wasn't working for me.\r\n\r\nConsider the following cases:\r\n\r\n```\r\n$ poetry run python -m toolname --version\r\n0.5.0\r\n$ echo $?\r\n1\r\n```\r\n\r\n```\r\n$ poetry run toolname --version\r\n0.5.0\r\n$ echo $?\r\n0\r\n```\r\n\r\nIt looks like https://github.com/python-poetry/poetry/pull/4456 did not fix https://github.com/python-poetry/poetry/issues/2369 because these are different execution paths. Looking at [`commands/run.py`](https://github.com/python-poetry/poetry/blob/16046d9ac9b72a49e1bc4618fb686695cc64821c/src/poetry/console/commands/run.py#L61), running a script (as defined in `pyproject.toml`) is a different execution path than running non-scripts. As mentioned in https://github.com/python-poetry/poetry/issues/2369, I believe the fix would be:\r\n\r\n`f\"import_module('{module}').{callable_}()\"` -> `f\"sys.exit(import_module('{module}').{callable_}())\"`\r\n\r\nI made this change locally and it fixed this issue for me.\r\n\r\nAlternatively, I believe another fix would be removing the `RunCommand.run_script` special-case code. Since `poetry` knows where the script is (`$VIRTUALENV/bin/`), it could include this directory in the `$PATH` when executing the command. `poetry` would then execute the script generated by [`builders/editable.py`](https://github.com/python-poetry/poetry/blob/master/src/poetry/masonry/builders/editable.py#L31).\r\n\r\nThere may be reasons these two code paths are distinct that I don't understand, but if they can be unified to a single path that would ease maintenance burden and help alleviate situations like this where one path is updated but the other is forgotten.\n", "before_files": [{"content": "from __future__ import annotations\n\nfrom typing import TYPE_CHECKING\n\nfrom cleo.helpers import argument\n\nfrom poetry.console.commands.env_command import EnvCommand\n\n\nif TYPE_CHECKING:\n    from poetry.core.masonry.utils.module import Module\n\n\nclass RunCommand(EnvCommand):\n    name = \"run\"\n    description = \"Runs a command in the appropriate environment.\"\n\n    arguments = [\n        argument(\"args\", \"The command and arguments/options to run.\", multiple=True)\n    ]\n\n    def handle(self) -> int:\n        args = self.argument(\"args\")\n        script = args[0]\n        scripts = self.poetry.local_config.get(\"scripts\")\n\n        if scripts and script in scripts:\n            return self.run_script(scripts[script], args)\n\n        try:\n            return self.env.execute(*args)\n        except FileNotFoundError:\n            self.line_error(f\"<error>Command not found: <c1>{script}</c1></error>\")\n            return 1\n\n    @property\n    def _module(self) -> Module:\n        from poetry.core.masonry.utils.module import Module\n\n        poetry = self.poetry\n        package = poetry.package\n        path = poetry.file.parent\n        module = Module(package.name, path.as_posix(), package.packages)\n\n        return module\n\n    def run_script(self, script: str | dict[str, str], args: str) -> int:\n        if isinstance(script, dict):\n            script = script[\"callable\"]\n\n        module, callable_ = script.split(\":\")\n\n        src_in_sys_path = \"sys.path.append('src'); \" if self._module.is_in_src() else \"\"\n\n        cmd = [\"python\", \"-c\"]\n\n        cmd += [\n            \"import sys; \"\n            \"from importlib import import_module; \"\n            f\"sys.argv = {args!r}; {src_in_sys_path}\"\n            f\"import_module('{module}').{callable_}()\"\n        ]\n\n        return self.env.execute(*cmd)\n", "path": "src/poetry/console/commands/run.py"}], "after_files": [{"content": "from __future__ import annotations\n\nfrom typing import TYPE_CHECKING\n\nfrom cleo.helpers import argument\n\nfrom poetry.console.commands.env_command import EnvCommand\n\n\nif TYPE_CHECKING:\n    from poetry.core.masonry.utils.module import Module\n\n\nclass RunCommand(EnvCommand):\n    name = \"run\"\n    description = \"Runs a command in the appropriate environment.\"\n\n    arguments = [\n        argument(\"args\", \"The command and arguments/options to run.\", multiple=True)\n    ]\n\n    def handle(self) -> int:\n        args = self.argument(\"args\")\n        script = args[0]\n        scripts = self.poetry.local_config.get(\"scripts\")\n\n        if scripts and script in scripts:\n            return self.run_script(scripts[script], args)\n\n        try:\n            return self.env.execute(*args)\n        except FileNotFoundError:\n            self.line_error(f\"<error>Command not found: <c1>{script}</c1></error>\")\n            return 1\n\n    @property\n    def _module(self) -> Module:\n        from poetry.core.masonry.utils.module import Module\n\n        poetry = self.poetry\n        package = poetry.package\n        path = poetry.file.parent\n        module = Module(package.name, path.as_posix(), package.packages)\n\n        return module\n\n    def run_script(self, script: str | dict[str, str], args: str) -> int:\n        if isinstance(script, dict):\n            script = script[\"callable\"]\n\n        module, callable_ = script.split(\":\")\n\n        src_in_sys_path = \"sys.path.append('src'); \" if self._module.is_in_src() else \"\"\n\n        cmd = [\"python\", \"-c\"]\n\n        cmd += [\n            \"import sys; \"\n            \"from importlib import import_module; \"\n            f\"sys.argv = {args!r}; {src_in_sys_path}\"\n            f\"sys.exit(import_module('{module}').{callable_}())\"\n        ]\n\n        return self.env.execute(*cmd)\n", "path": "src/poetry/console/commands/run.py"}]}