Repository: conda/conda-build

Issue:
conda-build failing to report git describe info to templated yaml file
I'm working on a project that keeps its conda recipe in PROJROOT/conda.recipe. In `meta.yaml` file, I have:

```
package:
  name: hexrd
  version: {{ environ['GIT_DESCRIBE_TAG'][1:]+environ.get('GIT_DESCRIBE_NUMBER', '') }}

source:
  path: ../

```

When I try to build it, I get:

```
$ conda build conda.recipe/
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:

    https://github.com/conda/conda-build/issues

Include the output of the command 'conda info' in your report.


Traceback (most recent call last):
  File "/opt/conda/bin/conda-build", line 5, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python2.7/site-packages/conda_build/main_build.py", line 110, in main
    args_func(args, p)
  File "/opt/conda/lib/python2.7/site-packages/conda_build/main_build.py", line 312, in args_func
    args.func(args, p)
  File "/opt/conda/lib/python2.7/site-packages/conda_build/main_build.py", line 244, in execute
    m = MetaData(recipe_dir)
  File "/opt/conda/lib/python2.7/site-packages/conda_build/metadata.py", line 250, in __init__
    self.parse_again()
  File "/opt/conda/lib/python2.7/site-packages/conda_build/metadata.py", line 258, in parse_again
    self.meta = parse(get_contents(self.meta_path))
  File "/opt/conda/lib/python2.7/site-packages/conda_build/metadata.py", line 234, in get_contents
    contents = template.render(environment=env)
  File "/opt/conda/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/opt/conda/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/darren/Projects/hexrd/conda.recipe/meta.yaml", line 4, in top-level template code
    version: {{ environ['GIT_DESCRIBE_TAG'][1:]+environ.get('GIT_DESCRIBE_NUMBER', '') }}
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'GIT_DESCRIBE_TAG'
```

Inspecting `environ.py:get_dict`, which calls `os.path.join(d['SRC_DIR'], '.git')`, I see the value of that path is `/home/darren/conda-bld/work/PyQt-x11-gpl-4.11.2/.git`, which is the previous conda package I attempted to build, but the build failed. I deleted my old `conda-bld/work`, and when I tried again to build the hexrd recipe, conda-build looked for `/home/darren/conda-bld/work/.git`. It appears that conda-build is not unpacking the source into conda-bld/work before attempting to get environment information that requires the source.

```
$ conda info
Current conda install:

             platform : linux-64
        conda version : 3.7.3
  conda-build version : 1.9.1
       python version : 2.7.8.final.0
     requests version : 2.5.0
     root environment : /opt/conda  (read only)
  default environment : /opt/conda
     envs directories : /home/darren/.conda/envs
                        /home/darren/envs
                        /opt/conda/envs
        package cache : /home/darren/.conda/envs/.pkgs
                        /home/darren/envs/.pkgs
                        /opt/conda/pkgs
         channel URLs : http://conda.binstar.org/CHESS/linux-64/
                        http://conda.binstar.org/dsdale24/linux-64/
                        http://conda.binstar.org/praxes/linux-64/
                        http://repo.continuum.io/pkgs/free/linux-64/
                        http://repo.continuum.io/pkgs/pro/linux-64/
          config file : /home/darren/.condarc
    is foreign system : False
```



Discussion:
Just discovered, I can reproduce this with http://github.com/praxes/hexrd master if I delete my `~/conda-bld` and then try to run `conda build conda.recipe`, I see the error I posted above.

If I then modify `meta.yaml` to contain a hardcoded version, begin `conda build` (can even abort before it finishes), then reintroduce the `GIT_DESCRIBE` version info (for example `git reset --hard`) into `meta.yaml`, and try `conda build` again, it will build successfully.


Resolving patch:
diff --git a/conda_build/build.py b/conda_build/build.py
--- a/conda_build/build.py
+++ b/conda_build/build.py
@@ -322,7 +322,7 @@ def build(m, get_src=True, verbose=True, post=None):
 
         # Display the name only
         # Version number could be missing due to dependency on source info.
-        print("BUILD START:", m.dist())
+        print("BUILD START:", m.name())
         create_env(config.build_prefix,
                    [ms.spec for ms in m.ms_depends('build')],
                    verbose=verbose)
diff --git a/conda_build/environ.py b/conda_build/environ.py
--- a/conda_build/environ.py
+++ b/conda_build/environ.py
@@ -31,9 +31,17 @@ def get_sp_dir():
 
 def get_git_build_info(src_dir):
     env = os.environ.copy()
-    env['GIT_DIR'] = join(src_dir, '.git')
-
     d = {}
+    git_dir = join(src_dir, '.git')
+    if os.path.isdir(git_dir):
+        env['GIT_DIR'] = git_dir
+    else:
+        d['GIT_DIR'] = ''
+        d['GIT_DESCRIBE_TAG'] = ''
+        d['GIT_DESCRIBE_NUMBER'] = ''
+        d['GIT_DESCRIBE_HASH'] = ''
+        return d
+
     # grab information from describe
     key_name = lambda a: "GIT_DESCRIBE_{}".format(a)
     keys = [key_name("TAG"), key_name("NUMBER"), key_name("HASH")]
@@ -87,8 +95,7 @@ def get_dict(m=None, prefix=None):
     except NotImplementedError:
         d['CPU_COUNT'] = "1"
 
-    if os.path.isdir(os.path.join(d['SRC_DIR'], '.git')):
-        d.update(**get_git_build_info(d['SRC_DIR']))
+    d.update(**get_git_build_info(d['SRC_DIR']))
 
     if sys.platform == 'win32':         # -------- Windows
         d['PATH'] = (join(prefix, 'Library', 'bin') + ';' +


Test patch: