Skip to content

Add env lock command to generate PEP compliant lock files.#2216

Merged
cjames23 merged 21 commits into
pypa:masterfrom
cjames23:env-lock-feature
Apr 23, 2026
Merged

Add env lock command to generate PEP compliant lock files.#2216
cjames23 merged 21 commits into
pypa:masterfrom
cjames23:env-lock-feature

Conversation

@cjames23
Copy link
Copy Markdown
Member

@cjames23 cjames23 commented Mar 26, 2026

This PR replaces #2174 which had been closed due to having to delete my fork to get back to a clean state for creating a release.

The latest changes made ensure dependency-groups and extras are passed through for lockfile generation as well.

Closes #716 #749 #2176

Includes AI generated content.

@flying-sheep
Copy link
Copy Markdown
Contributor

Awesome! I think the only thing missing is some tests of the actual lockfile, unless I’m missing something. E.g. that envs.some-env.dependencies end up in it and that that f14a893 works as expected.

@gabrielsimoes
Copy link
Copy Markdown

@flying-sheep @cjames23 firstly, thank you for your work here.

Are there plans to support generate_lockfile as a method in EnvironmentInterface so that plugins can implement custom behavior (e.g. populate [tool] with custom metadata)?

If this is not in the plans, would you be open to a contribution here? (once this PR merges of course) I can open another issue if this is best tracked separately.

@cjames23
Copy link
Copy Markdown
Member Author

Awesome! I think the only thing missing is some tests of the actual lockfile, unless I’m missing something. E.g. that envs.some-env.dependencies end up in it and that that f14a893 works as expected.

Nope not missing anything. I am working on those tests right now :) I was just getting the PR back open for us to continue discussing and iterating over it.

@ofek
Copy link
Copy Markdown
Contributor

ofek commented Mar 31, 2026

Are there plans to support generate_lockfile as a method in EnvironmentInterface so that plugins can implement custom behavior (e.g. populate [tool] with custom metadata)?

Perhaps useful for @cjames23 to know, there are other ways to go about implementing pluggable lock file support. I proposed a potential design in #355. The read/write methods would no longer be required though because I landed on a much better (imo) abstraction with the FileSystemContext as returned by an environment's EnvironmentInterface.fs_context method.

The idea was that if an environment interface exposed filesystem synchronization primitives then dependency lockers could merely use that and be its own type of plugin. I thought that it would be nice to have them decoupled, to a certain extent. Lockers would also call other environment methods that are part of the public API but I view that as way cleaner and easier to implement than a locker having to subclass an environment interface just to change a bit of logic.

This was years ago when I was in peak Hatch design mode so my gut instinct tells me that paradigm is directionally correct. However, I would prefer providing value to users sooner rather than later so it's totally your call! Whatever you do is fine by me; just let me know when to review 🙂

ofek
ofek previously approved these changes Apr 10, 2026
Copy link
Copy Markdown
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Feel free to merge after addressing the docs issues.

Comment thread docs/how-to/environment/lockfiles.md
Comment thread docs/plugins/environment/reference.md
Comment thread docs/plugins/locker.md Outdated
Comment thread docs/plugins/locker.md Outdated
Comment on lines +54 to +58
upgrade: bool = False,
upgrade_packages: tuple[str, ...] = (),
layered: bool = False,
lock_extras: tuple[str, ...] = (),
lock_groups: tuple[str, ...] = (),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with merging but do you think we should hold off on adding so many options until we get feedback on the workflow?

Comment thread src/hatch/env/lock.py Outdated
Comment thread tests/conftest.py Outdated
@cjames23 cjames23 merged commit 89fb3cb into pypa:master Apr 23, 2026
47 checks passed
@cjames23 cjames23 deleted the env-lock-feature branch April 23, 2026 04:59
github-actions Bot pushed a commit that referenced this pull request Apr 23, 2026
* Add env lock command to generate PEP compliant lock files.

* Fix formatting issues

* Move to per environment config of lockfiles

* Move to export-all, adjust docs, dedupe based on lock-filename

* Change to usefixture for helpers for lock tests

* Ensure all dependencies are included in lock

* Fix list transformation

* Fix formatting

* Add regression tests that confirm functionality

* dd pluggable dependency lockers (hatch_register_locker) with built-in UV/pip implementations, a shared lock coordinator (generate / in_sync / apply_lock), and new hatch dep lock, hatch dep sync, and hatch lock commands alongside existing hatch env lock.

* Fix formatting and type issues

* Remove links to github discussion

* Fix link

* Address issue with pip locker and apply_lock

* Fix formatting

* Fix docs, better mocking approach for locks.

* Fix tests after rebase. 89fb3cb
@tmke8
Copy link
Copy Markdown

tmke8 commented May 8, 2026

It seems only the first of the linked issues was closed by the merge.

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.

Feature request: Support dependency hashing

5 participants