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

Fix up bugs in parsing special chracters in EditorConfig #54511

Merged
merged 4 commits into from
Jul 25, 2021

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented Jul 1, 2021

Resolves issues like dotnet/aspnetcore#33832 and #51692.

Follow-up to #52515.

Changes in this PR

  1. Avoid using the WriteLinesToFile task to write the editor config to disk and write to disk inside GenerateMSBuildConfigEditor task.

Moving from C# to MSBuild to EditorConfig causes a lot of weird issues when writing editorconfigs with special characters mostly due to MSBuild's behavior in certain scenarios.

  • The editorconfig format uses backslashes to escape characters but MSBuild automatically converts backslashes to forward slashes which breaks writing special characters to disk.
  • Semicolons aren't interpretted correctly when processed in MSBuild since they are treated as item separators.
  • Probably some other weirdness that I'm not accounting for here.

Given all this, I think it makes sense to move the file writes to inside the GenerateMSBuildConfigEditor task to avoid issues that come up because of the interop between C# strings, MSBuild strings, and editorconfig's requirements.

  1. Escape section names before comparing source paths with the associated section names in GetSectionFromSource.

Validations

  • Local unit tests
  • Ran the changeset against the https://github.com/dotnet/website project which contains an AdditionalText that falls victim to this bug (ref)
  • Ran the changeset against the Razor source generator integration tests

@captainsafia captainsafia force-pushed the safia/fix-editor-config-special-chars branch from 2455390 to b07df6e Compare July 1, 2021 19:59
@captainsafia captainsafia force-pushed the safia/fix-editor-config-special-chars branch 3 times, most recently from bfae5c7 to a62cfea Compare July 1, 2021 23:33
@captainsafia captainsafia force-pushed the safia/fix-editor-config-special-chars branch from a62cfea to 08a8780 Compare July 2, 2021 00:38
@captainsafia captainsafia marked this pull request as ready for review July 2, 2021 03:15
@captainsafia captainsafia requested a review from a team as a code owner July 2, 2021 03:15
@captainsafia
Copy link
Member Author

@dotnet/roslyn-compiler Any chance I can get a review on this? This is blocking dotnet/website adopting the new Razor source generator and has been reported by a few customers in the previews.

@cston
Copy link
Member

cston commented Jul 19, 2021

", result);

Are we verifying the contents written to FileName?


Refers to: src/Compilers/Core/MSBuildTaskTests/GenerateMSBuildEditorConfigTests.cs:33 in 08a8780. [](commit_id = 08a8780, deletion_comment = False)

return string.IsNullOrEmpty(FileName.ItemSpec) ? true : WriteMSBuildEditorConfig();
}

public bool WriteMSBuildEditorConfig()
Copy link
Member

@cston cston Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public

private or internal?


Assert.True(File.Exists(fileName));
Assert.Equal(expectedContents, File.ReadAllText(fileName));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Consider adding Assert.False(configTask.WriteMSBuildEditorConfig());

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping to test that WriteMSBuildEditorConfig() did not update the file if the contents were already correct, but it looks like It looks the method returns true in either case.

Should we add an out bool updated parameter that indicated whether the file was modified, and check that value?

}
else
{
sb.Append(lexer.CurrentCharacter);
Copy link
Member

@cston cston Jul 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sb.Append(lexer.CurrentCharacter)

Do we ever hit this code path? I may be misreading the Lex() method, but it looks like CurrentCharacter is the character after the special character in this case. Is that expected here? #Closed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. This path never gets hit. I'm not sure why I had this in the first place (perhaps something came up when I was doing the end-to-end testing with the dotnet/website repo), but it's not needed. CanGetSectionsWithSpecialCharacters still passes without this.

@RikkiGibson RikkiGibson self-assigned this Jul 19, 2021
@captainsafia
Copy link
Member Author

Can this be merged or are there other things to sort out? Hoping to ship this fix alongside rc1.

@cston cston merged commit dc25977 into main Jul 25, 2021
@ghost ghost added this to the Next milestone Jul 25, 2021
@cston
Copy link
Member

cston commented Jul 25, 2021

Thanks @captainsafia for fixing this.

@allisonchou allisonchou modified the milestones: Next, 17.0.P3 Jul 27, 2021
@JoeRobich JoeRobich deleted the safia/fix-editor-config-special-chars branch May 9, 2022 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants