Skip to content

Commit 7b4b270

Browse files
committed
Respect user defined environment variables for context formatting
1 parent 2ed47c8 commit 7b4b270

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/history.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
3434
- The `env show`, `dep hash`, and all `dep show` commands now honor context formatting
3535
- Decreasing verbosity now has no affect on output that should always be displayed
3636
- Handle more edge cases in the `setuptools` migration script
37+
- Environments now respect user defined environment variables for context formatting
3738
- Allow `extra-dependencies` in environment overrides
3839
- Depend on `packaging` explicitly rather than relying on it being a transitive dependency of Hatchling
3940

src/hatch/env/plugin/interface.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def environment_dependencies_complex(self):
274274
from packaging.requirements import InvalidRequirement, Requirement
275275

276276
dependencies_complex = []
277-
with self.metadata.context.apply_context(self.context):
277+
with self.apply_context():
278278
for option in ('dependencies', 'extra-dependencies'):
279279
dependencies = self.config.get(option, [])
280280
if not isinstance(dependencies, list):
@@ -758,7 +758,7 @@ def expand_command(self, command):
758758
if not args:
759759
args = None
760760

761-
with self.metadata.context.apply_context(self.context):
761+
with self.apply_context():
762762
if possible_script in self.scripts:
763763
for cmd in self.scripts[possible_script]:
764764
yield self.metadata.context.format(cmd, args=args).strip()
@@ -879,6 +879,11 @@ def get_option_types() -> dict:
879879
"""
880880
return {}
881881

882+
@contextmanager
883+
def apply_context(self):
884+
with self.get_env_vars(), self.metadata.context.apply_context(self.context):
885+
yield
886+
882887
def __enter__(self):
883888
self.activate()
884889
return self

tests/env/plugin/test_interface.py

+23
Original file line numberDiff line numberDiff line change
@@ -1159,3 +1159,26 @@ def test_matrix_default_override(self, isolation, data_dir, platform):
11591159
)
11601160

11611161
assert environment.dependencies == ['pkg==42']
1162+
1163+
def test_env_vars_override(self, isolation, data_dir, platform):
1164+
config = {
1165+
'project': {'name': 'my_app', 'version': '0.0.1'},
1166+
'tool': {
1167+
'hatch': {
1168+
'envs': {
1169+
'default': {
1170+
'dependencies': ['pkg{env:DEP_PIN}'],
1171+
'env-vars': {'DEP_PIN': '==0.0.1'},
1172+
'overrides': {'env': {'DEP_ANY': {'env-vars': 'DEP_PIN='}}},
1173+
},
1174+
},
1175+
},
1176+
},
1177+
}
1178+
with EnvVars({'DEP_ANY': 'true'}):
1179+
project = Project(isolation, config=config)
1180+
environment = MockEnvironment(
1181+
isolation, project.metadata, 'default', project.config.envs['default'], {}, data_dir, platform, 0
1182+
)
1183+
1184+
assert environment.dependencies == ['pkg']

0 commit comments

Comments
 (0)