From 739bfa1933ae4b52d75e87272e0de34ecd01d0fa Mon Sep 17 00:00:00 2001 From: Mike Sarahan Date: Wed, 3 Jan 2024 10:21:15 -0600 Subject: [PATCH] Reduce unset script_env variable to warning (#5105) --- conda_build/build.py | 7 +++++-- conda_build/environ.py | 4 +++- news/5105-script-env-warn | 20 +++++++++++++++++++ .../_build_script_missing_var/meta.yaml | 9 +++++++++ tests/test_subpackages.py | 10 ++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 news/5105-script-env-warn create mode 100644 tests/test-recipes/split-packages/_build_script_missing_var/meta.yaml diff --git a/conda_build/build.py b/conda_build/build.py index c007286474..c6575bada0 100644 --- a/conda_build/build.py +++ b/conda_build/build.py @@ -1919,9 +1919,12 @@ def bundle_conda(output, metadata: MetaData, env, stats, **kw): val = var.split("=", 1)[1] var = var.split("=", 1)[0] elif var not in os.environ: - raise ValueError( - f"env var '{var}' specified in script_env, but is not set." + warnings.warn( + "The environment variable '%s' specified in script_env is undefined." + % var, + UserWarning, ) + val = "" else: val = os.environ[var] env_output[var] = val diff --git a/conda_build/environ.py b/conda_build/environ.py index c165bdeba2..85733d97d1 100644 --- a/conda_build/environ.py +++ b/conda_build/environ.py @@ -491,7 +491,9 @@ def meta_vars(meta: MetaData, skip_build_id=False): value = os.getenv(var_name) if value is None: warnings.warn( - "The environment variable '%s' is undefined." % var_name, UserWarning + "The environment variable '%s' specified in script_env is undefined." + % var_name, + UserWarning, ) else: d[var_name] = value diff --git a/news/5105-script-env-warn b/news/5105-script-env-warn new file mode 100644 index 0000000000..5b7d66bad5 --- /dev/null +++ b/news/5105-script-env-warn @@ -0,0 +1,20 @@ +### Enhancements + +* Relax script_env error in outputs when variable referenced in script_env is not defined. + This unifies current behavior with the top-level build. (#5105) + +### Bug fixes + +* + +### Deprecations + +* + +### Docs + +* + +### Other + +* diff --git a/tests/test-recipes/split-packages/_build_script_missing_var/meta.yaml b/tests/test-recipes/split-packages/_build_script_missing_var/meta.yaml new file mode 100644 index 0000000000..d1c2bfbe57 --- /dev/null +++ b/tests/test-recipes/split-packages/_build_script_missing_var/meta.yaml @@ -0,0 +1,9 @@ +package: + name: test_build_script_in_output + version: 1.0 + +outputs: + - name: test_1 + build: + script_env: + - TEST_FN_DOESNT_EXIST diff --git a/tests/test_subpackages.py b/tests/test_subpackages.py index 2d63042cb5..db75006a6d 100644 --- a/tests/test_subpackages.py +++ b/tests/test_subpackages.py @@ -335,6 +335,16 @@ def test_build_script_and_script_env(testing_config): api.build(recipe, config=testing_config) +@pytest.mark.sanity +def test_build_script_and_script_env_warn_empty_script_env(testing_config): + recipe = os.path.join(subpackage_dir, "_build_script_missing_var") + with pytest.warns( + UserWarning, + match="The environment variable 'TEST_FN_DOESNT_EXIST' specified in script_env is undefined", + ): + api.build(recipe, config=testing_config) + + @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):