Repository: conda/conda-build

Issue:
conda-build does not handle absence of MSVC on windows well
I was trying to build the **math3d** package inside conda, and I am failing and getting the following error

C:>conda skeleton pypi --recursive math3d --version 3.1.2 --output-dir C:\Anaconda2\skeleton
Using url https://files.pythonhosted.org/packages/1e/93/d0a462fc3acb5d9a71912129f9c72d448cd995ed98c632c729c6aa63d564/math3d-3.1.2.tar.gz (24 KB) for math3d.
Downloading math3d
Using cached download
Unpacking math3d...
done
working in c:\users\debang~1\appdata\local\temp\tmpsbez9sconda_skeleton_math3d-3.1.2.tar.gz
Fetching package metadata .........
Solving package specifications: ..........

The following NEW packages will be INSTALLED:

```
mkl:    11.3.3-1
numpy:  1.11.0-py27_1
pyyaml: 3.11-py27_4
yaml:   0.1.6-0
```

Linking packages ...
[      COMPLETE      ]|##################################################| 100%
Applying patch: 'c:\users\debang~1\appdata\local\temp\tmpsbez9sconda_skeleton_math3d-3.1.2.tar.gz\pypi-distutils.patch'
patching file core.py
Hunk #1 succeeded at 169 with fuzz 2 (offset 2 lines).
Writing recipe for math3d
Done

C:>conda build C:\Anaconda2\skeleton math3d
Removing old build environment
BUILD START: math3d-3.1.2-py27_0
Fetching package metadata .........
Solving package specifications: ..........

The following NEW packages will be INSTALLED:

```
pip:            8.1.2-py27_0
python:         2.7.11-4
setuptools:     23.0.0-py27_0
vs2008_runtime: 9.00.30729.1-1
wheel:          0.29.0-py27_0
```

Linking packages ...
[      COMPLETE      ]|##################################################| 100%
Source cache directory is: C:\Anaconda2\conda-bld\src_cache
Found source in cache: math3d-3.1.2.tar.gz
Extracting download
Package: math3d-3.1.2-py27_0
source tree in: C:\Anaconda2\conda-bld\work\math3d-3.1.2
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 "C:\Anaconda2\Scripts\conda-build-script.py", line 4, in <module>
    sys.exit(main())
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 144, in main
    args_func(args, p)
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 389, in args_func
    args.func(args, p)
  File "C:\Anaconda2\lib\site-packages\conda_build\main_build.py", line 332, in execute
    dirty=args.dirty, activate=args.activate)
  File "C:\Anaconda2\lib\site-packages\conda_build\build.py", line 563, in build
    import conda_build.windows as windows
  File "C:\Anaconda2\lib\site-packages\conda_build\windows.py", line 30, in <module>
    'installationfolder')
  File "C:\Anaconda2\lib\distutils\msvc9compiler.py", line 73, in get_value
    raise KeyError(key)
KeyError: 'installationfolder'

C:>conda info
Current conda install:

```
         platform : win-64
    conda version : 4.1.3
conda-env version : 2.5.1
```

  conda-build version : 1.21.2
       python version : 2.7.11.final.0
     requests version : 2.7.0
     root environment : C:\Anaconda2  (writable)
  default environment : C:\Anaconda2
     envs directories : C:\Anaconda2\envs
        package cache : C:\Anaconda2\pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/win-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/win-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
          config file : None
         offline mode : False
    is foreign system : False 



Discussion:
Sorry, you're seeing this error because I didn't anticipate people not having Visual Studio installed.  For now, I recommend downgrading to an earlier conda-build version.  1.21.0 should work for you.  I will fix this as soon as I can.

Thanks! It works with conda-build 1.21.0

We at Intel have Visual Studio (multiple versions) installed and also see this problem. Downgraded to 1.21.0 for now as a workaround

Thanks @tomashek - https://github.com/conda/conda-build/pull/1049 should fix this issue.  I'm wrapping up a couple of other things, and will push out a new tag in the next 1-2 days.

Also see this with Visual Studio installed.

Fixed by #1049 - release coming soon.

Thanks!

I am getting almost the same error when building for python 3.3 and 3.4:

```
Traceback (most recent call last):
  File "C:\Miniconda2\Scripts\conda-build-script.py", line 4, in <module>
    sys.exit(main())
  File "C:\Miniconda2\lib\site-packages\conda_build\main_build.py", line 144, in main
    args_func(args, p)
  File "C:\Miniconda2\lib\site-packages\conda_build\main_build.py", line 389, in args_func
    args.func(args, p)
  File "C:\Miniconda2\lib\site-packages\conda_build\main_build.py", line 248, in execute
    set_language_env_vars(args, parser, execute=execute)
  File "C:\Miniconda2\lib\site-packages\conda_build\render.py", line 45, in set_language_env_vars
    execute(args, parser)
  File "C:\Miniconda2\lib\site-packages\conda_build\main_build.py", line 332, in execute
    dirty=args.dirty, activate=args.activate)
  File "C:\Miniconda2\lib\site-packages\conda_build\build.py", line 564, in build
    windows.build(m, build_file, dirty=dirty, activate=activate)
  File "C:\Miniconda2\lib\site-packages\conda_build\windows.py", line 225, in build
    fo.write(msvc_env_cmd(bits=cc.bits, override=m.get_value('build/msvc_compiler', None)))
  File "C:\Miniconda2\lib\site-packages\conda_build\windows.py", line 130, in msvc_env_cmd
    'installationfolder')
  File "C:\Miniconda2\lib\distutils\msvc9compiler.py", line 73, in get_value
    raise KeyError(key)
KeyError: 'installationfolder'
```

conda info:

```
Current conda install:

             platform : win-64
        conda version : 4.1.4
    conda-env version : 2.5.1
  conda-build version : 1.21.3
       python version : 2.7.11.final.0
     requests version : 2.10.0
     root environment : C:\Miniconda2  (writable)
  default environment : C:\Miniconda2
     envs directories : C:\Miniconda2\envs
        package cache : C:\Miniconda2\pkgs
         channel URLs : https://repo.continuum.io/pkgs/free/win-64/
                        https://repo.continuum.io/pkgs/free/noarch/
                        https://repo.continuum.io/pkgs/pro/win-64/
                        https://repo.continuum.io/pkgs/pro/noarch/
          config file : C:\Miniconda2\.condarc
         offline mode : False
    is foreign system : False
```

@adament can you please also point me to your recipe (if you can), and the command you use to build things?  What compilers (if any) do you have installed?

For example when doing the math3d example from before for Python 3.3. I have Visual C++ for Python 2.7 installed and Visual Studio 2015, i.e. no apropriate Visual Studio for Python 3.3 and 3.4. I build using:

```
conda build --python=3.3
```

@adament what is your expected outcome?  I think this failure is expected.  The message could be a little more clear, but I don't expect it to work if you don't have the appropriate compiler for your platform.

Oh, wait, is math3d pure python?  So the compiler message really should not stop execution?  Sorry for my confusion.

I assumed that math3d was pure python given that it was the package in
the original bug report. I only did a quick test with it. I ran into the
problem with an internal package at work which is pure Python. A quick
browse at the windows.py code suggested that the bug fix you supplied in
2.7 was only implemented for the MSVC versions relevant for python 2.7
hence when I build with --python=all I ran into the error when it tried
to build for 3.3 and 3.4. It looked like almost the same fix applied to
line 130 should work too, though I have not tested it.

On Wed, Jun 29, 2016, at 02:13 PM, Mike Sarahan wrote:

> @adament can you please also point me to your recipe (if you can), and
> the command you use to build things?  What compilers (if any) do you have
> installed?
> 
> ---
> 
> You are receiving this because you were mentioned.
> Reply to this email directly or view it on GitHub:
> https://github.com/conda/conda-build/issues/1048#issuecomment-229338103

OK.  Thanks for your patience.  We have an internal build machine that just started showing this issue.  I'll have a fix soon.


Resolving patch:
diff --git a/conda_build/windows.py b/conda_build/windows.py
--- a/conda_build/windows.py
+++ b/conda_build/windows.py
@@ -92,15 +92,16 @@ def msvc_env_cmd(bits, override=None):
     version = None
     if override is not None:
         version = override
-        # The DISTUTILS_USE_SDK variable tells distutils to not try and validate
-        # the MSVC compiler. For < 3.5 this still forcibly looks for 'cl.exe'.
-        # For > 3.5 it literally just skips the validation logic.
-        # See distutils _msvccompiler.py and msvc9compiler.py / msvccompiler.py
-        # for more information.
-        msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
-        # This is also required to hit the 'don't validate' logic on < 3.5.
-        # For > 3.5 this is ignored.
-        msvc_env_lines.append('set MSSdk=1')
+
+    # The DISTUTILS_USE_SDK variable tells distutils to not try and validate
+    # the MSVC compiler. For < 3.5 this still forcibly looks for 'cl.exe'.
+    # For > 3.5 it literally just skips the validation logic.
+    # See distutils _msvccompiler.py and msvc9compiler.py / msvccompiler.py
+    # for more information.
+    msvc_env_lines.append('set DISTUTILS_USE_SDK=1')
+    # This is also required to hit the 'don't validate' logic on < 3.5.
+    # For > 3.5 this is ignored.
+    msvc_env_lines.append('set MSSdk=1')
 
     if not version:
         if config.PY3K and config.use_MSVC2015:
@@ -126,30 +127,34 @@ def build_vcvarsall_cmd(cmd, arch=arch_selector):
     msvc_env_lines.append('set "MSYS2_ARG_CONV_EXCL=/AI;/AL;/OUT;/out;%MSYS2_ARG_CONV_EXCL%"')
     msvc_env_lines.append('set "MSYS2_ENV_CONV_EXCL=CL"')
     if version == '10.0':
-        WIN_SDK_71_PATH = Reg.get_value(os.path.join(WINSDK_BASE, 'v7.1'),
-                                        'installationfolder')
-        WIN_SDK_71_BAT_PATH = os.path.join(WIN_SDK_71_PATH, 'Bin', 'SetEnv.cmd')
-
-        win_sdk_arch = '/Release /x86' if bits == 32 else '/Release /x64'
-        win_sdk_cmd = build_vcvarsall_cmd(WIN_SDK_71_BAT_PATH, arch=win_sdk_arch)
-
-        # There are two methods of building Python 3.3 and 3.4 extensions (both
-        # of which required Visual Studio 2010 - as explained in the Python wiki
-        # https://wiki.python.org/moin/WindowsCompilers)
-        # 1) Use the Windows SDK 7.1
-        # 2) Use Visual Studio 2010 (any edition)
-        # However, VS2010 never shipped with a 64-bit compiler, so in this case
-        # **only** option (1) applies. For this reason, we always try and
-        # activate the Windows SDK first. Unfortunately, unsuccessfully setting
-        # up the environment does **not EXIT 1** and therefore we must fall
-        # back to attempting to set up VS2010.
-        # DelayedExpansion is required for the SetEnv.cmd
-        msvc_env_lines.append('Setlocal EnableDelayedExpansion')
-        msvc_env_lines.append(win_sdk_cmd)
-        # If the WindowsSDKDir environment variable has not been successfully
-        # set then try activating VS2010
-        msvc_env_lines.append('if not "%WindowsSDKDir%" == "{}" ( {} )'.format(
-            WIN_SDK_71_PATH, build_vcvarsall_cmd(vcvarsall_vs_path)))
+        try:
+            WIN_SDK_71_PATH = Reg.get_value(os.path.join(WINSDK_BASE, 'v7.1'),
+                                            'installationfolder')
+            WIN_SDK_71_BAT_PATH = os.path.join(WIN_SDK_71_PATH, 'Bin', 'SetEnv.cmd')
+
+            win_sdk_arch = '/Release /x86' if bits == 32 else '/Release /x64'
+            win_sdk_cmd = build_vcvarsall_cmd(WIN_SDK_71_BAT_PATH, arch=win_sdk_arch)
+
+            # There are two methods of building Python 3.3 and 3.4 extensions (both
+            # of which required Visual Studio 2010 - as explained in the Python wiki
+            # https://wiki.python.org/moin/WindowsCompilers)
+            # 1) Use the Windows SDK 7.1
+            # 2) Use Visual Studio 2010 (any edition)
+            # However, VS2010 never shipped with a 64-bit compiler, so in this case
+            # **only** option (1) applies. For this reason, we always try and
+            # activate the Windows SDK first. Unfortunately, unsuccessfully setting
+            # up the environment does **not EXIT 1** and therefore we must fall
+            # back to attempting to set up VS2010.
+            # DelayedExpansion is required for the SetEnv.cmd
+            msvc_env_lines.append('Setlocal EnableDelayedExpansion')
+            msvc_env_lines.append(win_sdk_cmd)
+            # If the WindowsSDKDir environment variable has not been successfully
+            # set then try activating VS2010
+            msvc_env_lines.append('if not "%WindowsSDKDir%" == "{}" ( {} )'.format(
+                WIN_SDK_71_PATH, build_vcvarsall_cmd(vcvarsall_vs_path)))
+        # sdk is not installed.  Fall back to only trying VS 2010
+        except KeyError:
+            msvc_env_lines.append(build_vcvarsall_cmd(vcvarsall_vs_path))
     elif version == '9.0':
         # Get the Visual Studio 2008 path (not the Visual C++ for Python path)
         # and get the 'vcvars64.bat' from inside the bin (in the directory above


Test patch: