From cf8d27797ad9e43353750af6e848b6c31406dfda Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 30 Apr 2024 10:03:27 -0500 Subject: [PATCH 1/4] ensure undefined script_env variables are undefined in multi-output build environment --- conda_build/build.py | 4 +--- .../_build_script_relying_on_missing_var/meta.yaml | 14 ++++++++++++++ tests/test_subpackages.py | 9 +++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml diff --git a/conda_build/build.py b/conda_build/build.py index f1bf8eec02..5530e3b5a4 100644 --- a/conda_build/build.py +++ b/conda_build/build.py @@ -1768,10 +1768,8 @@ def bundle_conda(output, metadata: MetaData, env, stats, **kw): % var, 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 diff --git a/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml b/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml new file mode 100644 index 0000000000..686cb6ea50 --- /dev/null +++ b/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml @@ -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 diff --git a/tests/test_subpackages.py b/tests/test_subpackages.py index 11e43383d0..7ba1beb96a 100644 --- a/tests/test_subpackages.py +++ b/tests/test_subpackages.py @@ -6,6 +6,7 @@ import sys from glob import glob from pathlib import Path +from subprocess import CalledProcessError import pytest from conda.base.context import context @@ -352,6 +353,14 @@ 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(CalledProcessError): + 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") From a37383e43aedf17eeb84f335c2ad2f5d49fe9f91 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 30 Apr 2024 10:32:17 -0500 Subject: [PATCH 2/4] add news, fix formatting --- conda_build/build.py | 1 + news/5322-undefine-build-vars | 20 ++++++++++++++++++++ tests/test_subpackages.py | 6 +++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 news/5322-undefine-build-vars diff --git a/conda_build/build.py b/conda_build/build.py index 5530e3b5a4..fbd5098be3 100644 --- a/conda_build/build.py +++ b/conda_build/build.py @@ -1762,6 +1762,7 @@ def bundle_conda(output, metadata: MetaData, env, stats, **kw): 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( "The environment variable '%s' specified in script_env is undefined." diff --git a/news/5322-undefine-build-vars b/news/5322-undefine-build-vars new file mode 100644 index 0000000000..67c47373b5 --- /dev/null +++ b/news/5322-undefine-build-vars @@ -0,0 +1,20 @@ +### Enhancements + +* + +### 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 + +* + +### Docs + +* + +### Other + +* diff --git a/tests/test_subpackages.py b/tests/test_subpackages.py index 7ba1beb96a..1aab73ea63 100644 --- a/tests/test_subpackages.py +++ b/tests/test_subpackages.py @@ -353,8 +353,11 @@ 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): +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(CalledProcessError): @@ -362,6 +365,7 @@ def test_build_script_does_not_set_env_from_script_env_if_missing(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): From f0eefa1c54c43b14118165c167d667c3e4b6124a Mon Sep 17 00:00:00 2001 From: James Lamb Date: Tue, 30 Apr 2024 15:21:03 -0500 Subject: [PATCH 3/4] switch quoting for Windows tests --- .../_build_script_relying_on_missing_var/meta.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml b/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml index 686cb6ea50..55ce0b9d0d 100644 --- a/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml +++ b/tests/test-recipes/split-packages/_build_script_relying_on_missing_var/meta.yaml @@ -8,7 +8,7 @@ outputs: script_env: - TEST_FN_DOESNT_EXIST script: - - python -c 'import os; print("val... " + os.environ["TEST_FN_DOESNT_EXIST"])' + - python -c "import os; print('val...' + os.environ['TEST_FN_DOESNT_EXIST'])" requirements: host: - python From f7ac317102df0826c12bdb625365858f755d18f2 Mon Sep 17 00:00:00 2001 From: James Lamb Date: Mon, 3 Jun 2024 20:03:48 -0500 Subject: [PATCH 4/4] failures from the build script are now raised as BuildScriptException --- tests/test_subpackages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_subpackages.py b/tests/test_subpackages.py index 1aab73ea63..a856392d66 100644 --- a/tests/test_subpackages.py +++ b/tests/test_subpackages.py @@ -6,12 +6,12 @@ import sys from glob import glob from pathlib import Path -from subprocess import CalledProcessError import pytest from conda.base.context import context from conda_build import api, utils +from conda_build.exceptions import BuildScriptException from conda_build.metadata import MetaDataTuple from conda_build.render import finalize_metadata @@ -360,7 +360,7 @@ def test_build_script_does_not_set_env_from_script_env_if_missing( ): monkeypatch.delenv("TEST_FN_DOESNT_EXIST", raising=False) recipe = os.path.join(subpackage_dir, "_build_script_relying_on_missing_var") - with pytest.raises(CalledProcessError): + with pytest.raises(BuildScriptException): api.build(recipe, config=testing_config) captured = capfd.readouterr() assert "KeyError: 'TEST_FN_DOESNT_EXIST'" in captured.err