Skip to content

Commit

Permalink
cmd/shell: fix incorrect documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Feb 28, 2024
1 parent b7bc5ce commit 84dc2b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
15 changes: 11 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,12 @@ 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 `SHELL` (on posix) or `COMSPEC` (on windows) is used.

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

```bash
poetry shell
Expand All @@ -650,6 +652,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 <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 84dc2b8

Please sign in to comment.