Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
rc-mattschwager opened this issue Oct 14, 2022 · 3 comments · Fixed by #6824
Closed
4 tasks done

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

rc-mattschwager opened this issue Oct 14, 2022 · 3 comments · Fixed by #6824
Labels
kind/bug Something isn't working as expected status/triage This issue needs to be triaged

Comments

@rc-mattschwager
Copy link
Contributor

  • 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.

@rc-mattschwager rc-mattschwager added kind/bug Something isn't working as expected status/triage This issue needs to be triaged labels Oct 14, 2022
@dimbleby
Copy link
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

@dimbleby
Copy link
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.

Copy link

github-actions bot commented Mar 1, 2024

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/bug Something isn't working as expected status/triage This issue needs to be triaged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants