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

Consider loosening byte[] rules in resx resources #10551

Closed
rainersigwald opened this issue Aug 22, 2024 · 2 comments
Closed

Consider loosening byte[] rules in resx resources #10551

rainersigwald opened this issue Aug 22, 2024 · 2 comments
Labels
.NET Core Task: GenerateResource Problems with the task itself, resgen.exe, and resx resources in general.

Comments

@rainersigwald
Copy link
Member

originally by @ericstj at dotnet/project-system#9526 (comment)

it's being treated as a Activator type resource.

Here's the relevant code in MSBuild:

private static void AddLinkedResource(string resxFilename, bool pathsRelativeToBasePath, List<IResource> resources, string name, string value)
{
string[] fileRefInfo = ParseResxFileRefString(value);
string fileName = FileUtilities.FixFilePath(fileRefInfo[0]);
string fileRefType = fileRefInfo[1];
if (pathsRelativeToBasePath)
{
fileName = Path.Combine(
FileUtilities.GetDirectory(
FileUtilities.NormalizePath(resxFilename)),
fileName);
}
if (IsString(fileRefType))
{
string fileRefEncoding = null;
if (fileRefInfo.Length == 3)
{
fileRefEncoding = fileRefInfo[2];
#if RUNTIME_TYPE_NETCORE
// Ensure that all Windows codepages are available.
// Safe to call multiple times per https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding.registerprovider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
}
// from https://github.com/dotnet/winforms/blob/a88c1a73fd7298b0a5c45251771f439262016826/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L231-L241
Encoding textFileEncoding = fileRefEncoding != null
? Encoding.GetEncoding(fileRefEncoding)
: Encoding.Default;
using (StreamReader sr = new StreamReader(fileName, textFileEncoding))
{
resources.Add(new StringResource(name, sr.ReadToEnd(), resxFilename));
return;
}
}
else if (IsByteArray(fileRefType))
{
byte[] byteArray = File.ReadAllBytes(fileName);
resources.Add(new LiveObjectResource(name, byteArray));
return;
}
else if (IsMemoryStream(fileRefType))
{
// See special-case handling in ResXFileRef
// https://github.com/dotnet/winforms/blob/689cd9c69e632997bc85bf421af221d79b12ddd4/src/System.Windows.Forms/src/System/Resources/ResXFileRef.cs#L293-L297
byte[] byteArray = File.ReadAllBytes(fileName);
resources.Add(new LiveObjectResource(name, new MemoryStream(byteArray)));
return;
}
resources.Add(new FileStreamResource(name, fileRefType, fileName, resxFilename));
}

MSbuild is registering as activator resource: https://github.com/dotnet/msbuild/blob/1a51dd82a24e26b6aac68e29414182fa56fbb573/src/Tasks/ResourceHandling/MSBuildResXReader.cs#L279C31-L279C49

preserializedResourceWriter.AddActivatorResource(Name, fileStream, TypeAssemblyQualifiedName, closeAfterWrite: true);

That's wrong, it should be treating as byte[], but MSBuild is only treating this as a byte array if it has both System.Byte[] and mscorlib

return fileRefType.IndexOf("System.Byte[]") != -1 && fileRefType.IndexOf("mscorlib") != -1;

Here's what I see in the latest resx:

  <data name="ProgramSource" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>Program.cs;System.Byte[]</value>
  </data>

Repro: conRes.zip

So I can workaround this by adding ,mscorlib to the resx.

  <data name="ProgramSource" type="System.Resources.ResXFileRef, System.Windows.Forms">
-    <value>Program.cs;System.Byte[]</value>
+    <value>Program.cs;System.Byte[],mscorlib</value>
  </data>

Maybe MSBuild could broaden that check to consider this specific case of no core assembly name. @rainersigwald

@rainersigwald rainersigwald added .NET Core Task: GenerateResource Problems with the task itself, resgen.exe, and resx resources in general. labels Aug 22, 2024
@rainersigwald
Copy link
Member Author

@melytc it looks like you changed the resource editor to put ,mscorlib in there again; do you think we should change MSBuild as @ericstj suggested?

@melytc
Copy link

melytc commented Aug 22, 2024

What @ericstj described, where we weren't saving the assembly information for the resource correctly, was a bug on our side. I don't think you need to change MSBuild, as our goal is to save the resource as we were doing before with the legacy resource editor. :)

@rainersigwald rainersigwald closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Core Task: GenerateResource Problems with the task itself, resgen.exe, and resx resources in general.
Projects
None yet
Development

No branches or pull requests

2 participants