Skip to content

Commit

Permalink
cmd/shell: fix incorrect documentation (#9060)
Browse files Browse the repository at this point in the history
Co-authored-by: Bjorn Neergaard <bjorn@neersighted.com>
(cherry picked from commit 342f2a9)
  • Loading branch information
abn authored and github-actions[bot] committed Apr 30, 2024
1 parent d0b14c3 commit 792a276
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 12 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,13 @@ Note that this command has no option.

## shell

The `shell` command spawns a shell,
according to the `$SHELL` environment variable,
within the virtual environment.
If one doesn't exist yet, it will be created.
The shell command spawns a shell within the project's virtual environment.

By default, the current active shell is detected and used. Failing that,
the shell defined via the environment variable `SHELL` (on *nix) or
`COMSPEC` (on Windows) is used.

If a virtual environment does not exist, it will be created.

```bash
poetry shell
Expand All @@ -654,6 +657,11 @@ Note that this command starts a new shell and activates the virtual environment.

As such, `exit` should be used to properly exit the shell and the virtual environment instead of `deactivate`.

{{% note %}}
Poetry internally uses the [Shellingham](https://github.com/sarugaku/shellingham) project to detect current
active shell.
{{% /note %}}

## check

The `check` command validates the content of the `pyproject.toml` file
Expand Down
17 changes: 10 additions & 7 deletions src/poetry/console/commands/shell.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import os
import sys

from os import environ
from typing import TYPE_CHECKING
from typing import cast

Expand All @@ -17,9 +17,12 @@ class ShellCommand(EnvCommand):
name = "shell"
description = "Spawns a shell within the virtual environment."

help = """The <info>shell</> command spawns a shell, according to the
<comment>$SHELL</> environment variable, within the virtual environment.
If one doesn't exist yet, it will be created.
help = f"""The <info>shell</> command spawns a shell within the project's virtual environment.
By default, the current active shell is detected and used. Failing that,
the shell defined via the environment variable <comment>{'COMSPEC' if os.name == 'nt' else 'SHELL'}</> is used.
If a virtual environment does not exist, it will be created.
"""

def handle(self) -> int:
Expand All @@ -41,14 +44,14 @@ def handle(self) -> int:
env = cast("VirtualEnv", env)

# Setting this to avoid spawning unnecessary nested shells
environ["POETRY_ACTIVE"] = "1"
os.environ["POETRY_ACTIVE"] = "1"
shell = Shell.get()
shell.activate(env)
environ.pop("POETRY_ACTIVE")
os.environ.pop("POETRY_ACTIVE")

return 0

def _is_venv_activated(self) -> bool:
return bool(environ.get("POETRY_ACTIVE")) or getattr(
return bool(os.environ.get("POETRY_ACTIVE")) or getattr(
sys, "real_prefix", sys.prefix
) == str(self.env.path)

0 comments on commit 792a276

Please sign in to comment.