Q:

Error MSB4057 for msbuild targets with period in name

I have a UI to call msbuild on a solution, and I want to be able to check some boxes in a UI to specify which projects to build in the solution.  I'm using msbuild like this:
msbuild mysolution.sln /t:"proj1" /t:"proj2"

Everything works fine as long as the projects are alphanumeric.  But when I get a project which has a period in the name:
msbuild mysolution.sln /t:"ABC.XYZ"

I get an error like this (skipping some irrelevant details with ...):
Microsoft (R) Build Engine version 14.0.25420.1
...

Project "C:\...\mysolution.sln" on node 1 (ABC.XYZ target(s))
ValidateSolutionConfiguration:
Building solution configuration "Debug|Any CPU"
C:\...\mysolution.sln.metaproj : error MSB4057: The target "ABC.XYZ" does not exist in the project. [c:\...\mysolution.sln]

Obviously in this example, project ABC.XYZ does exist in mysolution.sln.
Is this just a bug with no workaround?  Or is it expected behavior, and I need to mangle the name in some way for msbuild to understand what I'm sending?

A:

I found the solution in the msbuild source code within the method static private string CleanseProjectName(string projectName):
https://github.com/Microsoft/msbuild -> ProjectInSolution.cs#L340
We can see that it mangles the Target names, specifically, it replaces:
private static readonly char[] charsToCleanse = { '%', '$', '@', ';', '.', '(', ')', '\'' };

with:
private const char cleanCharacter = '_';

So a target name such as ABC.XYZ would need to be encoded as ABC_XYZ, otherwise it will not be found.
[2]:

