Repository: conda/conda-build

Issue:
run_exports from channeldata.json not always applied
Using conda 3.18.7 run_exports are not reliably applied to recipes.

For example:

``` yaml
package:
  name: example
  version: 1.0

requirements:
  build:
    - {{ compiler('c') }}
```

Rendering this recipe repeatedly gives two different results, the second one in this example is correct. 

```
[root@chi9 io]# conda build --version
conda-build 3.18.7
[root@chi9 io]# conda render .
--------------
Hash contents:
--------------
{'c_compiler': 'gcc'}
----------
meta.yaml:
----------
package:
    name: example
    version: '1.0'
requirements:
    build:
        - _libgcc_mutex 0.1 main
        - binutils_impl_linux-64 2.31.1 h6176602_1
        - libstdcxx-ng 9.1.0 hdf63c60_0
        - binutils_linux-64 2.31.1 h6176602_7
        - libgcc-ng 9.1.0 hdf63c60_0
        - gcc_impl_linux-64 7.3.0 habb00fd_1
        - gcc_linux-64 7.3.0 h553295d_7
extra:
    copy_test_source_files: true
    final: true

[root@chi9 io]# conda render .
--------------
Hash contents:
--------------
{'c_compiler': 'gcc'}
----------
meta.yaml:
----------
package:
    name: example
    version: '1.0'
requirements:
    build:
        - _libgcc_mutex 0.1 main
        - libgcc-ng 9.1.0 hdf63c60_0
        - binutils_linux-64 2.31.1 h6176602_7
        - binutils_impl_linux-64 2.31.1 h6176602_1
        - gcc_impl_linux-64 7.3.0 habb00fd_1
        - gcc_linux-64 7.3.0 h553295d_7
        - libstdcxx-ng 9.1.0 hdf63c60_0
    host:
        - _libgcc_mutex 0.1 main
        - libgcc-ng 9.1.0 hdf63c60_0
    run:
        - libgcc-ng >=7.3.0
extra:
    copy_test_source_files: true
    final: true

```

From my limited debugging this seems to depend on the order in which the channeldata is read in for the sub-channels of `defaults` in the `download_channel` function.  If `main` is read in last the correct `run_exports` are applied, if `r` then the results are incorrect. 

Likely related to some of the comments in #3583 


Discussion:

Resolving patch:
diff --git a/conda_build/utils.py b/conda_build/utils.py
--- a/conda_build/utils.py
+++ b/conda_build/utils.py
@@ -1881,18 +1881,18 @@ def download_channeldata(channel_url):
     if channel_url.startswith('file://') or channel_url not in channeldata_cache:
         urls = get_conda_channel(channel_url).urls()
         urls = set(url.rsplit('/', 1)[0] for url in urls)
-        channeldata = {}
+        data = {}
         for url in urls:
             with TemporaryDirectory() as td:
                 tf = os.path.join(td, "channeldata.json")
                 try:
                     download(url + '/channeldata.json', tf)
                     with open(tf) as f:
-                        data = json.load(f)
+                        new_channeldata = json.load(f)
                 except (JSONDecodeError, CondaHTTPError):
-                    data = {}
-            merge_or_update_dict(channeldata, data)
-        channeldata_cache[channel_url] = channeldata
+                    new_channeldata = {}
+            merge_or_update_dict(data, new_channeldata)
+        channeldata_cache[channel_url] = data
     else:
         data = channeldata_cache[channel_url]
     return data


Test patch: