Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add --only-root option to install command, excluding all dependencies. #5783

Merged
merged 10 commits into from
Jun 15, 2022
7 changes: 7 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ It's also possible to only install specific dependency groups by using the `only
poetry install --only test,docs
```

To only install the project itself with no dependencies, use the `--only-root` flag.

```bash
poetry install --only-root
chadac marked this conversation as resolved.
Show resolved Hide resolved
```

See [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}}) for more information
about dependency groups.

Expand Down Expand Up @@ -220,6 +226,7 @@ option is used.
* `--without`: The dependency groups to ignore.
* `--with`: The optional dependency groups to include.
* `--only`: The only dependency groups to include.
* `--only-root`: Install only the root project, exclude all dependencies.
* `--default`: Only include the main dependencies. (**Deprecated**)
* `--sync`: Synchronize the environment with the locked packages and the specified groups.
* `--no-root`: Do not install the root package (your project).
Expand Down
9 changes: 9 additions & 0 deletions docs/managing-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ poetry install --only main
```
{{% /note %}}

{{% note %}}
If you want to install the project root, and no other dependencies, you can use
the `--only-root` option.

```bash
poetry install --only-root
```
{{% /note %}}

### Removing dependencies from a group

The [`remove`]({{< relref "cli#remove" >}}) command supports a `--group` option
Expand Down
33 changes: 33 additions & 0 deletions src/poetry/console/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ class InstallCommand(InstallerCommand):
multiple=True,
),
option("all-extras", None, "Install all extra dependencies."),
option(
"only-root",
None,
"Exclude all dependencies.",
flag=True,
multiple=False,
),
]

help = """The <info>install</info> command reads the <comment>poetry.lock</> file from
Expand All @@ -65,6 +72,13 @@ class InstallCommand(InstallerCommand):

_loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"]

@property
def activated_groups(self) -> set[str]:
if self.option("only-root"):
return set()
else:
return super().activated_groups

def handle(self) -> int:
from poetry.core.masonry.utils.module import ModuleOrPackageNotFound

Expand All @@ -82,6 +96,25 @@ def handle(self) -> int:
)
return 1

if self.option("only-root") and any(
self.option(key) for key in {"with", "without", "only"}
):
self.line_error(
"<error>The `<fg=yellow;options=bold>--with</>`,"
" `<fg=yellow;options=bold>--without</>` and"
" `<fg=yellow;options=bold>--only</>` options cannot be used with"
" the `<fg=yellow;options=bold>--only-root</>`"
" option.</error>"
)
return 1

if self.option("only-root") and self.option("no-root"):
self.line_error(
"<error>You cannot specify `<fg=yellow;options=bold>--no-root</>`"
" when using `<fg=yellow;options=bold>--only-root</>`.</error>"
)
return 1

if self.option("all-extras"):
extras = list(self.poetry.package.extras.keys())
else:
Expand Down
9 changes: 8 additions & 1 deletion tests/console/commands/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def tester(
("options", "groups"),
[
("", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--only-root", set()),
(f"--only {MAIN_GROUP}", {MAIN_GROUP}),
("--only foo", {"foo"}),
("--only foo,bar", {"foo", "bar"}),
Expand Down Expand Up @@ -111,7 +112,13 @@ def test_group_options_are_passed_to_the_installer(
if not with_root:
options = f"--no-root {options}"

tester.execute(options)
status_code = tester.execute(options)

if options == "--no-root --only-root":
assert status_code == 1
return
else:
assert status_code == 0

package_groups = set(tester.command.poetry.package._dependency_groups.keys())
installer_groups = set(tester.command.installer._groups)
Expand Down