-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Preserve file encoding in file-based app source files #52055
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
base: main
Are you sure you want to change the base?
Changes from all commits
92f12aa
9b7a4f7
13dd3b6
c061b9e
643569f
07074b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,6 +517,94 @@ public void RemoveMultiple() | |
| """)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that files without UTF-8 BOM don't get one added when saved. | ||
| /// This is critical for shebang (#!) scripts on Unix-like systems. | ||
| /// <see href="https://github.com/dotnet/sdk/issues/52054"/> | ||
| /// </summary> | ||
| [Fact] | ||
| public void PreservesNoBomEncoding() | ||
| { | ||
| var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
| var tempFile = Path.Join(testInstance.Path, "test.cs"); | ||
|
|
||
| // Create a file without BOM | ||
| var content = "#!/usr/bin/env dotnet run\nConsole.WriteLine();"; | ||
| File.WriteAllText(tempFile, content, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); | ||
|
|
||
| // Load, modify, and save | ||
| var sourceFile = SourceFile.Load(tempFile); | ||
| var editor = FileBasedAppSourceEditor.Load(sourceFile); | ||
| editor.Add(new CSharpDirective.Package(default) { Name = "MyPackage", Version = "1.0.0" }); | ||
| editor.SourceFile.Save(); | ||
|
|
||
| // Verify no BOM was added | ||
| var bytes = File.ReadAllBytes(tempFile); | ||
| Assert.True(bytes is not [0xEF, 0xBB, 0xBF, ..], | ||
| "File should not have UTF-8 BOM"); | ||
|
|
||
| // Verify shebang is still first | ||
| var savedContent = File.ReadAllText(tempFile); | ||
| Assert.StartsWith("#!/usr/bin/env dotnet run", savedContent); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that files with UTF-8 BOM preserve it when saved. | ||
| /// <see href="https://github.com/dotnet/sdk/issues/52054"/> | ||
| /// </summary> | ||
| [Fact] | ||
| public void PreservesBomEncoding() | ||
| { | ||
| var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
| var tempFile = Path.Join(testInstance.Path, "test.cs"); | ||
|
|
||
| // Create a file with BOM | ||
| var content = "Console.WriteLine();"; | ||
| File.WriteAllText(tempFile, content, new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)); | ||
|
|
||
| // Load, modify, and save | ||
| var sourceFile = SourceFile.Load(tempFile); | ||
| var editor = FileBasedAppSourceEditor.Load(sourceFile); | ||
| editor.Add(new CSharpDirective.Package(default) { Name = "MyPackage", Version = "1.0.0" }); | ||
| editor.SourceFile.Save(); | ||
|
|
||
| // Verify BOM is still present | ||
| var bytes = File.ReadAllBytes(tempFile); | ||
| Assert.True(bytes is [0xEF, 0xBB, 0xBF, ..], | ||
| "File should have UTF-8 BOM"); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to verify the behavior when original file uses some other encoding besides UTF-8.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 643569f. The new |
||
|
|
||
| /// <summary> | ||
| /// Verifies that files with non-UTF-8 encodings (like UTF-16) preserve their encoding when saved. | ||
| /// <see href="https://github.com/dotnet/sdk/issues/52054"/> | ||
| /// </summary> | ||
| [Fact] | ||
| public void PreservesNonUtf8Encoding() | ||
| { | ||
| var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
| var tempFile = Path.Join(testInstance.Path, "test.cs"); | ||
|
|
||
| // Create a file with UTF-16 encoding (includes BOM by default) | ||
| var content = "Console.WriteLine(\"UTF-16 test\");"; | ||
| File.WriteAllText(tempFile, content, Encoding.Unicode); | ||
|
|
||
| // Load, modify, and save | ||
| var sourceFile = SourceFile.Load(tempFile); | ||
| var editor = FileBasedAppSourceEditor.Load(sourceFile); | ||
| editor.Add(new CSharpDirective.Package(default) { Name = "MyPackage", Version = "1.0.0" }); | ||
| editor.SourceFile.Save(); | ||
|
|
||
| // Verify UTF-16 BOM is still present (0xFF 0xFE for UTF-16 LE) | ||
| var bytes = File.ReadAllBytes(tempFile); | ||
| Assert.True(bytes is [0xFF, 0xFE, ..], | ||
| "File should have UTF-16 LE BOM"); | ||
|
|
||
| // Verify content is still readable as UTF-16 | ||
| var savedContent = File.ReadAllText(tempFile, Encoding.Unicode); | ||
| Assert.Contains("#:package MyPackage@1.0.0", savedContent); | ||
| Assert.Contains("Console.WriteLine", savedContent); | ||
| } | ||
|
|
||
| private void Verify( | ||
| string input, | ||
| params ReadOnlySpan<(Action<FileBasedAppSourceEditor> action, string expectedOutput)> verify) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.