June 13th, 2018

vswhere now supports -utf8 to override console code page

Heath Stewart
Principal Software Engineer

A new switch parameter has been introduced to vswhere. You can now pass -utf8 to force output to write text using UTF-8 to the console, though it’s important to note that what the console host does with that encoded can vary greatly. powershell.exe, for example, when redirecting to a file will always encode Unicode regardless of the [Console]::OutputEncoding or program output encoding.

The problem is that while Visual Studio 2017 instance data is stored as UTF-8 and the query API parses this text into Unicode strings in memory, vswhere writes these strings using the console APIs which transcode text based on the active code page or text written to a file or to a pipe is incorrectly transcoded into gibberish…or at least a bunch of question marks (fallback character). After much discussion, to avoid breaking changes the new switch parameter -utf8 was added to force text to use UTF-8 expecting that the caller will handle it appropriately as in the following example.

const cp = require('child_process');
cp.execFile('vswhere.exe', ['-latest', '-format', 'json', '-utf8'], (err, stdout, stderr) => {
  var instances = JSON.parse(stdout);
  if (instances.length > 0) {
    // do something with instances[0].description
  }
});

Install vswhere and give it a try.

Author

Heath Stewart
Principal Software Engineer

Heath is an application architect and developer, looking to help educate others to learn professional development. Besides designing and developing applications he enjoys writing about intermediate and advanced topics. Heath also consults for deployment packages and scenarios within Microsoft and for external customers.

0 comments

Discussion are closed.