Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ermshiperete opened this issue Jul 2, 2018 · 15 comments
Open

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

ermshiperete opened this issue Jul 2, 2018 · 15 comments
Labels
Mono Mono-specific issues. Also see https://github.com/mono/msbuild xplat

Comments

@ermshiperete
Copy link

ermshiperete commented Jul 2, 2018

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
Copy link
Member

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?

@rainersigwald rainersigwald added xplat Mono Mono-specific issues. Also see https://github.com/mono/msbuild labels Jul 2, 2018
@slide
Copy link

slide commented Aug 1, 2018

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
Copy link
Member

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

@lassana
Copy link

lassana commented Jun 7, 2019

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
Copy link

jinek commented Feb 4, 2022

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
Copy link

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
Copy link

lonix1 commented Jun 3, 2022

Anyone have a workaround?

@FlorianRappl
Copy link

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
Copy link

lonix1 commented Jun 7, 2022

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
Copy link
Contributor

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.

@tushargw
Copy link

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
Copy link
Author

@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
Copy link

lucicam commented Feb 21, 2023

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

mwerle added a commit to bfishman/Docking-Port-Alignment-Indicator that referenced this issue Mar 4, 2023
There's a bug/feature in msbuild for Linux which "helpfully" replaces
backslashes with forward slashes while parsing project files. While in
general this is useful for cross-platform compatibility with filenames,
it is a major issue when using regular expressions or, indeed, any
other situation in which a backslash is explicitly required.

See : dotnet/msbuild#3468
mwerle added a commit to bfishman/Docking-Port-Alignment-Indicator that referenced this issue Mar 4, 2023
There's a bug/feature in msbuild for Linux which "helpfully" replaces
backslashes with forward slashes while parsing project files. While in
general this is useful for cross-platform compatibility with filenames,
it is a major issue when using regular expressions or, indeed, any
other situation in which a backslash is explicitly required.

See : dotnet/msbuild#3468
@lonix1
Copy link

lonix1 commented Dec 30, 2023

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. 😡

@alexrp
Copy link

alexrp commented Apr 24, 2024

Hit this as well: vezel-dev/zig-sdk#121

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Mono Mono-specific issues. Also see https://github.com/mono/msbuild xplat
Projects
None yet
Development

No branches or pull requests