Skip to content

fix: execute installed file scripts with poetry run - #11108

Closed
Mohamedajab wants to merge 3 commits into
python-poetry:mainfrom
Mohamedajab:fix/run-file-scripts
Closed

Mohamedajab wants to merge 3 commits into
python-poetry:mainfrom
Mohamedajab:fix/run-file-scripts

Conversation

@Mohamedajab

Copy link
Copy Markdown
Contributor

Resolves: #11090

File scripts declared with type = "file" were passed to the console-entry-point code path, which expects a callable field and raised KeyError. Detect file scripts before that conversion, preserve their installed name on Windows instead of adding a .cmd suffix, and execute the installed file directly with its arguments.

  • Added tests for changed code.
  • Updated documentation for changed code. (N/A: this restores documented behavior.)

Tested with:

  • python -m pytest tests/console/commands/test_run.py -q (20 passed)
  • pre-commit run --files src/poetry/console/commands/run.py tests/console/commands/test_run.py
  • mypy src/poetry/console/commands/run.py tests/console/commands/test_run.py

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/poetry/console/commands/run.py" line_range="74-75" />
<code_context>
             self._warning_not_installed_script(args[0])

+        if is_file_script:
+            return self.env.execute(*args)
+
         if isinstance(script, dict):
</code_context>
<issue_to_address>
**issue (bug_risk):** When a file script is declared but not installed, `run_script` emits the not-installed warning and then calls `self.env.execute(*args)` with the bare script name. That raises `FileNotFoundError` instead of returning the command-not-found error handled by `handle`, so `poetry run <file-script>` produces an uncaught exception.

**Triggers:** When the project has a `type = "file"` script but `poetry install` has not installed its target, or installation skipped the script because its reference is invalid.

**Suggested fix:** Handle the missing-installed-file case explicitly before calling `self.env.execute`, or route the execution through the same `FileNotFoundError` handling used for ordinary commands.

```suggestion
        if is_file_script:
            try:
                return self.env.execute(*args)
            except FileNotFoundError:
                self.line_error(f"<error>Command not found: <c1>{args[0]}</c1></error>")
                return 1
```
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/poetry/console/commands/run.py:75


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/poetry/console/commands/run.py Outdated
sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Sep 27, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sourcery assessment

Approved.

@dimbleby

Copy link
Copy Markdown
Contributor

Explain why not #11092

@sourcery-ai
sourcery-ai Bot dismissed their stale review September 30, 2026 21:13

Sourcery withdrew this approval because the latest commits introduced blocking findings.

@Mohamedajab

Copy link
Copy Markdown
Contributor Author

Correct — #11092 predates this PR and addresses the same file-script execution bug with broader integration coverage. There is no benefit in keeping a competing duplicate open, so I am closing this in favor of #11092. Thanks for pointing it out.

@Mohamedajab Mohamedajab closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make file scripts executable via poetry run

2 participants