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

"Unrecognized escape sequence" when remote url is a Windows path #119

Closed
sbj42 opened this issue Aug 21, 2020 · 1 comment · Fixed by #120
Closed

"Unrecognized escape sequence" when remote url is a Windows path #119

sbj42 opened this issue Aug 21, 2020 · 1 comment · Fixed by #120

Comments

@sbj42
Copy link
Contributor

sbj42 commented Aug 21, 2020

When GitInfo generates ThisAssembly.GitInfo.g.cs, it does not escape backslashes in the RepositoryUrl field. Backslashes can appear in a remote URL if someone clones a local repository on Windows.

Please consider escaping those backslashes, or if that's not possible set RepositoryUrl to an empty sting so that the build will not fail.

Steps to reproduce:

This can be reproduced simply by setting the remote url in git to a string with backslashes. For instance, in Windows CMD:

git remote set-url origin C:\foo

But for a "real-world" scenario, you can use the following:

  1. On a Windows machine, clone https://github.com/sbj42/gitinfo-local-clone-bug (e.g. to C:\GitInfoTest)
  2. Then clone the clone (e.g. git clone C:\GitInfoTest C:\GitInfoTestClone)
  3. Open the cloned solution in Visual Studio and build it.

Expected results:

The solution should build.

The RepositoryUrl (which the program will print) should be C:\GitInfoTest. An empty string would also be acceptable, though less desirable.

Actual results:

The solution fails to build with:

1>------ Build started: Project: GitInfoTest, Configuration: Debug Any CPU ------
1>C:\GitInfoTestClone\obj\Debug\netcoreapp3.1\ThisAssembly.GitInfo.g.cs(43,46,43,48): error CS1009: Unrecognized escape sequence
1>Done building project "GitInfoTest.csproj" -- FAILED.

The problem is line 43 of ThisAssembly.GitInfo.g.cs, where the remote "url" is not properly string-escaped:

      public const string RepositoryUrl = "C:\GitInfoTest";
@sbj42
Copy link
Contributor Author

sbj42 commented Aug 21, 2020

I think this can be fixed by using "verbatim string literals" in GitInfo.cs.pp (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim). For instance, replace this line:

      public const string RepositoryUrl = "$GitRepositoryUrl$";

with this:

      public const string RepositoryUrl = @"$GitRepositoryUrl$";

(adding the @ character)

@kzu kzu closed this as completed in #120 Aug 28, 2020
@devlooped devlooped locked and limited conversation to collaborators Sep 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant