Skip to content

Exit code relay fails when run command is a script #6801

Closed

Description

@rc-mattschwager
  • 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"
  • I am on the latest stable Poetry version, installed using a recommended method.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • I have consulted the FAQ and blog for any relevant entries or release notes.
  • If an exception occurs when executing a command, I executed it again in debug mode (-vvv option) and have included the output below.

Issue

Hi there,

When using poetry run to run a script defined in the 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 #5773 and #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 #4456 did not fix #2369 because these are different execution paths. Looking at commands/run.py, running a script (as defined in pyproject.toml) is a different execution path than running non-scripts. As mentioned in #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.

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.

Activity

  1. dimbleby commented on Oct 14, 2022

    @dimbleby
    Contributor

    works fine for me, can you put together a reproduction eg in a dockerfile?

    $ cat toolname/__main__.py
    #!/usr/bin/env python3
    
    def main():
        import sys
        print("hello")
        sys.exit(4)
    
    
    if __name__ == "__main__":
        main()
    $ poetry run python -m toolname
    hello
    $ echo $?
    4
    $ poetry run toolname
    hello
    $ echo $?
    4
    
  2. dimbleby commented on Oct 14, 2022

    @dimbleby
    Contributor

    oh I see, you don't mean that the exit code is not relayed, you mean that the return code is not relayed, as an exit code.

    (Apologies if this seems pedantic, but this is what confused me)

    I expect an MR would be welcome, either of the approaches that you suggest sounds reasonable.

  3. added a commit that references this issue on Oct 17, 2022
    bc7e2ea
  4. added a commit that references this issue on Nov 2, 2022
    7699c90
  5. added a commit that references this issue on Nov 16, 2022
    ead1fa3
  6. github-actions commented on Mar 1, 2024

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  7. locked as resolved and limited conversation to collaborators on Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugSomething isn't working as expectedstatus/triageThis issue needs to be triaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions