Commit 66328c74 by Chad Crawford Committed by GitHub

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

* Add `--only-root` option to group commands, excluding all dependencies.
* Add documentation for usage.
* Apply suggestions from code review

Co-authored-by: Bjorn Neergaard <bjorn@neersighted.com>
parent 993519fd
...@@ -167,6 +167,12 @@ It's also possible to only install specific dependency groups by using the `only ...@@ -167,6 +167,12 @@ It's also possible to only install specific dependency groups by using the `only
poetry install --only test,docs poetry install --only test,docs
``` ```
To only install the project itself with no dependencies, use the `--only-root` flag.
```bash
poetry install --only-root
```
See [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}}) for more information See [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}}) for more information
about dependency groups. about dependency groups.
...@@ -220,6 +226,7 @@ option is used. ...@@ -220,6 +226,7 @@ option is used.
* `--without`: The dependency groups to ignore. * `--without`: The dependency groups to ignore.
* `--with`: The optional dependency groups to include. * `--with`: The optional dependency groups to include.
* `--only`: The only 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**) * `--default`: Only include the main dependencies. (**Deprecated**)
* `--sync`: Synchronize the environment with the locked packages and the specified groups. * `--sync`: Synchronize the environment with the locked packages and the specified groups.
* `--no-root`: Do not install the root package (your project). * `--no-root`: Do not install the root package (your project).
......
...@@ -158,6 +158,15 @@ poetry install --only main ...@@ -158,6 +158,15 @@ poetry install --only main
``` ```
{{% /note %}} {{% /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 ### Removing dependencies from a group
The [`remove`]({{< relref "cli#remove" >}}) command supports a `--group` option The [`remove`]({{< relref "cli#remove" >}}) command supports a `--group` option
......
...@@ -47,6 +47,13 @@ class InstallCommand(InstallerCommand): ...@@ -47,6 +47,13 @@ class InstallCommand(InstallerCommand):
multiple=True, multiple=True,
), ),
option("all-extras", None, "Install all extra dependencies."), 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 help = """The <info>install</info> command reads the <comment>poetry.lock</> file from
...@@ -65,6 +72,13 @@ dependencies and not including the current project, run the command with the ...@@ -65,6 +72,13 @@ dependencies and not including the current project, run the command with the
_loggers = ["poetry.repositories.pypi_repository", "poetry.inspection.info"] _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: def handle(self) -> int:
from poetry.core.masonry.utils.module import ModuleOrPackageNotFound from poetry.core.masonry.utils.module import ModuleOrPackageNotFound
...@@ -82,6 +96,25 @@ dependencies and not including the current project, run the command with the ...@@ -82,6 +96,25 @@ dependencies and not including the current project, run the command with the
) )
return 1 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"): if self.option("all-extras"):
extras = list(self.poetry.package.extras.keys()) extras = list(self.poetry.package.extras.keys())
else: else:
......
...@@ -73,6 +73,7 @@ def tester( ...@@ -73,6 +73,7 @@ def tester(
("options", "groups"), ("options", "groups"),
[ [
("", {MAIN_GROUP, "foo", "bar", "baz", "bim"}), ("", {MAIN_GROUP, "foo", "bar", "baz", "bim"}),
("--only-root", set()),
(f"--only {MAIN_GROUP}", {MAIN_GROUP}), (f"--only {MAIN_GROUP}", {MAIN_GROUP}),
("--only foo", {"foo"}), ("--only foo", {"foo"}),
("--only foo,bar", {"foo", "bar"}), ("--only foo,bar", {"foo", "bar"}),
...@@ -111,7 +112,13 @@ def test_group_options_are_passed_to_the_installer( ...@@ -111,7 +112,13 @@ def test_group_options_are_passed_to_the_installer(
if not with_root: if not with_root:
options = f"--no-root {options}" 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()) package_groups = set(tester.command.poetry.package._dependency_groups.keys())
installer_groups = set(tester.command.installer._groups) installer_groups = set(tester.command.installer._groups)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment