Skip to content

Commit

Permalink
Merge pull request #1617 from pypa/env-order
Browse files Browse the repository at this point in the history
fix: evaluate environment-pass before environment, so environment can reference or override those variables
  • Loading branch information
joerick authored Oct 3, 2023
2 parents 9da4ede + 29cb138 commit f4291e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 6 additions & 2 deletions cibuildwheel/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ def as_dictionary(

return environment

def add(self, name: str, value: str) -> None:
self.assignments.append(EnvironmentAssignmentRaw(name=name, value=value))
def add(self, name: str, value: str, prepend: bool = False) -> None:
assignment = EnvironmentAssignmentRaw(name=name, value=value)
if prepend:
self.assignments.insert(0, assignment)
else:
self.assignments.append(assignment)

def __repr__(self) -> str:
return f"{self.__class__.__name__}({[repr(a) for a in self.assignments]!r})"
Expand Down
4 changes: 2 additions & 2 deletions cibuildwheel/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ def build_options(self, identifier: str | None) -> BuildOptions:

# Pass through environment variables
if self.platform == "linux":
for env_var_name in environment_pass:
for env_var_name in reversed(environment_pass):
with contextlib.suppress(KeyError):
environment.add(env_var_name, self.env[env_var_name])
environment.add(env_var_name, self.env[env_var_name], prepend=True)

if dependency_versions == "pinned":
dependency_constraints: None | (
Expand Down
19 changes: 19 additions & 0 deletions unit_test/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ def test_container_engine_option(tmp_path: Path, toml_assignment, result_name, r
assert parsed_container_engine.create_args == result_create_args


def test_environment_pass_references():
options = Options(
platform="linux",
command_line_arguments=CommandLineArguments.defaults(),
env={
"CIBW_ENVIRONMENT_PASS_LINUX": "STARTER MAIN_COURSE",
"STARTER": "green eggs",
"MAIN_COURSE": "ham",
"CIBW_ENVIRONMENT": 'MEAL="$STARTER and $MAIN_COURSE"',
},
)
parsed_environment = options.build_options(identifier=None).environment
assert parsed_environment.as_dictionary(prev_environment={}) == {
"MEAL": "green eggs and ham",
"STARTER": "green eggs",
"MAIN_COURSE": "ham",
}


@pytest.mark.parametrize(
("toml_assignment", "result_name", "result_args"),
[
Expand Down

0 comments on commit f4291e7

Please sign in to comment.