Skip to content

.NET Framework MSBuild doesn't cast char to string in property functions #9976

Open
@rainersigwald

Description

@rainersigwald

Consider

<Project>
  <Target Name="Go">
    <Warning Text="$(P.EndsWith($([System.IO.Path]::DirectorySeparatorChar)))" />
  </Target>
</Project>

This works with .NET 8 MSBuild:

dotnet msbuild .\test.proj -p:P=\Some\Path\
MSBuild version 17.10.0-preview-24162-02+0326fd7c9 for .NET
  test succeeded with warnings (0.0s)
    S:\repro\dotnet\razor\pull\10220\test.proj(3,5): warning : True [S:\repro\dotnet\razor\pull\10220\test.proj]

Build succeeded with warnings in 0.0sdotnet msbuild .\test.proj -p:P=\Some\Path
MSBuild version 17.10.0-preview-24162-02+0326fd7c9 for .NET
  test succeeded with warnings (0.0s)
    S:\repro\dotnet\razor\pull\10220\test.proj(3,5): warning : False [S:\repro\dotnet\razor\pull\10220\test.proj]

Build succeeded with warnings in 0.0s

But MSBuild.exe doesn't like it:

msbuild .\test.proj -p:P=\Some\Path\
  test failed with 1 error(s) (0.0s)
    S:\repro\dotnet\razor\pull\10220\test.proj(3,14): error MSB4186: Invalid static method invocation syntax: "P.EndsWith($([System.IO.Path]::DirectorySeparatorChar))". Object of type 'System.Char' cannot be converted to type 'System.String'. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(`a`, `b`)). Check that all parameters are defined, are of the correct type, and are specified in the right order.

Build failed with 1 error(s) in 0.0s

(seen while investigating dotnet/razor#10220)

Workaround

You can wrap the char-generating method:

diff --git a/test.proj b/test.proj
index 87fa27a..d08cd98 100644
--- a/test.proj
+++ b/test.proj
@@ -1,5 +1,5 @@
 <Project>
   <Target Name="Go">
-    <Warning Text="$(P.EndsWith($([System.IO.Path]::DirectorySeparatorChar)))" />
+    <Warning Text="$(P.EndsWith($([System.String]::new($([System.IO.Path]::DirectorySeparatorChar)))))" />
   </Target>
 </Project>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: LanguageIssues impacting the MSBuild programming language.Priority:2Work that is important, but not critical for the releasebacklogbugtriaged

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions