Skip to content

Commit

Permalink
ensure undefined script_env variables are undefined in multi-output b…
Browse files Browse the repository at this point in the history
…uild environment (#5322)

* ensure undefined script_env variables are undefined in multi-output build environment

* add news, fix formatting

* switch quoting for Windows tests

* failures from the build script are now raised as BuildScriptException
  • Loading branch information
jameslamb committed Jul 29, 2024
1 parent 6dbdec8 commit 1fd2dcb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
5 changes: 2 additions & 3 deletions conda_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,15 +1762,14 @@ def bundle_conda(
if "=" in var:
val = var.split("=", 1)[1]
var = var.split("=", 1)[0]
env_output[var] = val
elif var not in os.environ:
warnings.warn(
f"The environment variable '{var}' specified in script_env is undefined.",
UserWarning,
)
val = ""
else:
val = os.environ[var]
env_output[var] = val
env_output[var] = os.environ[var]
dest_file = os.path.join(metadata.config.work_dir, output["script"])
utils.copy_into(os.path.join(metadata.path, output["script"]), dest_file)
from os import stat
Expand Down
20 changes: 20 additions & 0 deletions news/5322-undefine-build-vars
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Enhancements

* <news item>

### Bug fixes

* Ensures that variables mentioned in `script_env` are undefined in multi-output build environment
if undefined in the environment `conda-build` is invoked from.

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package:
name: test_build_script_relying_on_missing_var
version: 1.0

outputs:
- name: test_1
build:
script_env:
- TEST_FN_DOESNT_EXIST
script:
- python -c "import os; print('val...' + os.environ['TEST_FN_DOESNT_EXIST'])"
requirements:
host:
- python
14 changes: 13 additions & 1 deletion tests/test_subpackages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from conda.base.context import context

from conda_build import api, utils
from conda_build.exceptions import CondaBuildUserError
from conda_build.exceptions import BuildScriptException, CondaBuildUserError
from conda_build.metadata import MetaDataTuple
from conda_build.render import finalize_metadata

Expand Down Expand Up @@ -354,6 +354,18 @@ def test_build_script_and_script_env_warn_empty_script_env(testing_config):
api.build(recipe, config=testing_config)


@pytest.mark.sanity
def test_build_script_does_not_set_env_from_script_env_if_missing(
testing_config, capfd, monkeypatch
):
monkeypatch.delenv("TEST_FN_DOESNT_EXIST", raising=False)
recipe = os.path.join(subpackage_dir, "_build_script_relying_on_missing_var")
with pytest.raises(BuildScriptException):
api.build(recipe, config=testing_config)
captured = capfd.readouterr()
assert "KeyError: 'TEST_FN_DOESNT_EXIST'" in captured.err


@pytest.mark.sanity
@pytest.mark.skipif(sys.platform != "darwin", reason="only implemented for mac")
def test_strong_run_exports_from_build_applies_to_host(testing_config):
Expand Down

0 comments on commit 1fd2dcb

Please sign in to comment.