poetry icon indicating copy to clipboard operation
poetry copied to clipboard

Fix: indicate location of toml file in question when raising a TOMLDecodeError

Open JaviMaligno opened this issue 2 weeks ago • 1 comments

Fixes #10270

Summary by Sourcery

Bug Fixes:

  • Include the path to the problematic TOML configuration file in the runtime error raised when local configuration parsing fails.

JaviMaligno avatar Dec 09 '25 14:12 JaviMaligno

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Wraps reading of the local TOML configuration in a try/except so that TOML parsing errors are re-raised as PoetryRuntimeError with the path of the problematic config file included in the message.

Sequence diagram for error handling when loading local TOML configuration

sequenceDiagram
    actor Developer
    participant CLI as PoetryCLI
    participant Factory as Factory
    participant Config as Config
    participant LocalFile as LocalConfigFile
    participant TOML as TOMLParser

    Developer->>CLI: run_command()
    CLI->>Factory: create_poetry(io)
    Factory->>LocalFile: path
    Factory->>LocalFile: read()
    LocalFile->>TOML: parse_toml(content)
    alt TOML parsing succeeds
        TOML-->>LocalFile: parsed_data
        LocalFile-->>Factory: parsed_data
        Factory->>Config: merge(parsed_data)
        Config-->>Factory: merged_config
        Factory-->>CLI: poetry_instance
    else TOML parsing fails
        TOML-->>LocalFile: TOMLError
        LocalFile-->>Factory: TOMLError
        Factory->>Factory: catch TOMLError
        Factory-->>CLI: raise PoetryRuntimeError("Invalid TOML configuration file <path>: <TOMLError>")
        CLI-->>Developer: display error with file path
    end

File-Level Changes

Change Details Files
Handle TOML parsing errors when loading the local configuration file and surface them as PoetryRuntimeError including the config path.
  • Wrap the call that merges the local TOML configuration into a try/except block catching TOMLError.
  • On TOMLError, raise PoetryRuntimeError with a message that includes the invalid configuration file path and the original error message while preserving the exception cause.
src/poetry/factory.py

Assessment against linked issues

Issue Objective Addressed Explanation
https://github.com/python-poetry/poetry/issues/10270 When a TOMLDecodeError occurs while loading any TOML file relevant to Poetry (e.g., the main project pyproject.toml, path dependency pyproject.toml files, or Poetry configuration TOML files), the error message should indicate which TOML file path is invalid. The PR only wraps config.merge(local_config_file.read()) in factory.py and rethrows a PoetryRuntimeError that includes the path of local_config_file. This improves error messages for invalid TOML in the Poetry configuration file, but it does not change the TOML loading path shown in the issue stack trace (which goes through poetry/core/pyproject/toml.py and tomli/_parser.py for pyproject.toml files and path dependencies). Therefore, TOMLDecodeError messages for invalid pyproject.toml files—particularly in path dependencies, which are the main focus of the issue—still will not indicate which TOML file is at fault.

Possibly linked issues

  • #10270: PR adds explicit TOML file path to error messages, directly addressing the request to show offending TOML file.
  • #10270: PR changes TOML error handling to include the path of the invalid configuration file, addressing clearer-location request.

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 Dec 09 '25 14:12 sourcery-ai[bot]

The title says something about the location of the toml file but the code does not any such information if I do not miss anything. I do not think this fixes #10270.

radoering avatar Dec 12 '25 15:12 radoering

Thanks for the feedback @radoering! You're right that the error message was missing the file location.

I've updated the code to include self.project_directory in the error message:

except TOMLError as e:
    raise PoetryRuntimeError(
        f"Problem processing TOML configuration in {self.project_directory}: {e}"
    ) from e

Now when users encounter a TOML parsing error, they'll see which project directory has the issue, making it easier to identify the problematic file.

JaviMaligno avatar Dec 12 '25 16:12 JaviMaligno

I think this still does not solve the issue described in #10270:

The issue was in a toml file for one of this project's path dependencies.

However, there already has been such a fix in https://github.com/python-poetry/poetry-core/pull/734. This fix is included in Poetry 2.0. The issue was reported with Poetry 1.7.1. Actually, I think the issue has already been fixed and your change does not add any additional information. I closed the issue and will close this PR. Thank you anyway for the effort.

radoering avatar Dec 12 '25 16:12 radoering