Repository: conda/conda-build

Issue:
yaml parsing error of recipe with "outputs" using conda-build 3.1.0
Recipes with defined "outputs" sections that built with conda-build 3.0.31 are failing with 3.1.0.

stacktrace:
```
$ conda-build mkl-recipe
Attempting to finalize metadata for mkl
INFO:conda_build.metadata:Attempting to finalize metadata for mkl
Traceback (most recent call last):
  File "/localdisk/work/miniconda3/bin/conda-build", line 11, in <module>
    sys.exit(main())
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/cli/main_build.py", line 388, in main
    execute(sys.argv[1:])
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/cli/main_build.py", line 379, in execute
    verify=args.verify)
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/api.py", line 187, in build
    need_source_download=need_source_download, config=config, variants=variants)
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/build.py", line 1833, in build_tree
    notest=notest,
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/build.py", line 914, in build
    output_metas = expand_outputs([(m, need_source_download, need_reparse_in_env)])
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/render.py", line 607, in expand_outputs
    for (output_dict, m) in _m.get_output_metadata_set(permit_unsatisfiable_variants=False):
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/metadata.py", line 1773, in get_output_metadata_set
    permit_unsatisfiable_variants=permit_unsatisfiable_variants)
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/metadata.py", line 658, in finalize_outputs_pass
    fm = finalize_metadata(om, permit_unsatisfiable_variants=permit_unsatisfiable_variants)
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/conda_build/render.py", line 315, in finalize_metadata
    template_string=template_string)) or {}).get('requirements', {})
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/__init__.py", line 94, in safe_load
    return load(stream, SafeLoader)
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/__init__.py", line 72, in load
    return loader.get_single_data()
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/constructor.py", line 35, in get_single_data
    node = self.get_single_node()
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/composer.py", line 39, in get_single_node
    if not self.check_event(StreamEndEvent):
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "/localdisk/work/miniconda3/lib/python3.6/site-packages/yaml/parser.py", line 174, in parse_document_start
    self.peek_token().start_mark)
yaml.parser.ParserError: expected '<document start>', but found '<block sequence start>'
  in "<unicode string>", line 7, column 3:
      - name: mkl-include
      ^
```

recipe
```
{% set version = "2018.0.1" %}
{% set buildnumber = 5 %}

package:
  name: mkl
  version: {{version}}

build:
  number: {{buildnumber}}
  string: intel_{{buildnumber}}
  binary_relocation: false
  detect_binary_files_with_prefix: false

outputs:
  - name: mkl
    script: mkl-setup.sh
    requirements:
      - openmp 2018*
  - name: mkl-include
    script: mkl-include-setup.sh
  - name: mkl-static
    script: mkl-static-setup.sh
    requirements:
      - mkl-include {{version}}
  - name: mkl-devel
    requirements:
      - mkl {{version}}
      - mkl-include {{version}}

about:
  home: http://software.intel.com/en-us/articles/intel-mkl/
  license: proprietary - Intel
  license_file: license.txt
  license_family: Proprietary
  summary: Math library for Intel and compatible processors
```


Discussion:

Resolving patch:
diff --git a/conda_build/metadata.py b/conda_build/metadata.py
--- a/conda_build/metadata.py
+++ b/conda_build/metadata.py
@@ -1442,12 +1442,12 @@ def uses_vcs_in_build(self):
                             return vcs
         return None
 
-    def get_recipe_text(self, extract_pattern=None):
+    def get_recipe_text(self, extract_pattern=None, force_top_level=False):
         is_output = self.meta.get('extra', {}).get('parent_recipe', {}).get('path')
         meta_path = self.meta_path or (os.path.join(is_output, 'meta.yaml') if is_output else '')
         if meta_path:
             recipe_text = read_meta_file(meta_path)
-            if (is_output and
+            if (is_output and not force_top_level and
                   self.name() != self.meta.get('extra', {}).get('parent_recipe', {}).get('name')):
                 recipe_text = _filter_recipe_text(recipe_text, _output_filter_pattern(self.name()))
         else:
@@ -1464,7 +1464,7 @@ def extract_requirements_text(self):
             filter_ = r'(^requirements:.*?)(^\s*test:|^\s*extra:|^\s*about:|^outputs:|\Z)'
         else:
             # outputs are already filtered into each output for us
-            filter_ = r'(^\s*requirements:.*?)(^\s*test:|^\s*extra:|^\s*about:|^outputs:|\Z)'
+            filter_ = r'(^\s*requirements:.*?)(^\s*test:|^\s*extra:|^\s*about:|^\s*-\sname:|^outputs:|\Z)'  # NOQA
         return self.get_recipe_text(filter_)
 
     def extract_outputs_text(self):
diff --git a/conda_build/render.py b/conda_build/render.py
--- a/conda_build/render.py
+++ b/conda_build/render.py
@@ -309,7 +309,8 @@ def finalize_metadata(m, permit_unsatisfiable_variants=False):
     #     requirements for a particular output
     # Re-parse the output from the original recipe, so that we re-consider any jinja2 stuff
     extract_pattern = r'(.*)package:'
-    template_string = '\n'.join((m.get_recipe_text(extract_pattern=extract_pattern),
+    template_string = '\n'.join((m.get_recipe_text(extract_pattern=extract_pattern,
+                                                   force_top_level=True),
                                 m.extract_requirements_text()))
     requirements = (yaml.safe_load(m._get_contents(permit_undefined_jinja=False,
                             template_string=template_string)) or {}).get('requirements', {})


Test patch:
diff --git a/tests/test-recipes/split-packages/outputs_using_vars_defined_in_meta/meta.yaml b/tests/test-recipes/split-packages/outputs_using_vars_defined_in_meta/meta.yaml
new file mode 100644
--- /dev/null
+++ b/tests/test-recipes/split-packages/outputs_using_vars_defined_in_meta/meta.yaml
@@ -0,0 +1,31 @@
+# Regression test for https://github.com/conda/conda-build/issues/2548
+
+{% set version = "2018.0.1" %}
+{% set buildnumber = 5 %}
+
+package:
+  name: mkl
+  version: {{version}}
+
+build:
+  number: {{buildnumber}}
+  string: intel_{{buildnumber}}
+  binary_relocation: false
+  detect_binary_files_with_prefix: false
+
+outputs:
+  - name: mkl
+  - name: mkl-include
+  - name: mkl-static
+    requirements:
+      - mkl-include {{version}}
+  - name: mkl-devel
+    requirements:
+      - mkl {{version}}
+      - mkl-include {{version}}
+
+about:
+  home: http://software.intel.com/en-us/articles/intel-mkl/
+  license: proprietary - Intel
+  license_family: Proprietary
+  summary: Math library for Intel and compatible processors
diff --git a/tests/test_subpackages.py b/tests/test_subpackages.py
--- a/tests/test_subpackages.py
+++ b/tests/test_subpackages.py
@@ -6,7 +6,7 @@
 
 from conda_build.render import finalize_metadata
 from conda_build.conda_interface import subdir
-from conda_build import api, utils, exceptions
+from conda_build import api, utils
 
 from .utils import subpackage_dir, is_valid_dir