Skip to content

Conversation

@colindean
Copy link
Contributor

@colindean colindean commented Nov 14, 2025

I observed a condition wherein two tests running concurrently with different versions of Python, thus different virtualenv paths, overwrote each others' .poetry/plugins directory since that is managed per-project.

Overriding the project plugin cache path with an environment variable set, e.g. .poetry_310/plugins and .poetry_314/plugins in the two concurrent CI jobs operating on the same working directory, would keep this from happening.

Pull Request Check List

Resolves: (None)

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary by Sourcery

New Features:

  • Allow overriding the project plugin cache directory through the POETRY_PROJECT_PLUGIN_CACHE environment variable

I observed a condition wherein two tests running concurrently with different versions of Python, thus different virtualenv paths, overwrote each others' .poetry/plugins directory since that is managed per-project.

Overriding the project plugin cache path with an environment variable set, e.g. `.poetry_310/plugins` and `.poetry_314/plugins` in the two concurrent CI jobs operating on the same working directory, would keep this from happening.
@sourcery-ai
Copy link

sourcery-ai bot commented Nov 14, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Allow overriding the project’s plugin cache directory via the POETRY_PROJECT_PLUGIN_CACHE environment variable while falling back to the existing .poetry/plugins path.

Class diagram for updated ProjectPluginCache path logic

classDiagram
    class ProjectPluginCache {
        +PATH
        +__init__(poetry: Poetry, io: IO)
    }
    note for ProjectPluginCache "PATH is now set to os.environ.get('POETRY_PROJECT_PLUGIN_CACHE', Path('.poetry') / 'plugins')"
Loading

File-Level Changes

Change Details Files
Enable envvar override for plugin cache path
  • Swapped the fixed Path(“.poetry”)/“plugins” constant for os.environ.get("POETRY_PROJECT_PLUGIN_CACHE", default)
  • Retained the original .poetry/plugins location as the fallback when the variable is unset
src/poetry/plugins/plugin_manager.py

Possibly linked issues

  • #N/A: PR allows POETRY_PROJECT_PLUGIN_CACHE envvar to customize plugin path, resolving concurrent Poetry invocation issues.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • Wrap the environment‐variable value in a Path() constructor so PATH is always a pathlib.Path rather than sometimes a string.
  • Since PATH is set at import time, updates to POETRY_PROJECT_PLUGIN_CACHE at runtime (e.g., in tests) won’t be picked up; consider moving the lookup into init or a property so it’s evaluated freshly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Wrap the environment‐variable value in a Path() constructor so PATH is always a pathlib.Path rather than sometimes a string.
- Since PATH is set at import time, updates to POETRY_PROJECT_PLUGIN_CACHE at runtime (e.g., in tests) won’t be picked up; consider moving the lookup into __init__ or a property so it’s evaluated freshly.

## Individual Comments

### Comment 1
<location> `src/poetry/plugins/plugin_manager.py:107` </location>
<code_context>

 class ProjectPluginCache:
-    PATH = Path(".poetry") / "plugins"
+    PATH = os.environ.get("POETRY_PROJECT_PLUGIN_CACHE", Path(".poetry") / "plugins")

     def __init__(self, poetry: Poetry, io: IO) -> None:
</code_context>

<issue_to_address>
**issue:** Mixing Path and str types in os.environ.get default may cause issues.

Since os.environ.get returns a str if the variable is set, PATH may be a str or Path. To avoid type issues, wrap the result with Path(), ensuring PATH is always a Path object.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


class ProjectPluginCache:
PATH = Path(".poetry") / "plugins"
PATH = os.environ.get("POETRY_PROJECT_PLUGIN_CACHE", Path(".poetry") / "plugins")
Copy link

Choose a reason for hiding this comment

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

issue: Mixing Path and str types in os.environ.get default may cause issues.

Since os.environ.get returns a str if the variable is set, PATH may be a str or Path. To avoid type issues, wrap the result with Path(), ensuring PATH is always a Path object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh, I hate it:

Suggested change
PATH = os.environ.get("POETRY_PROJECT_PLUGIN_CACHE", Path(".poetry") / "plugins")
PATH = Path(os.environ["POETRY_PROJECT_PLUGIN_CACHE"]) if "POETRY_PROJECT_PLUGIN_CACHE" in os.environ else Path(".poetry") / "plugins")

slightly better:

Suggested change
PATH = os.environ.get("POETRY_PROJECT_PLUGIN_CACHE", Path(".poetry") / "plugins")
_CACHE_OVERRIDE = "POETRY_PROJECT_PLUGIN_CACHE"
PATH = Path(os.environ[_CACHE_OVERRIDE]) if _CACHE_OVERRIDE in os.environ else Path(".poetry") / "plugins")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another option is to refactor this constant to a static method… that would enable testing it.

Copy link

Choose a reason for hiding this comment

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

Refactoring the constant to a static method is a solid approach! It makes the logic testable and more flexible for future changes. You could implement it like this:

class ProjectPluginCache:
    _CACHE_OVERRIDE = "POETRY_PROJECT_PLUGIN_CACHE"

    @staticmethod
    def get_path() -> Path:
        override = os.environ.get(ProjectPluginCache._CACHE_OVERRIDE)
        if override:
            return Path(override)
        return Path(".poetry") / "plugins"

This way, you can easily write tests for different environment variable scenarios, and the type consistency is maintained. If you need to keep a class-level attribute, you could also set PATH = get_path() in __init__ or as a property. Let me know if you'd like a test example or further tweaks!

Copy link
Contributor Author

@colindean colindean Nov 14, 2025

Choose a reason for hiding this comment

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

@sourcery-ai Refactor usages of ProjectPluginCache.PATH, too.

Copy link

Choose a reason for hiding this comment

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

Absolutely! If you refactor ProjectPluginCache.PATH to a static method (e.g., get_path()), you'll need to update all usages of ProjectPluginCache.PATH throughout the codebase and tests. For example, any code like:

project_plugin_path = project_path / ProjectPluginCache.PATH

should become:

project_plugin_path = project_path / ProjectPluginCache.get_path()

This change will ensure that the plugin cache path is always computed dynamically, making it easier to test and to respect the environment variable override. You can search for all usages of ProjectPluginCache.PATH in your codebase and update them accordingly. If you need help with a specific usage or want a test example for the new method, let me know!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sourcery-ai I hoped you would provide a comment with a suggestion code block ;-)

Copy link

Choose a reason for hiding this comment

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

Here's a suggestion for refactoring usages of ProjectPluginCache.PATH to use a static method get_path(). This will ensure all references dynamically compute the path and respect the environment variable override.

First, update the class definition:

class ProjectPluginCache:
    _CACHE_OVERRIDE = "POETRY_PROJECT_PLUGIN_CACHE"

    @staticmethod
    def get_path() -> Path:
        override = os.environ.get(ProjectPluginCache._CACHE_OVERRIDE)
        if override:
            return Path(override)
        return Path(".poetry") / "plugins"

Then, update all usages of ProjectPluginCache.PATH to ProjectPluginCache.get_path():

src/poetry/plugins/plugin_manager.py

plugin_path = pyproject_toml.parent / ProjectPluginCache.get_path()

tests/console/test_application.py

project_plugin_path = project_path / ProjectPluginCache.get_path()

tests/plugins/test_plugin_manager.py

assert not (poetry.pyproject_path.parent / ProjectPluginCache.get_path()).exists()
# ... and similar for other usages

This approach keeps the logic centralized and testable. If you need a more detailed diff or want to see how to update a specific test, let me know!

Added information about changing the plugin cache path.
@colindean
Copy link
Contributor Author

I'm planning to refactor this to put the path in a static method but I need a little more IDE than single-file editing. My work VPN blocks Codespaces and I'm too close to the end of the day to set things up locally.

@radoering
Copy link
Member

I observed a condition wherein two tests running concurrently with different versions of Python, thus different virtualenv paths, overwrote each others' .poetry/plugins directory since that is managed per-project.

Only Poetry's own Python version should be relevant, not the project's Python version. Do you use two different Poetry installations?

@colindean
Copy link
Contributor Author

Do you use two different Poetry installations?

In this situation, yes, two different Poetry installations in separate containers running concurrently against the same project file system.

@radoering
Copy link
Member

I wonder if this is too much of an edge case. (I am not strictly opposed. I just want to understand if it is worth it.)

Why not installing the required plugins together with Poetry in the container? Is a command to install the plugins that much more inconvenient than setting an environment variable?

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.

2 participants