dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.17k stars 1.34k forks source link

Backslash in property gets converted to forward slash on Linux #3468

Open ermshiperete opened 6 years ago

ermshiperete commented 6 years ago

When a property contains backslashs in a project file, it will be converted to forward slashs when running with msbuild on Linux.

While this would be fine for paths (although one could just as well use forward slash in the project file since that works on both Windows and Linux), it causes problems when trying to set a regular expression in a property which requires a backslash to escape the next character.

Steps to reproduce

Project file bug.proj:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Regex>\(([^]]+)\)</Regex>
    </PropertyGroup>

    <Target Name="Build">
        <Message Text="Regex=$(Regex)"/>
    </Target>
</Project>

Command line

msbuild /t:Build bug.proj

Expected behavior

Outputs the line

Regex=\(([^]]+)\)

Actual behavior

Outputs the line

Regex=/(([^]]+)/)

Environment data

msbuild /version output: 15.6.0.0

Also happens with xbuild from older Mono versions.

When building with dotnet build -v d bug.proj the output is as expected.

OS Info: Ubuntu 16.04.4 LTS

rainersigwald commented 6 years ago

It's interesting that .NET Core MSBuild doesn't do the same thing:

raines@raines-z220u:~$ msbuild bug.proj
Microsoft (R) Build Engine version 15.6.0.0 ( Thu May 10 14:00:26 UTC 2018) for Mono
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 7/2/2018 10:13:27 AM.
Project "/home/raines/bug.proj" on node 1 (default targets).
Build:
  Regex=/(([^]]+)/)
Done Building Project "/home/raines/bug.proj" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.22
raines@raines-z220u:~$ dotnet msbuild bug.proj 
Microsoft (R) Build Engine version 15.7.179.6572 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Regex=\(([^]]+)\)

@radical Do you recall anything that might explain this difference?

slide commented 5 years ago

This also seems to happen with the Exec task as well

<Exec Command="perl -pi -e 's/\r\n|\n|\r/\n/g' %(StdLibOutputFiles.Identity)" />

I get the error:

The command "perl -pi -e 's//r/n|/n|/r//n/g' abc.py" exited with code 255

Would be nice to have an attribute or something that turned off the replacement of backslashes.

akoeplinger commented 5 years ago

@radical any idea why this is happening only on our fork?

lassana commented 5 years ago

Same problem on Mac.

I have the following statement in my Xamarin.Android app:

<XmlPoke
    XmlInputPath="Resources\values\Strings.xml"
    Query="/resources/string[@name = 'asset_statements']"
    Value=" [{ \&quot;include\&quot;: \&quot;https://$(AppAssociatedDomain)/.well-known/assetlinks.json\&quot; }]" />

The result should be:

[{ \"include\": \"https://www.example.com/.well-known/assetlinks.json\" }]

But in fact it is:

[{ /"include/": /"https://www.example.com/.well-known/assetlinks.json/" }]

Any workaround?

My environment: Runtime: `Mono 5.20.1.19 (2018-10/886c4901747) (64-bit)`, Package version: `520010019`. ``` $ msbuild -version Microsoft (R) Build Engine version 16.0.42-preview+g804bde742b for Mono Copyright (C) Microsoft Corporation. All rights reserved. 16.0.42.19328 ```
jinek commented 2 years ago

I have related problem embedding resources:

        <EmbeddedResource Include="@(XsdSchema)">
            <LogicalName>$([System.String]::Copy(%(Identity)).Replace('/','\'))</LogicalName>
        </EmbeddedResource>

Even after I call replace, it still /

FlorianRappl commented 2 years ago

This is a huge issue also for me. Not sure why it is not addressed. For me, its either in the WriteLinesToFile or Copy task...

lonix1 commented 2 years ago

Anyone have a workaround?

FlorianRappl commented 2 years ago

Yes @lonix1

Instead of using the inbuilt task(s) you can create your own task and use this one instead. Advantage here is that you can potentially customize / improve the flow even more. Disadvantage is the additional maintenance and complexity.

lonix1 commented 2 years ago

you can create your own task

Thanks for that nice workaround, I'm going to try it!

Disadvantage is the additional maintenance and complexity.

Agreed. This is a nasty bug. Who doesn't use linux for devops these days? Every time I encounter this bug, I resort to some hacky workaround. And scripts often fail and it takes me ages to debug the problem.

Please attend to this, it's really a big one. Thanks!

Adam-S-Daniel commented 1 year ago

Workaround found! Use %5C instead of \

In the related (duplicate IMO) issue https://github.com/dotnet/msbuild/issues/1622, @r3ho discovered it does the trick in task attribute values and @lordscarlet found that it also works in property values.

tushargw commented 1 year ago

Workaround found! Use %5C instead of \

In the related (duplicate IMO) issue #1622, @r3ho discovered it does the trick in task attribute values and @lordscarlet found that it also works in property values.

I am having a similar issue. The "Dotnet test" call is being made in the CI/CD pipeline. And Azure DevOps is stripping the " no matter what I do. I don't think this is a \ problem here though, but a " problem. And this happens irrespective of windows or ubuntu machine.

ermshiperete commented 1 year ago

@tushargw This sounds like a different problem. I'd recommend opening a new issue for that (of course unless there's already a better fitting existing one).

lucicam commented 1 year ago

Workaround found! Use %5C instead of \

In the related (duplicate IMO) issue #1622, @r3ho discovered it does the trick in task attribute values and @lordscarlet found that it also works in property values.

%5C does not work, it's still replaced by forward slash in my case

lonix1 commented 6 months ago

Just hit this bug again, and again spent almost a day on it.

The %5C trick doesn't work when the slashes are inside a file processed by msbuild. e.g. I'm using msbuild to concat javascript files, which use backslashes for regexes, and those are mangled by msbuild into forward slashes which are basically comments, and thus breaks the script.

No workaround other than a complete redesign. :rage:

alexrp commented 2 months ago

Hit this as well: https://github.com/vezel-dev/zig-sdk/issues/121

Would really like to see a better answer to this than "write an inline task"...