diff --git a/docs/cli.md b/docs/cli.md index 98eac1bcfac..4c29035c80b 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -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 +``` + See [Dependency groups]({{< relref "managing-dependencies#dependency-groups" >}}) for more information about dependency groups. @@ -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). diff --git a/docs/managing-dependencies.md b/docs/managing-dependencies.md index 5d0046e17c2..33d44cbcd94 100644 --- a/docs/managing-dependencies.md +++ b/docs/managing-dependencies.md @@ -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 diff --git a/src/poetry/console/commands/install.py b/src/poetry/console/commands/install.py index af71a12c180..c65a3225202 100644 --- a/src/poetry/console/commands/install.py +++ b/src/poetry/console/commands/install.py @@ -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 install command reads the poetry.lock file from @@ -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 @@ -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( + "The `--with`," + " `--without` and" + " `--only` options cannot be used with" + " the `--only-root`" + " option." + ) + return 1 + + if self.option("only-root") and self.option("no-root"): + self.line_error( + "You cannot specify `--no-root`" + " when using `--only-root`." + ) + return 1 + if self.option("all-extras"): extras = list(self.poetry.package.extras.keys()) else: diff --git a/tests/console/commands/test_install.py b/tests/console/commands/test_install.py index a931c448d58..99182c61a90 100644 --- a/tests/console/commands/test_install.py +++ b/tests/console/commands/test_install.py @@ -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"}), @@ -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)