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

Non-Resx Embedded Resources get mangled names, possible name collisions #4740

Closed
Anipik opened this issue Sep 17, 2019 · 21 comments · Fixed by dotnet/runtime#36140
Closed

Non-Resx Embedded Resources get mangled names, possible name collisions #4740

Anipik opened this issue Sep 17, 2019 · 21 comments · Fixed by dotnet/runtime#36140
Assignees
Labels
Milestone

Comments

@Anipik
Copy link

Anipik commented Sep 17, 2019

I was able to create a small repro

the csproj looks like this

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <EmbeddedResource Include="MyResource.bmp" WithCulture="false" Type="Non-Resx" />
  </ItemGroup>
</Project>

Program.cs

namespace resourceName
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MyResource.Get()?.Length ?? -1);
        }
    }
}

MyResource.cs

namespace resourceName
{
    public static class MyResource
    {
        public static Stream Get() => typeof(MyResource).Assembly.GetManifestResourceStream($"{nameof(resourceName)}.{nameof(MyResource)}.bmp");
    }
}

Running this code under preview 7 gives output as

C:\git\scratch\resourceName>dotnet.exe run
485514

where as running this code under preview 9 gives output as

C:\git\corefx>dotnet c:\git\scratch\resourceName\bin\Debug\netcoreapp3.0\resourceName.dll
-1

I checked dlls
the resource name include in .dll in previe9 is resourceName.MyResource where as under preview 7 is resourceName.MyResource.bmp

I hit this error while building corefx with preview9 for testData.resources file.

This works fine with 5.0 as well.

cc @ericstj @danmosemsft @livarcocc

@ericstj
Copy link
Member

ericstj commented Sep 17, 2019

@Anipik can you zip up the project and add it to the issue? Does setting EmbeddedResourceUseDependentUponConvention = false also fix it?

@Anipik
Copy link
Author

Anipik commented Sep 17, 2019

yes setting EmbeddedResourceUseDependentUponConvention to false fixes it for preview9
MyResource.zip

@ericstj
Copy link
Member

ericstj commented Sep 17, 2019

Looks like this was regressed with #4597. @benvillalobos @rainersigwald

I wonder if the DependentUpon heuristic should be scoped a little?

@nguerrera
Copy link
Contributor

We probably should have only applied the convention to .resx resources.

@nguerrera
Copy link
Contributor

If we want to change this, we should do it ideally quickly as it would be breaking to change the resource names again. I think we're too late for 3.0 which makes this difficult. :(

@nguerrera
Copy link
Contributor

Restricting to resx makes sense because that is the case that would have been persisted with DependentUpon in a classic project. I'm not aware of anything that would have put DependentUpon for non-resx in your project unless you did it manually so I don't think it should have been part of the convention to do so.

@nguerrera
Copy link
Contributor

A very similar repro could be constructed for a resx though, in some cases the convention will pick a different name than earlier previews (that's the point of the change after all) so it is to be expected that some projects will be broken by the convention, this is why I insisted on scoping the default to TFM so that it would only break 3.0 preview projects. But it is unfortunate that non-resx is affected at all.

@nguerrera
Copy link
Contributor

It turns out that this changes the extension from .bmp or .jpg or whatever else non-resx to blank. So even if the source file found by convention has first class RootNamespace.Folder.Filename, the manifest resource will not match what you would get without the inferred DependentUpon

@nguerrera
Copy link
Contributor

I imagine there will also be build errors if you have say Foo.txt and Foo.bmp as embedded resources next to Foo.cs. Without preserving the extension, this will cause duplicate resource names.

@rainersigwald
Copy link
Member

Looks like there's another related failure mode here:

error CS1508: Resource identifier 'C' has already been used in this assembly"

From https://dev.azure.com/dnceng/public/_build/results?buildId=361408&view=results (thanks, @ViktorHofer).

We'll have to dig in, but I suspect this is the thing Nick predicted

I imagine there will also be build errors if you have say Foo.txt and Foo.bmp as embedded resources next to Foo.cs. Without preserving the extension, this will cause duplicate resource names.

@ericstj
Copy link
Member

ericstj commented Sep 20, 2019

@nguerrera
Copy link
Contributor

nguerrera commented Sep 20, 2019

Alright, I think we need to remove the convention from affecting non-resx resources. It's becoming pretty clear to me that the odds of wanting the convention to apply (even if it preserved extension) are close to zero. I suspect eveyone in this case is going to disable the convention.

This is strictly a breaking change from 3.0 /16.3 GA (assuming we cannot get this fix into that, and I don't think we can), but I will bet that most of the people who hit this will disable the convention entirely and therefore not get re-broken when it gets less aggressive.

I would consider taking this in 16.0.x/3.0.1xx servicing as quickly as feasible so that the window of time to take a dependency on this odd, non-deliberate fallout from the change is as short as possible.

@livarcocc @ericstj @rainersigwald Do you agree?

@rainersigwald
Copy link
Member

restrict convention to .resx: yes

service for it: probably

@nguerrera
Copy link
Contributor

Maybe 16.4/3.1 being not too far out is good enough. If we think most people with this code pattern will have to disable the convention, then it's not really breaking, and will help others who only move to 3.x when it goes LTS.

@nguerrera
Copy link
Contributor

Just a note that there is another hit count on this from a customer in a mail thread.

@nguerrera
Copy link
Contributor

HitCount++

https://twitter.com/simoncropp/status/1175385910919090176?s=21

This one is the other duplicate name case I predicted.

@rainersigwald rainersigwald changed the title Embedded Resource Getting dropped Non-Resx Embedded Resources get mangled names, possible name collisions Sep 23, 2019
@panost
Copy link

panost commented Sep 24, 2019

Got hit by this in all my projects, after upgrading to VS 16.3
The error was CS1508: The resource identifier ... has already been used in this assembly
I have resources that generated on build and somehow msbuild made all the wrong assumptions, on how to name them. I got a lot of lines in CreateCSharpManifestResourceName like this
Resource file 'blah..' gets manifest resource name 'wrong_name_blah'.

Thankfully the EmbeddedResourceUseDependentUponConvention=false workaround works, but it took me 3 missing hours to figure what happen.
Sorry for the long post, I am actually trying to make it google searchable, if anyone has the same problem

@SimonCropp
Copy link
Contributor

@panost i feel your pain. i burnt 2 hours :)

@rainersigwald
Copy link
Member

Sorry for the troubles, folks. This issue is now listed as a .NET Core 3.0 known issue to hopefully improve visibility.

@rainersigwald
Copy link
Member

Fixed for 16.3 servicing (and 16.4) by #4764.

@benvillalobos
Copy link
Member

For folks who stumble onto this closed issue, refer to this dotnet docs issue for a detailed explanation on how manifest resource naming works now.

ViktorHofer added a commit to dotnet/runtime that referenced this issue May 8, 2020
Anipik pushed a commit to dotnet/runtime that referenced this issue May 11, 2020
@AR-May AR-May added the triaged label Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants