kswoll / someta

General purpose meta programming with Fody
11 stars 3 forks source link

Fody Can't Find Framework #19

Open hughesjs opened 2 years ago

hughesjs commented 2 years ago

The details of this issue are here. However, I've replicated the relevant section below for posterity.


So far, I've got this attribute:

[AttributeUsage(AttributeTargets.Method)]
public class ValidatedMethodAttribute: Attribute, IMethodInterceptor
{
    public object Invoke(MethodInfo methodInfo, object instance, Type[] typeArguments, object[] arguments, Func<object[], object> invoker)
    {
        Attribute[]? attributes = methodInfo.GetCustomAttributes().Where(att => att.GetType().IsAssignableTo(typeof(IParameterValidator<>))).ToArray();
        // Do my thing
        return invoker.Invoke(arguments);
    }
}

However, when I execute the build, something weird is going on with Fody but I can't quite work out what this issue is..?

  Fody: An unhandled exception occurred:
Exception:
Failed to execute weaver /home/james/.nuget/packages/someta.fody/1.2.1/build/../weaver/Someta.Fody.dll
Type:
System.Exception
StackTrace:
   at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 222
   at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 112
Source:
FodyIsolated
TargetSite:
Void ExecuteWeavers()
System.Type was somehow not found.  Aborting.
Type:
System.InvalidOperationException
StackTrace:
   at Someta.Fody.CecilExtensions.Initialize(ModuleDefinition moduleDefinition, TypeSystem typeSystem, AssemblyNameReference soMeta)
   at Someta.Fody.ModuleWeaver.Execute()
   at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186
Source:
Someta.Fody
TargetSite:
Boolean Initialize(Mono.Cecil.ModuleDefinition, Fody.TypeSystem, Mono.Cecil.AssemblyNameReference)

As requested on our SO chat, here's the project I'm having issues with:

https://github.com/hughesjs/warm-up-exercises/tree/user-validator/05-user-validator/cs

hughesjs commented 2 years ago

I've tried adding a zip to this but for some reason Github won't let me upload it, if it's vital you get it as a zip let me know and I'll upload it somewhere you can grab it!

kswoll commented 2 years ago

Thanks, the link to your GitHub repo is perfect. Starting to look at it now.

kswoll commented 2 years ago

So after checking out your repo I was able to successfully build your solution. (I checked out the user-validator branch, of course)

I then created an empty implementation of IUserRepository and added the following lines to your UnitTest1:

[Fact]
public void Test1()
{
    var userService = new UserService(new UserRepository());
    userService.CreateUser(null, null, null);
}

I then debugged the test after putting a breakpoint inside the Invoke method in ValidatedMethodAttribute and the breakpoint was successfully hit. So it all seems to be working for me.

Do you think you could try a clean checkout (into a new folder) like I just did? I wonder if you have some cruft hanging around your obj folder that is somehow getting in the way of a successful build.

hughesjs commented 2 years ago

Huh, strange, I'll give it a try after my dinner!

Out of interest, are you building on Windows/Linux/MacOS? The C:/ drive showing up in the error message is making me wonder if this is some platform dependency issue

hughesjs commented 2 years ago

Yup, I get the same issue on a clean build... Could you try building it in either WSL or a linux docker container? That would pretty much confirm the issue. I've not got anything running Windows to test on I'm afraid

kswoll commented 2 years ago

Oh, interesting. I definitely haven't tried it in that scenario. And yeah, I'll give that a whirl, though might not be able to get back to you until tomorrow. Thanks for working with me on this, as I'd like it to work for everyone. :)

hughesjs commented 2 years ago

No worries mate, let me know if there's anything I can do to help!

hughesjs commented 2 years ago

Hey my dude, wondering if you've had a chance to look at this or if there's anything I can do to help? Of course, no worries if not, I know how busy life gets!

kswoll commented 2 years ago

Hey, sorry, the 4th of July festivities took up way more of my time than I was anticipating. :) But I'm planning to look at it tomorrow. Hopefully it's reproducible on a Mac, as Linux is a bit tricker for me to test on, though I can if it comes to it.

hughesjs commented 2 years ago

Ahh yes of course... I forgot the colonies tend to get quite rowdy about this time of year! Nah, I hope you had a good break mate!

As for testing on Linux (if Mac doesn't reproduce the issue), WSL2 is very easy to set up and should hopefully make testing it easy for you! Happy to help with that if your'e not familiar!

image

kswoll commented 2 years ago

Love it! And thanks! I have WSL installed, so can definitely go that route. Made some headway today just getting set up, but hopefully more time tomorrow.

hughesjs commented 2 years ago

Thanks for all of your help on this one mate, there's no rush, this isn't for production code or anything, just a useful tool to have in my belt

mmintoff commented 2 years ago

Hey, I submitted a PR relevant to this issue :) You can reproduce the "System.Type was somehow not found. Aborting." issue by using "dotnet build" from Powershell.

hughesjs commented 2 years ago

@kswoll - Any chance you've got the time to take a look at #20 ?