kentcb / PCLMock

A simple mocking framework in a PCL.
MIT License
45 stars 8 forks source link

Cannot find System.Coposition.TypedParts #9

Open bezysoftware opened 9 years ago

bezysoftware commented 9 years ago

I just tried to use the T4 generator, but when I try to run the custom tool, I get following error:

Error   16  Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: One or more errors occurred. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Composition.TypedParts, Version=1.0.27.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   at Microsoft.CodeAnalysis.Host.Mef.MefHostServices.Create(IEnumerable`1 assemblies)
   at Microsoft.CodeAnalysis.Host.Mef.DesktopMefHostServices.get_DefaultServices()
   at Kent.Boogaart.PCLMock.CodeGeneration.Generator.<GenerateMocksAsync>d__0.MoveNext() in C:\Users\kent\Repository\PCLMock\Src\Kent.Boogaart.PCLMock.CodeGeneration\Generator.cs:line 24
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Kent.Boogaart.PCLMock.CodeGeneration.XmlBasedGenerator.<GenerateMocksAsync>d__2.MoveNext() in C:\Users\kent\Repository\PCLMock\Src\Kent.Boogaart.PCLMock.CodeGeneration\XmlBasedGenerator.cs:line 48
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at Kent.Boogaart.PCLMock.CodeGeneration.XmlBasedGenerator.GenerateMocks(String solutionPath, String xmlPath, Language language) in C:\Users\kent\Repository\PCLMock\Src\Kent.Boogaart.PCLMock.CodeGeneration\XmlBasedGenerator.cs:line 29
   at Kent.Boogaart.PCLMock.CodeGeneration.XmlBasedGenerator.GenerateMocks(String solutionPath, String xmlPath, String language) in C:\Users\kent\Repository\PCLMock\Src\Kent.Boogaart.PCLMock.CodeGeneration\XmlBasedGenerator.cs:line 21
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.VisualStudio.TextTemplating59389E350AC879AC2313AB3BF88249987D0426CB576DCFE492173DC732D3844164099E1D5EA5CD146FE3DD173ED128E47A71AAFFE3474AF69A3196EB6CD0079A.GeneratedTextTransformation.TransformText() in c:\Src\Strat\ConsoleApplication5\UnitTestProject1\Mocks.tt:line 19   c:\Src\Strat\ConsoleApplication5\UnitTestProject1\Mocks.tt  19  1   UnitTestProject1

It's .net 4.5 project

kentcb commented 9 years ago

V3 was released in error - I'd have removed it if I could.

Basically, I thought my patches for Roslyn were applied, but they will not be available until Roslyn stable ships (which should be soonish). That said, it shouldn't lead to the error you're seeing. Anyway, once Roslyn v1 ships I'll release PCLMock v3.1 and will ping you here to retry with those binaries.

bezysoftware commented 9 years ago

Alright, I'll wait for Roslyn, thanks for the info

kentcb commented 9 years ago

Hi,

v3.1 has been released and I've successfully integrated the code generation (T4 in my case, but I also tried via the console) in my own large project. Could you please try again and let me know how you get on?

Thanks

bezysoftware commented 9 years ago

Hi, just tried v3.1 + T4 on a clean new project and still fails :(, VS2013 Premium, Windows 7, .NET 4.5 project.

I will try on Windows 10 & VS2015 when I get home.

kentcb commented 9 years ago

thanks @bezysoftware I'd appreciate you trying that. I assume it's the same error you're getting?

BTW, my setup is Win8.1 VS2015 RTM, .NET 4.5

bezysoftware commented 9 years ago

I just tried multiple things (all on Windows 10 RTM) VS 2015 Universal Project: didn't even create Mocks.tt and Mocks.xml, after I created them manually I get "Could not find packages directory relative to the template's directory". It seems the Universal Project uses completely different structure for packages, there is even no packages.config VS 2015 .NET 4.6: WORKS

VS 2013 Windows 8.1 Project: Missing TypedParts (as in first post) VS 2013 .NET 4.6: Missing TypedParts (as in first post)

kentcb commented 9 years ago

@bezysoftware thanks for that. Weird - I'll have a look when I get some time (which might not be for a while).

bezysoftware commented 9 years ago

Seems there are some big changes with latest nuget for uwp... http://blog.nuget.org/20150729/Introducing-nuget-uwp.html

kentcb commented 9 years ago

Thanks for the link. NuGet 3 sounds like a WIP. Creating the content files manually (Mocks.tt Mocks.xml) is your only choice right now until they add support back in for content files. As for the other issue, I'll try to take a look at some point, but if it's a big issue I might have to postpone a resolution until NuGet 3 is bedded down more.

elwray commented 9 years ago

Have the same error (Cannot find System.Coposition.TypedParts). Windows 8 Pro / VisualStudio 2013 / Portable project with targeting Windows 8.1 and Windows Phone 8.1. When I use PCLMockCodeGen tool all works fine.

technicallyerik commented 8 years ago

I'll try to submit a PR at some point, but using the T4 generator on UWP fails (even after manually copying over 'Mocks.tt' and 'Mocks.xml' because NuGet 3 packages are stored globally in your user profile, instead of "packages" project in your solution. This worked for me:

private void HookAssemblyResolution()
{
    // Get Packages Directory
    string packagesDirectory = Environment.ExpandEnvironmentVariables("%userprofile%\\.nuget\\packages");
    if (packagesDirectory == null)
    {
        throw new InvalidOperationException("Could not find packages directory in your user profile.");
    }
    this.Debug("packages directory: {0}", packagesDirectory);

    // Get T4 Code Generation Directory
    var packageDirectories = Directory.GetDirectories(packagesDirectory, "PCLMock.CodeGeneration.T4.*");
    if (packageDirectories.Length == 0)
    {
        throw new InvalidOperationException("Could not find PCLMock.CodeGeneration.T4 package in packages directory (" + packagesDirectory + ").");
    }
    var packageDirectory = packageDirectories.First();
    this.Debug("package directory: {0}", packageDirectory);

    // Get first version subfolder
    var packageVersionDirectories = Directory.GetDirectories(packageDirectory);
    if (packageDirectories.Length == 0)
    {
        throw new InvalidOperationException("Could not find any versions of PCLMock.CodeGeneration.T4 package in package directory (" + packageDirectory + ").");
    }
    var packageVersionDirectory = packageVersionDirectories.First();    // TODO:  Could do this smarter?

    // Get tools directory
    var assembliesDirectory = Path.Combine(packageVersionDirectory, "tools");
    this.Debug("assemblies directory: {0}", assembliesDirectory);
    if (!Directory.Exists(assembliesDirectory))
    {
        throw new InvalidOperationException("Package directory " + packageVersionDirectory + " was found but no 'tools' subdirectory was found within.");
    }

    AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
    {
        this.Debug("resolve: {0}", e.Name);

        var matches = Directory.GetFiles(assembliesDirectory, e.Name + ".dll", SearchOption.TopDirectoryOnly);

        if (matches.Length == 0)
        {
            this.Debug("Failed to find assembly in assemblies directory ({0}) named '{1}'.", assembliesDirectory, e.Name);
            return null;
        }
        else if (matches.Length > 1)
        {
            this.Debug("Found more than one potential match for '{0}' in assemblies directory ({0}). Bailing.", e.Name, assembliesDirectory);
            return null;
        }

        var match = matches.First();
        this.Debug("match: {0}", match);

        return Assembly.LoadFrom(match);
    };
}