Skip to content

Conversation

@br3ndonland
Copy link
Contributor

Description

Fixes #2152

Dependency groups do not install in Hatch environments where builder = true or dev-mode = false.

The 1.16.0 changelog entry says "any environment now may be used for building." It would therefore be helpful to support dependency groups in any environment.

Changes

Currently, the dependencies_complex property on EnvironmentInterface ignores dependency groups if self.builder or not self.dev_mode.

This PR will adjust the dependencies_complex property on EnvironmentInterface so that it includes dependency groups regardless of self.builder or self.dev_mode.

The corresponding unit test will be updated to include builder and dev_mode in the pyproject.toml environment configuration.

Related

@br3ndonland br3ndonland force-pushed the builder-non-dev-dependency-groups branch from 1eee16a to e05a30d Compare January 5, 2026 23:31
@lwasser
Copy link
Contributor

lwasser commented Jan 7, 2026

Hi there - Thank you so much for working on this fix!!

I'm testing this PR, and it still seems I need builder=True in the environment declaration.

This doesn't fix things to set builder to true in default settings

# Use UV to create Hatch environments
[tool.hatch.envs.default]
installer = "uv"
builder = true

But this does if i set it in the specific environment:

[tool.hatch.envs.build]
description = """Test the installation the package."""
dependency-groups = [
    "build",
]
detached = true
builder = true 

Also i have the same issue with optional-dependency groups where i need to set builder to true for these to work without error as well.

Is that the intention that you need to have builder enabled in order to access dependency groups? This seems to impact project.optional-dependencies group(s)as well.

But the documentation does not show that we need to set builder to try (which i think we don't need to pure python packages.

Screenshot 2026-01-06 at 5 49 04 PM

If my comment is out of scope here, please let me know, I'm just testing things!! Thank you!

@cjames23
Copy link
Member

cjames23 commented Jan 7, 2026

Is that the intention that you need to have builder enabled in order to access dependency groups? This seems to impact project.optional-dependencies group(s)as well.

But the documentation does not show that we need to set builder to try (which i think we don't need to pure python packages.

The intention should be that dependency groups should work without having to set as a builder environment because this should follow the spec from the PEP for dependency groups. I am going to take a deeper look at this within the next couple of days. @lwasser Thank you for testing out this change! this will help for us to make sure we get the right fix for this to match what the documentation suggests which is correct that dependency-groups should work for any environment.

@lwasser
Copy link
Contributor

lwasser commented Jan 7, 2026

@cjames23 of course. I'm happy to test again. We have a packaging template that uses hatch. And we will be teaching with it this spring. Our tests started to fail and it turns out this is the culprit! So our community is happy to be a test bed as this pr moves forward! ✨

@cjames23
Copy link
Member

cjames23 commented Jan 7, 2026

After taking a look I think I actually know what the change likely needs to look like

@cached_property
    def dependencies_complex(self) -> list[Dependency]:
        from hatch.dep.core import Dependency

        all_dependencies_complex = list(self.environment_dependencies_complex)

        # Convert additional_dependencies to Dependency objects
        for dep in self.additional_dependencies:
            if isinstance(dep, Dependency):
                all_dependencies_complex.append(dep)
            else:
                all_dependencies_complex.append(Dependency(str(dep)))

        if self.builder:
            from hatch.project.constants import BuildEnvVars

            # Convert build requirements to Dependency objects
            for req in self.metadata.build.requires_complex:
                if isinstance(req, Dependency):
                    all_dependencies_complex.append(req)
                else:
                    all_dependencies_complex.append(Dependency(str(req)))

            for target in os.environ.get(BuildEnvVars.REQUESTED_TARGETS, "").split():
                target_config = self.app.project.config.build.target(target)
                all_dependencies_complex.extend(map(Dependency, target_config.dependencies))

            return all_dependencies_complex

        # Ensure these are checked last to speed up initial environment creation since
        # they will already be installed along with the project
        if self.dev_mode:
            all_dependencies_complex.extend(self.project_dependencies_complex)

        return all_dependencies_complex

Should become

@cached_property
  def dependencies_complex(self) -> list[Dependency]:
      from hatch.dep.core import Dependency

      all_dependencies_complex = list(self.environment_dependencies_complex)

      # Convert additional_dependencies to Dependency objects
      for dep in self.additional_dependencies:
          if isinstance(dep, Dependency):
              all_dependencies_complex.append(dep)
          else:
              all_dependencies_complex.append(Dependency(str(dep)))

      if self.builder:
          from hatch.project.constants import BuildEnvVars

          # Convert build requirements to Dependency objects
          for req in self.metadata.build.requires_complex:
              if isinstance(req, Dependency):
                  all_dependencies_complex.append(req)
              else:
                  all_dependencies_complex.append(Dependency(str(req)))

          for target in os.environ.get(BuildEnvVars.REQUESTED_TARGETS, "").split():
              target_config = self.app.project.config.build.target(target)
              all_dependencies_complex.extend(map(Dependency, target_config.dependencies))

          return all_dependencies_complex

      # Ensure these are checked last to speed up initial environment creation since
      # they will already be installed along with the project
      if self.dev_mode or self.features or self.dependency_groups:
          all_dependencies_complex.extend(self.project_dependencies_complex)

      return all_dependencies_complex

The critical line is the change to if self.dev_mode: to include the conditionals of features or dependency_groups. I have not confirmed this yet but I will try to confirm sometime in the next day that this does indeed solve the problem.

@cjames23
Copy link
Member

cjames23 commented Jan 8, 2026

@br3ndonland let me know if you want to make the additional change from the comment I made in this PR, otherwise I can create a separate PR that addresses everything.

Update the conditional check for dev_mode to include features or dependency groups.
@cjames23
Copy link
Member

cjames23 commented Jan 8, 2026

@lwasser With the committed change I made to this PR, would you like to test those changes and see if that resolves the issue?

@cjames23 cjames23 merged commit e33e232 into pypa:master Jan 9, 2026
72 of 74 checks passed
github-actions bot pushed a commit that referenced this pull request Jan 9, 2026
* Allow dependency groups in builder and non-dev envs

* Un-parametrize dependency group test

* Update dev_mode check to include features and dependency groups

Update the conditional check for dev_mode to include features or dependency groups.

---------

Co-authored-by: Cary Hawkins <hawkinscary23@gmail.com> e33e232
@br3ndonland
Copy link
Contributor Author

@cjames23 thanks for your review.

@lwasser thanks for your testing. It's great to see this community engagement, and I'm glad this fix will help you.

@br3ndonland br3ndonland deleted the builder-non-dev-dependency-groups branch January 10, 2026 00:28
github-actions bot pushed a commit to DimitriPapadopoulos/hatch that referenced this pull request Jan 10, 2026
* Allow dependency groups in builder and non-dev envs

* Un-parametrize dependency group test

* Update dev_mode check to include features and dependency groups

Update the conditional check for dev_mode to include features or dependency groups.

---------

Co-authored-by: Cary Hawkins <hawkinscary23@gmail.com> e33e232
@lwasser
Copy link
Contributor

lwasser commented Jan 12, 2026

Thank you all for the work on this. I'll test it out this week!!

@lwasser
Copy link
Contributor

lwasser commented Jan 13, 2026

Hi everyone. Just a quick note that I was able to test this out today and it's working well. Thank you again for the speedy patch. We will watch for a new release!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Builder and non-dev environments ignore dependency groups

3 participants