1010
1111from build import BuildBackendException
1212from build .env import IsolatedEnv as BaseIsolatedEnv
13+ from poetry .core .packages .dependency_group import DependencyGroup
1314
1415from poetry .utils ._compat import decode
1516from poetry .utils .env import Env
2425
2526 from build import DistributionType
2627 from build import ProjectBuilder
28+ from poetry .core .packages .dependency import Dependency
2729
2830 from poetry .repositories import RepositoryPool
2931
3032
33+ CONSTRAINTS_GROUP_NAME = "constraints"
34+
35+
3136class IsolatedBuildBaseError (Exception ): ...
3237
3338
@@ -111,7 +116,12 @@ def make_extra_environ(self) -> dict[str, str]:
111116 )
112117 }
113118
114- def install (self , requirements : Collection [str ]) -> None :
119+ def install (
120+ self ,
121+ requirements : Collection [str ],
122+ * ,
123+ constraints : list [Dependency ] | None = None ,
124+ ) -> None :
115125 from cleo .io .buffered_io import BufferedIO
116126 from poetry .core .packages .dependency import Dependency
117127 from poetry .core .packages .project_package import ProjectPackage
@@ -136,6 +146,13 @@ def install(self, requirements: Collection[str]) -> None:
136146 # safe as this environment is ephemeral
137147 package .add_dependency (dependency )
138148
149+ if constraints :
150+ constraints_group = DependencyGroup (CONSTRAINTS_GROUP_NAME , optional = True )
151+ for constraint in constraints :
152+ if constraint .marker .validate (env_markers ):
153+ constraints_group .add_dependency (constraint )
154+ package .add_dependency_group (constraints_group )
155+
139156 io = BufferedIO ()
140157
141158 installer = Installer (
@@ -161,6 +178,8 @@ def isolated_builder(
161178 distribution : DistributionType = "wheel" ,
162179 python_executable : Path | None = None ,
163180 pool : RepositoryPool | None = None ,
181+ * ,
182+ build_constraints : list [Dependency ] | None = None ,
164183) -> Iterator [ProjectBuilder ]:
165184 from build import ProjectBuilder
166185 from pyproject_hooks import quiet_subprocess_runner
@@ -196,12 +215,15 @@ def isolated_builder(
196215 )
197216
198217 with redirect_stdout (stdout ):
199- env .install (builder .build_system_requires )
218+ env .install (
219+ builder .build_system_requires , constraints = build_constraints
220+ )
200221
201222 # we repeat the build system requirements to avoid poetry installer from removing them
202223 env .install (
203224 builder .build_system_requires
204- | builder .get_requires_for_build (distribution )
225+ | builder .get_requires_for_build (distribution ),
226+ constraints = build_constraints ,
205227 )
206228
207229 yield builder
0 commit comments