Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

cmd/shell: fix incorrect documentation #9060

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,10 +637,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 @@ -650,6 +653,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)
Loading