Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.DotNet.MSIdentity.AuthenticationParameters;
using Microsoft.DotNet.MSIdentity.Project;
using Microsoft.DotNet.MSIdentity.Properties;
Expand Down Expand Up @@ -47,7 +48,7 @@ internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement
{
File.Copy(filePath, filePath + "%");
}
File.WriteAllText(filePath, fileContent);
File.WriteAllText(filePath, fileContent, new UTF8Encoding(false));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ private void AddFile(CodeFile file)
if (!string.IsNullOrEmpty(fileDir))
{
Directory.CreateDirectory(fileDir);
File.WriteAllText(filePath, codeFileString);
File.WriteAllText(filePath, codeFileString, new UTF8Encoding(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.DotNet.MSIdentity.AuthenticationParameters;
using Microsoft.DotNet.MSIdentity.Tool;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void ModifyAppSettings(ApplicationParameters applicationParameters, IEnum
// TODO: save comments somehow, only write to appsettings.json if changes are made
if (modifiedAppSettings != null)
{
System.IO.File.WriteAllText(_provisioningToolOptions.AppSettingsFilePath, modifiedAppSettings.ToString());
System.IO.File.WriteAllText(_provisioningToolOptions.AppSettingsFilePath, modifiedAppSettings.ToString(), new UTF8Encoding(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private ProjectAuthenticationSettings ConvertAadApplicationToB2CApplication(Proj
+ updatedContent.Substring(indexCallbackPath);
}
}
System.IO.File.WriteAllText(filePath, updatedContent);
System.IO.File.WriteAllText(filePath, updatedContent, new UTF8Encoding(false));
}

if (projectSettings.ApplicationParameters.CallsMicrosoftGraph)
Expand Down
3 changes: 2 additions & 1 deletion src/Scaffolding/VS.Web.CG.Mvc/Dependency/ReadMeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DotNet.Scaffolding.Shared.Project;
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void GenerateReadMeWithContent(string content)
Constants.ReadMeOutputFileName);
try
{
File.WriteAllText(outputPath, content);
File.WriteAllText(outputPath, content, new UTF8Encoding(false));
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public async Task WriteToClassFileAsync(string filePath)
{
var changedDocument = GetDocument();
var classFileTxt = await changedDocument.GetTextAsync();
File.WriteAllText(filePath, classFileTxt.ToString());
File.WriteAllText(filePath, classFileTxt.ToString(), new UTF8Encoding(false));
}

internal static BaseMethodDeclarationSyntax AddMethodParameters(BaseMethodDeclarationSyntax originalMethod, Method methodChanges, CodeChangeOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.DotNet.Scaffolding.Shared
Expand Down Expand Up @@ -72,7 +73,7 @@ public void RemoveDirectory(string path, bool recursive)

public void WriteAllText(string path, string contents)
{
File.WriteAllText(path, contents);
File.WriteAllText(path, contents, new UTF8Encoding(false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text;

namespace Microsoft.Extensions.Internal
{
Expand Down Expand Up @@ -100,7 +101,7 @@ private static void EnsureBindingRedirects(string assemblyFullPath, string toolN
{
var text = File.ReadAllText(bindingRedirectFile);
var toolBindingRedirectFile = Path.Combine(Path.GetDirectoryName(assemblyFullPath), $"{toolName}.config");
File.WriteAllText(toolBindingRedirectFile, text);
File.WriteAllText(toolBindingRedirectFile, text, new UTF8Encoding(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ internal static void ApplyReplacementsOnFileOnDisk(string filePath, IEnumerable<

if (sourceChanged)
{
File.WriteAllText(filePath, sourceFileString);
File.WriteAllText(filePath, sourceFileString, new UTF8Encoding(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace Microsoft.DotNet.Scaffolding.Internal.Services;

Expand Down Expand Up @@ -41,7 +42,7 @@ public string ReadAllText(string filePath)
/// <inheritdoc />
public void WriteAllText(string filePath, string content)
{
File.WriteAllText(filePath, content);
File.WriteAllText(filePath, content, new UTF8Encoding(false));
}

/// <inheritdoc />
Expand All @@ -53,7 +54,7 @@ public string[] ReadAllLines(string filePath)
/// <inheritdoc />
public void WriteAllLines(string filePath, string[] content)
{
File.WriteAllLines(filePath, content);
File.WriteAllLines(filePath, content, new UTF8Encoding(false));
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
using Microsoft.DotNet.Scaffolding.Core.Scaffolders;
using Microsoft.DotNet.Scaffolding.Core.Steps;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -92,7 +93,7 @@ public override Task<bool> ExecuteAsync(ScaffolderContext context, CancellationT
// If Overwrite is true, write file, or if it doesn't exist
if (Overwrite || !File.Exists(templatingProperty.OutputPath))
{
File.WriteAllText(templatingProperty.OutputPath, templatedString);
File.WriteAllText(templatingProperty.OutputPath, templatedString, new UTF8Encoding(false));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.IO;
using System.Text;
using Microsoft.DotNet.Scaffolding.Shared;
using Xunit;

namespace Microsoft.DotNet.Scaffolding.Shared.Tests
{
public class DefaultFileSystemEncodingTests
{
[Fact]
public void WriteAllText_UsesUtf8EncodingWithoutBom()
{
// Arrange
var fileSystem = new DefaultFileSystem();
var tempFile = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.txt");
var contentWithNonAscii = "Hello мир 世界 العالم"; // Russian, Chinese, Arabic

try
{
// Act
fileSystem.WriteAllText(tempFile, contentWithNonAscii);

// Assert
var bytes = File.ReadAllBytes(tempFile);

// Check that file does NOT start with UTF-8 BOM (EF BB BF)
Assert.False(bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF,
"File should not contain UTF-8 BOM");

// Check that content can be read correctly as UTF-8
var readContent = File.ReadAllText(tempFile, Encoding.UTF8);
Assert.Equal(contentWithNonAscii, readContent);

// Verify encoding by reading with UTF8 encoding explicitly
using (var reader = new StreamReader(tempFile, new UTF8Encoding(false)))
{
var content = reader.ReadToEnd();
Assert.Equal(contentWithNonAscii, content);
}
}
finally
{
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}

[Fact]
public void WriteAllText_PreservesNonAsciiCharacters()
{
// Arrange
var fileSystem = new DefaultFileSystem();
var tempFile = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.txt");
var testCases = new[]
{
"Русский текст", // Russian
"中文文本", // Chinese
"النص العربي", // Arabic
"Ελληνικό κείμενο", // Greek
"日本語のテキスト", // Japanese
"한국어 텍스트", // Korean
"Émojis: 😀🎉🌟" // Emojis
};

try
{
foreach (var testContent in testCases)
{
// Act
fileSystem.WriteAllText(tempFile, testContent);

// Assert
var readContent = File.ReadAllText(tempFile, Encoding.UTF8);
Assert.Equal(testContent, readContent);
}
}
finally
{
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}

[Fact]
public void WriteAllText_DoesNotUseSystemDefaultEncoding()
{
// Arrange
var fileSystem = new DefaultFileSystem();
var tempFile = Path.Combine(Path.GetTempPath(), $"test_{Guid.NewGuid()}.txt");
var russianText = "Привет мир";

try
{
// Act
fileSystem.WriteAllText(tempFile, russianText);

// Assert - Try reading with default encoding (which might be different on different systems)
// If the file was written with UTF-8, it should read correctly
var bytes = File.ReadAllBytes(tempFile);
var utf8Content = Encoding.UTF8.GetString(bytes);
Assert.Equal(russianText, utf8Content);

// Verify it's not using some other encoding like Windows-1251
// If it were Windows-1251, these bytes would be different
var utf8Bytes = new UTF8Encoding(false).GetBytes(russianText);
Assert.Equal(utf8Bytes, bytes);
}
finally
{
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,105 @@ public void AddLeadingTriviaSpacesTests()
Assert.True(formattedCodeSnippets[3].LeadingTrivia.NumberOfSpaces == 4 + whitespaceBeingAdded);
}

[Fact]
public async Task WriteToClassFileAsync_UsesUtf8EncodingWithoutBom()
{
// Arrange
var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"test_{Guid.NewGuid()}.cs");
var documentWithRussianComments = @"using System;

namespace TestNamespace
{
// Класс для тестирования
public class TestClass
{
// Метод с русскими комментариями
public void TestMethod()
{
var message = ""Привет, мир!"";
}
}
}";

try
{
DocumentEditor editor = await DocumentEditor.CreateAsync(CreateDocument(documentWithRussianComments));
CodeFile codeFile = new CodeFile();
DocumentBuilder docBuilder = new DocumentBuilder(editor, codeFile, new MSIdentity.Shared.ConsoleLogger());

// Act
await docBuilder.WriteToClassFileAsync(tempFile);

// Assert
var bytes = System.IO.File.ReadAllBytes(tempFile);

// Check that file does NOT start with UTF-8 BOM (EF BB BF)
Assert.False(bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF,
"File should not contain UTF-8 BOM");

// Check that content can be read correctly as UTF-8
var readContent = System.IO.File.ReadAllText(tempFile, System.Text.Encoding.UTF8);
Assert.Contains("Привет, мир!", readContent);
Assert.Contains("Класс для тестирования", readContent);
}
finally
{
// Cleanup
if (System.IO.File.Exists(tempFile))
{
System.IO.File.Delete(tempFile);
}
}
}

[Fact]
public async Task WriteToClassFileAsync_PreservesNonAsciiCharacters()
{
// Arrange
var tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), $"test_{Guid.NewGuid()}.cs");
var documentWithMultilingualComments = @"using System;

namespace TestNamespace
{
// English, Русский, 中文, العربية
public class MultilingualClass
{
public void TestMethod()
{
var message = ""Hello мир 世界 🌍"";
}
}
}";

try
{
DocumentEditor editor = await DocumentEditor.CreateAsync(CreateDocument(documentWithMultilingualComments));
CodeFile codeFile = new CodeFile();
DocumentBuilder docBuilder = new DocumentBuilder(editor, codeFile, new MSIdentity.Shared.ConsoleLogger());

// Act
await docBuilder.WriteToClassFileAsync(tempFile);

// Assert
var readContent = System.IO.File.ReadAllText(tempFile, System.Text.Encoding.UTF8);
Assert.Contains("Hello мир 世界 🌍", readContent);
Assert.Contains("English, Русский, 中文, العربية", readContent);

// Verify UTF-8 encoding
var bytes = System.IO.File.ReadAllBytes(tempFile);
var utf8Content = System.Text.Encoding.UTF8.GetString(bytes);
Assert.Contains("мир", utf8Content);
Assert.Contains("世界", utf8Content);
}
finally
{
// Cleanup
if (System.IO.File.Exists(tempFile))
{
System.IO.File.Delete(tempFile);
}
}
}

}
}
Loading