Skip to content

WriteCodeFragment fails in .NET Core MSBuild if input has braces #937

@nguerrera

Description

@nguerrera

Repro steps

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Description>There's a bug {here}</Description>
  </PropertyGroup>
</Project>

dotnet build

Expected

Builds successfully

Actual

error MSB4018: The "WriteCodeFragment" task failed unexpectedly.
error MSB4018: System.FormatException: Input string was not in a correct format.
error MSB4018:    at System.Text.StringBuilder.FormatError()
error MSB4018:    at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
error MSB4018:    at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
error MSB4018:    at System.String.Format(String format, Object[] args)
error MSB4018:    at Microsoft.Build.Tasks.WriteCodeFragment.GenerateCodeCoreClr(String& extension)
error MSB4018:    at Microsoft.Build.Tasks.WriteCodeFragment.Execute()

The best fix for this would be to remove the CODEDOM ifdefs and use the same code as .NET Framework in .NET Core msbuild. A regression test should also be added.

Original bug description, focusing more on code inspection than scenario

By code inspection, it appears that the inputs to WriteCodeFragment inputs aren't escaped in the xplat (non-CodeDOM) implementation.

See https://github.com/nguerrera/msbuild/blob/71bf8112fbb1ec2115631c9d2e8db3412004ff5e/src/XMakeTasks/WriteCodeFragment.cs#L360

I believe this will fail if _ParameterN has text that needs to be escaped. (e.g. imagine if there are embedded quotes in the value.) EDIT: Quotes and other things were fixed in #1369, but braces are still a problem as they throw off String.Format.

I also don't understand the non-string side of that ?: expression because the CodeDOM equivalent casts unconditionally to string and why do we expect non-strings to be quoted in their ToString() output?

Spotted while investigating #935

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: TasksIssues impacting the tasks shipped in Microsoft.Build.Tasks.Core.dll.help wantedIssues that the core team doesn't plan to work on, but would accept a PR for. Comment to claim.triagedxplat

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions