Repository: conan-io/conan

Issue:
[bug] Conan 2.0 fails to find Visual Studio Build Tools using the 'MSBuildToolchain'
### Environment details

* Operating System+version: Windows 10
* Compiler+version: msvc 2019 build tools
* Conan version: 2.0.0
* Python version: 3.9


### Steps to reproduce

1. Install the msvc 2019 build tools (a.k.a Visual Studio 16)
2. In a recipe, use the generator 'MSBuildToolchain'
3. and build

It fails finding the Visual Studio 16 __Build Tools__ installation with an error:

```
ERROR: Error in generator 'MSBuildToolchain': VS non-existing installation: Visual Studio 16
```

Note: I already tried to manually activate vcvars, but conan still does not find the installation.

### Logs

```
======== Finalizing install (deploy, generators) ========
conanfile.py (qt/5.15.8): Writing generators to C:\src\qt-conan\5.x.x
conanfile.py (qt/5.15.8): Generator 'MSBuildToolchain' calling 'generate()'
conanfile.py (qt/5.15.8): MSBuildToolchain created conantoolchain_release_x64.props
conanfile.py (qt/5.15.8): MSBuildToolchain writing conantoolchain.props
conanfile.py (qt/5.15.8): ERROR: Traceback (most recent call last):
  File "c:\local\miniforge3\lib\site-packages\conans\client\generators\__init__.py", line 70, in write_generators
    generator.generate()
  File "c:\local\miniforge3\lib\site-packages\conan\tools\microsoft\toolchain.py", line 104, in generate
    VCVars(self._conanfile).generate()
  File "c:\local\miniforge3\lib\site-packages\conan\tools\microsoft\visual.py", line 139, in generate
    vcvars = vcvars_command(vs_version, architecture=vcvarsarch, platform_type=None,
  File "c:\local\miniforge3\lib\site-packages\conan\tools\microsoft\visual.py", line 216, in vcvars_command
    cmd.append('call "%s" ' % _vcvars_path(version, vs_install_path))
  File "c:\local\miniforge3\lib\site-packages\conan\tools\microsoft\visual.py", line 232, in _vcvars_path
    raise ConanException("VS non-existing installation: Visual Studio %s" % version)
conans.errors.ConanException: VS non-existing installation: Visual Studio 16

ERROR: Error in generator 'MSBuildToolchain': VS non-existing installation: Visual Studio 16
```


Discussion:
Note, in conan 1.x, the recipe used `tools.vcvars()` to activate the build environment. This worked very well!

As this is not awailable anymore, I tried to use the MSBuildToolchain and NMakeToolchain, both with the error described above.
Hi @rainman110 

Thanks for reporting this, this is really unexpected, and not sure why it is not working, the underlying code that does the VS detection based on vswhere is the same as in 1.X.

Could you please try to define in your profile ``tools.microsoft.msbuild:installation_path`` to point directly to your VS 2019 installation, to see what happens? (that is an override of the automatic ``vswhere`` detection)
Should this be the path to vcvarsall.bat or where should it point to?
It should be the base installation path, something like:
```
>>> from conans.client.conf.detect_vs import vs_installation_path as vs
>>> vs("16")
'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community'
```
@memsharded I am a bit puzzled. It does not accept this config variable when defining it in the profile.

I need to set it as a config variable.

`conan config set` does not exists anymore though

Nevermind, I just had to add it to the `[conf]` section, not to the `[settings]` section of the profile.

Now, the error is gone. I will see, wether it works now.
Just for future reference, the correct settings needs to be:

```
[conf]
tools.microsoft.msbuild:installation_path=C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools
```
Do you have installed exclusively the build-tools, but not the full VS IDE?
In that case, it is possible that the auto-detection/vswhere is not prepared for that. It would be great to know a bit more about the setup, how did you installed the BuildTools only. Do you even have ``vswhere`` installed?
Exactly, I only used the "free" buildtools, no IDE is installed. It comes with the compiler, MsBuild, WinSDK etc.
Is vswhere only called when creating the profile via detect? If so, where is the installation_path actually stored in the profile after creating it?
@memsharded In fact, vswhere does not list my visual studio build tools installation.

I can live for now with the workaround though! 
However, if I pass "*" to the products arguments, it will find the build tools:

```
(base) C:\src\qt-conan>"c:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"  -products * -format json
[
  {
    "instanceId": "4a629174",
    "installDate": "2022-02-10T23:39:11Z",
    "installationName": "VisualStudio/16.11.10+32126.315",
    "installationPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools",
    "installationVersion": "16.11.32126.315",
    "productId": "Microsoft.VisualStudio.Product.BuildTools",
    "productPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\Common7\\Tools\\LaunchDevCmd.bat",
    "state": 4294967295,
    "isComplete": true,
    "isLaunchable": true,
    "isPrerelease": false,
    "isRebootRequired": false,
    "displayName": "Visual Studio Build Tools 2019",
    "description": "Die Visual Studio Build Tools ermöglichen Ihnen die Erstellung nativer und verwalteter MSBuild-basierter Anwendungen, ohne dass die Visual Studio-IDE erforderlich ist. Es stehen Optionen zur Installation von Visual C++-Compilern und -Bibliotheken, MFC, ATL sowie C++/CLI-Unterstützung zur Verfügung.",
    "channelId": "VisualStudio.16.Release",
    "channelUri": "https://aka.ms/vs/16/release/channel",
    "enginePath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\resources\\app\\ServiceHub\\Services\\Microsoft.VisualStudio.Setup.Service",
    "releaseNotes": "https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-v16.11#16.11.10",
    "thirdPartyNotices": "https://go.microsoft.com/fwlink/?LinkId=660909",
    "updateDate": "2022-02-10T23:39:11.2650007Z",
    "catalog": {
      "buildBranch": "d16.11",
      "buildVersion": "16.11.32126.315",
      "id": "VisualStudio/16.11.10+32126.315",
      "localBuild": "build-lab",
      "manifestName": "VisualStudio",
      "manifestType": "installer",
      "productDisplayVersion": "16.11.10",
      "productLine": "Dev16",
      "productLineVersion": "2019",
      "productMilestone": "RTW",
      "productMilestoneIsPreRelease": "False",
      "productName": "Visual Studio",
      "productPatchVersion": "10",
      "productPreReleaseMilestoneSuffix": "1.0",
      "productSemanticVersion": "16.11.10+32126.315",
      "requiredEngineVersion": "2.11.52.58712"
    },
    "properties": {
      "campaignId": "",
      "channelManifestId": "VisualStudio.16.Release/16.11.10+32126.315",
      "nickname": "",
      "setupEngineFilePath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\setup.exe"
    }
  },
  {
    "instanceId": "8c55b13a",
    "installDate": "2021-10-27T07:14:48Z",
    "installationName": "VisualStudio/15.0.0+26228.98",
    "installationPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools",
    "installationVersion": "15.0.26228.98",
    "productId": "Microsoft.VisualStudio.Product.BuildTools",
    "state": 4294967295,
    "isComplete": true,
    "isLaunchable": true,
    "isPrerelease": false,
    "isRebootRequired": false,
    "displayName": "Visual Studio Build Tools 2017",
    "description": "The Visual Studio Build Tools allows you to build native and managed MSBuild-based applications without requiring the Visual Studio IDE. There are options to install the Visual C++ compilers and libraries, MFC, ATL, and C++/CLI support.",
    "channelId": "VisualStudio.15.Release",
    "channelPath": "C:\\Users\\otte_to-a\\AppData\\Local\\Microsoft\\VisualStudio\\Packages\\_Channels\\4CB340F5\\install_catalog.json",
    "channelUri": "https://aka.ms/vs/15/release/channel",
    "enginePath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\resources\\app\\ServiceHub\\Services\\Microsoft.VisualStudio.Setup.Service",
    "installChannelUri": "https://aka.ms/vs/15/release/cf7baf3ed_-1205811054/channel",
    "releaseNotes": "https://go.microsoft.com/fwlink/?LinkId=659724",
    "updateDate": "2022-01-05T14:30:46.1331746Z",
    "catalog": {
      "buildBranch": "d15rtwsvc",
      "buildVersion": "15.0.26228.98",
      "id": "VisualStudio/15.0.0+26228.98",
      "localBuild": "build-lab",
      "manifestName": "VisualStudio",
      "productDisplayVersion": "15.0 (26228.98)",
      "productLine": "Dev15",
      "productLineVersion": "2017",
      "productMilestone": "RTW",
      "productName": "Visual Studio",
      "productPatchVersion": "0",
      "productRelease": "RTW",
      "productSemanticVersion": "15.0.0+26228.98"
    },
    "properties": {
      "campaignId": "",
      "channelManifestId": "",
      "nickname": "",
      "setupEngineFilePath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\Installer\\vs_installershell.exe"
    }
  }
]
```
That is interesting. We indeed have:
```python
 try:
        legacy_products = vswhere(legacy=True)
        all_products = vswhere(products=["*"])
        products = legacy_products + all_products
    except ConanException:
        products = None
```
So the product=* is there, maybe the order needs to be reversed? 

The ``vswhere`` is not called for the profile detect only, but in every ``vcvars`` creation.
Could you  please point to the MSBuild thing to install only the tools? not evident in the msft site.
> Could you please point to the MSBuild thing to install only the tools? not evident in the msft site.

Sorry, I could not understand what you want me to do 😅 
It seems to raise an exception at line 

```
all_products = vswhere(products=["*"]
```

so, all_products is not set .
A link to download the MSBuildTools-only installer 😄 

Sure, let me see, whether I still can find it.
So the old 2019 build tools installer is not provided anymore. 

Still, you can install only the build tools 2019 via the 2022 community installer:

![image](https://user-images.githubusercontent.com/3213107/222119423-7753be0c-e178-480e-95a1-8e61ef94447f.png)

I started debugging the code. It raises an exception at line 

```
all_products = vswhere(products=["*"])
```

which is

```
vswhere error: 'utf-8' codec can't decode byte 0x94 in position 717: invalid start byte
```

__Update__: The exception comes from the line ```output = check_output_runner(cmd).strip()```
Okay... The problem is the encoding of the vswhere output, which contains letters such as "ö". See output above.
On windows, the encoding needs to be "ansi"
If you want, I'll provide a PR, including a potential fix

Resolving patch:
diff --git a/conans/client/conf/detect_vs.py b/conans/client/conf/detect_vs.py
--- a/conans/client/conf/detect_vs.py
+++ b/conans/client/conf/detect_vs.py
@@ -114,6 +114,7 @@ def vswhere(all_=False, prerelease=False, products=None, requires=None, version=
     arguments.append(vswhere_path)
 
     # Output json format
+    arguments.append("-utf8")
     arguments.append("-format")
     arguments.append("json")
 


Test patch: