Skip to content

Poetry package listing fails for nested/non-root pyproject.toml projects (poetry show runs with wrong cwd) #1779

Description

Environment

  • vscode-python-envs version: 1.36.0
  • OS: Linux (devcontainer, linux-arm64)
  • Poetry: 2.4.3

Description

When a Poetry project is registered via python-envs.pythonProjects but is not at the workspace root (e.g. a monorepo with scripts/pyproject.toml), the "Manage Packages" package listing fails with:

poetry: Poetry could not find a pyproject.toml file in <extension host cwd> or its parents

Root cause (found in source)

PoetryPackageManager.getDirectPackageNames() (src/managers/poetry/poetryPackageManager.ts) builds PoetryShowTopLevelCommand without ever passing a cwd:

const showTopLevelCmd = new PoetryShowTopLevelCommand({
    pythonExecutable: poetry,
    log: this.log,
});

This means poetry show --no-ansi --top-level always inherits the extension host process's own cwd (in a remote/devcontainer setup this is the vscode-server install directory), not the project directory. This is confirmed intentional by the existing unit test poetryPackageManager.unit.test.ts: "direct package listing inherits the process working directory" asserts runPoetryStub.firstCall.args[1] === undefined.

fetchPackagesFromTool() (used for plain poetry show --no-ansi) does compute a cwd via getPoetryCwd(), but that logic only reliably resolves when api.getPythonProjects() returns exactly one project. Since VS Code always implicitly adds the workspace root as a project (PythonProjectManagerImpl.getInitialProjects()), any repo with a registered non-root Poetry project (e.g. scripts/) ends up with 2+ projects, forcing the "match by environment identity" branch — which can return an empty matchingDirectories set (and thus undefined cwd) depending on how the workspace-root's own resolved environment compares.

Repro

  1. Monorepo with pyproject.toml only in a subfolder, e.g. scripts/pyproject.toml.
  2. Add to .vscode/settings.json:
    "python-envs.pythonProjects": [
      { "path": "scripts", "envManager": "ms-python.python:poetry", "packageManager": "ms-python.python:poetry" }
    ]
  3. Open "Manage Packages" for the scripts project's Poetry environment.

Expected

poetry show commands run with cwd set to the registered project directory (scripts/).

Actual

Both poetry show --no-ansi --top-level and poetry show --no-ansi fail with "could not find a pyproject.toml", repeating every time packageWatchers triggers an auto-refresh.

Suggested fix

  • Pass cwd (resolved from the project associated with environment, e.g. via api.getPythonProject(environment.environmentPath) or similar) into PoetryShowTopLevelCommand in getDirectPackageNames().
  • In getPoetryCwd(), prefer resolving cwd from the actual project that owns the environment (e.g. via reverse lookup by environment path) rather than only matching by envId.id equality across all projects.

Activity

  1. hansmbakker commented on Sep 11, 2026

    @hansmbakker
    Author

    Also: the ${workspaceFolder} variable is not resolved in the path property of python-envs.pythonProjects.

    ${workspaceFolder}/scripts does not result in an absolute path, but in a verbatim ${workspaceFolder}/scripts entry in the Python Projects pane.

    "python-envs.pythonProjects": [
        {
          "path": "${workspaceFolder}/scripts",
          "envManager": "ms-python.python:poetry",
          "packageManager": "ms-python.python:poetry"
        }
      ]
    
  2. changed the title [-]Poetry package listing fails for nested/non-root pyproject.toml projects (poetry show runs with wrong cwd)[/-] [+]Poetry package listing fails for nested/non-root pyproject.toml projects (`poetry show` runs with wrong cwd)[/+] on Sep 11, 2026
  3. eleanorjboyd commented on Sep 23, 2026

    @eleanorjboyd
    Member

    Longer-term suggestion: carry the Python project URI explicitly through package operations rather than trying to infer the project solely from the environment. The Python Projects view already has the project URI (projectView.ts), but the PackageManager methods currently receive only a PythonEnvironment. An environment can live outside its project or be shared by multiple projects, so getPythonProject(environment.environmentPath) and reverse-matching by environment ID cannot always identify the Poetry project whose pyproject.toml should be used.

    Consider an optional project/scope URI in the relevant package-operation context (preserving environment-only callers), propagated through package listing, refresh, direct-dependency lookup, and add/remove. Poetry would run each project-dependent command with cwd set to that project directory. For calls without an explicit project, resolve only when the environment maps to one unambiguous Poetry project; otherwise report the missing/ambiguous context instead of inheriting the extension-host cwd. Cache keys would also need to include project identity where results depend on the project, so two projects sharing an environment do not overwrite each other.

    A regression test with two Poetry projects sharing an external virtualenv would exercise the ambiguity that an environment-only lookup cannot solve. This is a longer-term API design point; the immediate cwd bug can be addressed independently.

  4. added a commit that references this issue on Sep 28, 2026
    7ea97a3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions