Repository: conda/conda-build

Issue:
PIP_NO_DEPENDENCIES & co not set for outputs/*/script
<!--
Thanks for opening an issue! A few things to keep in mind:

- This issue tracker is for issues with or enhancements for conda-build.

Anaconda Community Code of Conduct: https://www.anaconda.com/community-code-of-conduct/

For conda issues, you should open an issue at https://github.com/conda/conda/issues
For Anaconda issues, you should open an issue at https://github.com/ContinuumIO/anaconda-issues/issues
For an issue with a particular conda-forge package, you should open an issue on the 
corresponding feedstock: https://github.com/conda-forge
-->

### Actual Behavior

Environment variables like `PIP_NO_DEPENDENCIES` set for the build script at https://github.com/conda/conda-build/blob/3.19.2/conda_build/build.py#L2372-L2397 , are not set during the execution of `outputs/*/script`.
https://github.com/conda/conda-build/blob/3.19.2/conda_build/build.py#L1406-L1450 seems to be the `outputs/*/script` relevant code.

See https://dev.azure.com/conda-forge/feedstock-builds/_build/results?buildId=186409&view=logs&j=1bf226d3-0e2f-52d8-fa93-7d9e633347b3&t=c09a5a47-eec3-5700-9284-6f1bd7b23501&l=333 .

### Expected Behavior

The env vars set at https://github.com/conda/conda-build/blob/3.19.2/conda_build/build.py#L2372-L2397 should also be set when running a recipe's `outputs`' `script`.

### Steps to Reproduce

See https://github.com/conda-forge/black-feedstock/pull/14 .
If you run that recipe without the added `--no-deps`, it will trigger the reported behavior.


##### `Output of conda info`
https://dev.azure.com/conda-forge/feedstock-builds/_build/results?buildId=186409&view=logs&j=1bf226d3-0e2f-52d8-fa93-7d9e633347b3&t=c09a5a47-eec3-5700-9284-6f1bd7b23501&l=189
```

     active environment : base
    active env location : /opt/conda
            shell level : 1
       user config file : /home/conda/.condarc
 populated config files : /opt/conda/.condarc
                          /home/conda/.condarc
          conda version : 4.8.3
    conda-build version : 3.19.2
         python version : 3.7.6.final.0
       virtual packages : __glibc=2.12
       base environment : /opt/conda  (writable)
           channel URLs : https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/conda/pkgs
                          /home/conda/.conda/pkgs
       envs directories : /opt/conda/envs
                          /home/conda/.conda/envs
               platform : linux-64
             user-agent : conda/4.8.3 requests/2.24.0 CPython/3.7.6 Linux/4.15.0-1091-azure centos/6.10 glibc/2.12
                UID:GID : 1001:1001
             netrc file : None
           offline mode : False
```

cc @carlodri, @xhochy


Discussion:
Isn't this a feature that the envs for outputs are not activated by default and `activate_in_script: True` has to be added explicitly to make it so?
IDK. AFAICT, `activate_in_script` is still an undocumented feature.
There is not much to find with https://github.com/conda/conda-build/search?q=activate_in_script.
https://github.com/conda/conda-build/issues/3695
https://github.com/conda/conda-build/issues/2735
https://github.com/conda/conda-build/blob/3.19.3/CHANGELOG.txt#L1694
> * Activate output scripts in meta.yaml ( #2667 ), but only when:
>   * output has a build/script entry
>   * output uses ``{{ compiler() }}`` jinja2 function in its requirements AND output extension is either .sh or .bat
>   * output has ``build/activate_in_script`` key in meta.yaml set to a truthy value AND output extension is either .sh or .bat

We do have "output has `build/script` entry", so I would expect the environment gets activated.
I also tried adding `build: activate_in_script: True` for the recipe that can be found in https://github.com/conda-forge/black-feedstock/pull/15 (whilst removing the manually set env vars for the `pip install` call). Even then, the expected environment variables are not set.

To me, it looks like this is broken currently (w/ 3.19.2). IDK if/when it worked before.
This also looks similar to issue ( https://github.com/conda/conda-build/issues/4292 )

cc @jaimergp
Bumping this issue since I recently ran into the same problem using `mamba build`, and I'm assuming they may be related.  @jakirkham mentioned that I should apply the same workarounds used in [this PR](https://github.com/rapidsai/ucx-split-feedstock/pull/28), which worked for me.

FWIW, I'm building my package using `mamba build ...` and I have conda-build version 3.21.8 installed.
FWIW just saw this issue again today

Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -1775,6 +1775,7 @@ def bundle_conda(
         env_output["RECIPE_DIR"] = metadata.path
         env_output["MSYS2_PATH_TYPE"] = "inherit"
         env_output["CHERE_INVOKING"] = "1"
+        _set_env_variables_for_build(metadata, env_output)
         for var in utils.ensure_list(metadata.get_value("build/script_env")):
             if "=" in var:
                 val = var.split("=", 1)[1]
@@ -3060,13 +3061,7 @@ def construct_metadata_for_test(recipedir_or_package, config):
     return m, hash_input
 
 
-def write_build_scripts(m, script, build_file):
-    # TODO: Prepending the prefixes here should probably be guarded by
-    #         if not m.activate_build_script:
-    #       Leaving it as is, for now, since we need a quick, non-disruptive patch release.
-    with utils.path_prepended(m.config.host_prefix, False):
-        with utils.path_prepended(m.config.build_prefix, False):
-            env = environ.get_dict(m=m)
+def _set_env_variables_for_build(m, env):
     env["CONDA_BUILD_STATE"] = "BUILD"
 
     # hard-code this because we never want pip's build isolation
@@ -3098,6 +3093,17 @@ def write_build_scripts(m, script, build_file):
     if "replacements" in env:
         del env["replacements"]
 
+
+def write_build_scripts(m, script, build_file):
+    # TODO: Prepending the prefixes here should probably be guarded by
+    #         if not m.activate_build_script:
+    #       Leaving it as is, for now, since we need a quick, non-disruptive patch release.
+    with utils.path_prepended(m.config.host_prefix, False):
+        with utils.path_prepended(m.config.build_prefix, False):
+            env = environ.get_dict(m=m)
+
+    _set_env_variables_for_build(m, env)
+
     work_file = join(m.config.work_dir, "conda_build.sh")
     env_file = join(m.config.work_dir, "build_env_setup.sh")
     with open(env_file, "w") as bf:


Test patch:
diff --git a/tests/test-recipes/split-packages/script_install_files/subpackage1.py b/tests/test-recipes/split-packages/script_install_files/subpackage1.py
--- a/tests/test-recipes/split-packages/script_install_files/subpackage1.py
+++ b/tests/test-recipes/split-packages/script_install_files/subpackage1.py
@@ -2,6 +2,8 @@
 
 out_path = os.path.join(os.environ['PREFIX'], 'subpackage_file_1')
 
+assert "PIP_NO_INDEX" in os.environ
+
 with open(out_path, 'w') as f:
     f.write("weeee")