CMake fails to find VS 2019 Build tools in Windows Docker container
When trying to use CMake inside of a Windows Docker container, CMake will always fail to find Visual Studio 2019 Build Tools.
Issue
Using this Dockerfile:
# escape=`
# initial setup taken from https://docs.microsoft.com/en-us/visualstudio/install/build-tools-container?view=vs-2019
# Use the latest Windows Server Core image with .NET Framework 4.7.
FROM mcr.microsoft.com/dotnet/framework/runtime:4.7.2-windowsservercore-ltsc2019
# Restore the default Windows shell for correct batch processing.
SHELL ["cmd", "/S", "/C"]
# Download the Build Tools bootstrapper.
ADD https://aka.ms/vs/16/release/vs_buildtools.exe C:\TEMP\vs_buildtools.exe
# Install Build Tools excluding workloads and components with known issues.
RUN C:\TEMP\vs_buildtools.exe --quiet --wait --norestart --nocache `
--installPath C:\BuildTools `
--all `
--remove Microsoft.VisualStudio.Component.Windows10SDK.10240 `
--remove Microsoft.VisualStudio.Component.Windows10SDK.10586 `
--remove Microsoft.VisualStudio.Component.Windows10SDK.14393 `
--remove Microsoft.VisualStudio.Component.Windows81SDK `
|| IF "%ERRORLEVEL%"=="3010" EXIT 0
# Start developer command prompt with any other commands specified.
ENTRYPOINT C:\BuildTools\Common7\Tools\VsDevCmd.bat -arch=amd64 &&
SHELL ["powershell"]
RUN iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System' -y --no-progress
COPY . C:\src
WORKDIR C:\src\build
# Issue occurs here
RUN cmake .. -G 'Visual Studio 16 2019'
# Default to PowerShell if no other command specified.
CMD ["powershell.exe", "-NoLogo", "-ExecutionPolicy", "Bypass"]
Building inside the CMake repository using:
docker build -t cmake-builder:latest .
Results in the following output from CMake:
CMake Error at CMakeLists.txt:7 (project):
Generator
Visual Studio 16 2019
could not find any instance of Visual Studio.
-- Configuring incomplete, errors occurred!
See also "C:/src/build/CMakeFiles/CMakeOutput.log".
CMakeOutput.log contents:
The system is: Windows - 10.0.17763 - AMD64
This occurs with both CMake 3.14.3, as well as the latest master branch (as of commit c6f6e2b3).
Analysis
Debugging the issue shows that in cmVSSetupHelper.cxx, cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
, the call to enumerate Visual Studio instances (SmartCOMPtr<ISetupConfiguration2>->EnumInstances(...)
) succeeds, but returns an empty list.
Environment Variables are set as expected:
Variable | Value |
---|---|
VS160COMNTOOLS | C:\BuildTools\Common7\Tools\ |
VSINSTALLDIR | C:\BuildTools\ |
VisualStudioVersion | 16.0 |
VSWhere does not find Visual Studio installation:
C:\src\build>"c:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -all
Visual Studio Locator version 2.6.7+91f4c1d09e [query version 2.0.2250.60958]
Copyright (C) Microsoft Corporation. All rights reserved.
However, VSSetup powershell tools does find the correct installation:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module VSSetup -Scope AllUsers -Force
Get-VSSetupInstance -All
InstanceId : 27b7d979
DisplayName : Visual Studio Build Tools 2019
InstallationVersion : 16.0.28803.352
InstallationPath : C:\BuildTools
InstallDate : 5/8/2019 11:15:11 AM
From this, it seems that vswhere
and cmake
use the same method of finding Visual Studio installations. However, Get-VSSetupInstance
appears to have a different and functional method of finding Visual Studio.