poetry icon indicating copy to clipboard operation
poetry copied to clipboard

fix: make relative working directory absolute

Open finswimmer opened this issue 4 months ago • 1 comments

Pull Request Check List

Resolves: https://github.com/python-poetry/poetry/issues/10541 Resolves: https://github.com/python-poetry/poetry/issues/10287

  • [x] Added tests for changed code.
  • [ ] Updated documentation for changed code.

Summary by Sourcery

Resolve the application's working directory to an absolute path when a relative --directory option is provided and expose it via a new property, with corresponding test updates.

New Features:

  • Add a working_directory property to expose the internal working directory Path

Enhancements:

  • Call resolve() on the directory path in _configure_global_options to convert relative paths to absolute

Tests:

  • Add a test to verify that passing a relative directory parameter yields an absolute WorkingDirectory
  • Update the existing version command test to assert usage of the application.working_directory property

finswimmer avatar Nov 03 '25 20:11 finswimmer

Reviewer's Guide

Adds a working_directory property to the Application, ensures it is resolved to an absolute path when provided via the --directory option, and updates tests to verify this behavior.

Sequence diagram for resolving working directory to absolute path

sequenceDiagram
participant IO
participant Application
IO->>Application: option("directory")
Application->>Application: ensure_path(option, is_directory=True)
Application->>Application: .resolve() (make absolute)
Application->>Application: set _working_directory

Class diagram for updated Application class

classDiagram
class Application {
  +project_directory: Path
  +working_directory: Path
  +poetry: Poetry
  _project_directory: Path
  _working_directory: Path
  _default_definition(): Definition
  _configure_global_options(io: IO): None
}
Application : +working_directory
Application : +project_directory
Application : +poetry

File-Level Changes

Change Details Files
Expose Application.working_directory via a public property
  • Added a @property working_directory
  • Return self._working_directory in the new property
src/poetry/console/application.py
Resolve the working directory to an absolute path when parsing the --directory option
  • Appended .resolve() to the ensure_path call
  • Assigned the resolved path back to _working_directory
src/poetry/console/application.py
Update and add tests to verify the new working_directory behavior
  • Assert the application instance type and use application.working_directory instead of Path.cwd() in the mock
  • Modify existing test to expect the working_directory property
  • Add a new test for relative --directory parameter yielding an absolute path
tests/console/test_application_global_options.py

Assessment against linked issues

Issue Objective Addressed Explanation
https://github.com/python-poetry/poetry/issues/10541 Ensure that when poetry is run with a relative path via the -C/--directory option, the working directory is resolved to an absolute path before use, so that generated shebangs in installed scripts (e.g., .venv/bin/pytest) use the correct absolute path to the Python interpreter.
https://github.com/python-poetry/poetry/issues/10541 Add tests to verify that passing a relative directory parameter results in the working directory being set to the absolute path, and that this is reflected in output and behavior.

Possibly linked issues

  • #10541: The PR adds .resolve() to make the working directory absolute, fixing the FileNotFoundError when poetry build -C uses a relative path.
  • #10541: The PR makes the internal working directory absolute using .resolve(), which resolves issue 10541 by fixing an underlying path-related problem that caused poetry run to misinterpret arguments after a trailing --.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Nov 03 '25 20:11 sourcery-ai[bot]