Fsharp: Failure to load type providers from Nuget package

Created on 11 Feb 2018  路  17Comments  路  Source: dotnet/fsharp

I have a strange situation where a type provider works perfectly when reference as local binary but fails with strange inconsistent errors when referenced as Nuget package. Up to the point where I can point to exactly same binary located in nuget local cache but it fails as a nuget package
I hit this problem with https://www.nuget.org/packages/FSharp.Data.Npgsql.
Seems @sergey-tihon has this issue with Swagger type provider too.
https://github.com/fsprojects/FSharp.TypeProviders.SDK/issues/184#issuecomment-364625781

This basically renders type providers as completely un-usable due to unpredictability of the issue so it would nice to fix ASAP.
It is possibly related to the latest changes to https://github.com/fsprojects/FSharp.TypeProviders.SDK but I'm not sure because I expect that it would not work as local binary too.
Thanks.

Most helpful comment

@dsyme @sergey-tihon Thank you for help.
@dsyme I can claim that we have solid-working PostgreSQL type provider with advanced features and basic docs. https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/master/README.md

All 17 comments

IDE shows error Incorrect instance type Parameter name: obj after ~1min of working with provided types and methods. Occurs only when TP restored from NuGet.

Compilation is successful, compiled program also runs correctly and ~1st min of Intellisense look good.

Repro steps

  1. Clone https://github.com/sergey-tihon/swagger-issue101
  2. Open onle solution in IDE
  3. Wait for intellisense, type something in editor, checkout intellisense of provided types
  4. More details (if needed) https://github.com/fsprojects/SwaggerProvider/issues/101

Actual behavior

VS 2017
vs_swagger_low

Ionide
sw_vs_code

Related information

Environment 1

  • Windows 10
  • Compiled for .NET Runtime 4.6.1 (old project files)
  • Visual Studio 15.5.6
  • Visual F# Tool 15.4.1.18020604

Environment 2

  • macOS High Sierra 10.13.3
  • Mono JIT compiler version 5.4.1.7 (2017-06/e66d9abbb27 Wed Oct 25 12:10:41 EDT 2017)
  • Latest Ionide

Any step-by-step instructions for both problems would be immensely useful. Also sample projects and exact NuGet package numbers please.

For the second issue we really need a stack trace too

I got more details for my case! On this line so I try to get value of provided field inside provided method.

System.ArgumentException
  HResult=0x80070057
  Message=Incorrect instance type
Parameter name: obj
  Source=FSharp.Core
  StackTrace:
   at Microsoft.FSharp.Quotations.PatternsModule.checkObj(MemberInfo membInfo, FSharpExpr obj)
   at Microsoft.FSharp.Quotations.PatternsModule.mkInstanceFieldGet(FSharpExpr obj, FieldInfo finfo)
   at Microsoft.FSharp.Quotations.FSharpExpr.FieldGet(FSharpExpr obj, FieldInfo fieldInfo)
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](Tuple`2 tupledArg) in X:\SwaggerProvider\src\SwaggerProvider.DesignTime\DefinitionCompiler.fs:line 232
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](FSharpList`1 args) in X:\SwaggerProvider\src\SwaggerProvider.DesignTime\DefinitionCompiler.fs:line 231
   at ProviderImplementation.ProvidedTypes.ProvidedMethod.GetInvokeCode(FSharpList`1 args) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 993
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](FSharpList`1 arg00) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 8883
   at ProviderImplementation.ProvidedTypes.QuotationSimplifier.TranslateQuotationToCode(FSharpFunc`2 qexprf, String[] paramNames, FSharpExpr[] argExprs) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 8361
   at ProviderImplementation.ProvidedTypes.ProvidedTypesContext.convCodeToTgt[a](FSharpFunc`2 codeFun, Boolean isStatic, ProvidedParameter[] parameters, Boolean isGenerated, FSharpList`1 argsT) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 8761
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](FSharpList`1 argsT) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 8883
   at ProviderImplementation.ProvidedTypes.ProvidedMethod.GetInvokeCode(FSharpList`1 args) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 993
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](ILTypeBuilder tb, FSharpOption`1 ptdT) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 14119
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](ProvidedTypeDefinition ptd) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 13865
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](Type ntd) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 13871
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](ProvidedTypeDefinition ptd) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 13866
   at ProviderImplementation.ProvidedTypes.AssemblyCompiler.iterateTypes[a](FSharpFunc`2 f, IEnumerable`1 providedTypeDefinitions) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 13877
   at <StartupCode$SwaggerProvider-DesignTime>[email protected](FSharpFunc`2 f, IEnumerable`1 providedTypeDefinitions) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 14023
   at ProviderImplementation.ProvidedTypes.AssemblyCompiler.Compile(Boolean isHostedExecution) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 14023
   at ProviderImplementation.ProvidedTypes.TypeProviderForNamespaces.Microsoft-FSharp-Core-CompilerServices-ITypeProvider-GetGeneratedAssemblyContents(Assembly assembly) in X:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs:line 14406
   at Microsoft.FSharp.Compiler.Tainted`1.Protect[a](FSharpFunc`2 f, range range)

2018-02-12_1914

https://github.com/demetrixbio/FSharp.Data.Npgsql/tree/visualfsharp-issue-4338

Steps:
1) from repo root

FSharp.Data.Npgsql\tests\SampleApp\Program.fs(10,74): error FS0001: This expression was expected to have type    'FSharp.Data.Npgsql.ResultType (FSharp.Data.Npgsql.DesignTime, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)'    but here has type    'FSharp.Data.Npgsql.ResultType (FSharp.Data.Npgsql, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)'

@dsyme Because my problem is around type rewrite for ProvidedStaticParameter I tried to pull it from runtime component. It still fails and here what I found so far

image

Since when TypeProviderConfig.RuntimeAssembly returns design-time assembly?

I guess this is why Swagger type provider fails too - it also has design-time/run-time separation. For combo style type providers it doesn't matter.

@dsyme I also can confirm that TypeProviderConfig.RuntimeAssembly has incorrect value only when a type provider loaded from nuget package. When loaded directly from binary it has right value.

@dmitry-a-morozov Maybe we should explicitly specify in *.nuspec that NuGet should not reference DesignTime file to project after install https://docs.microsoft.com/en-us/nuget/reference/nuspec#specifying-explicit-assembly-references ?

I don't use *.nuspec anymore. It's just dotnet pack.
Also, design time assembly is never explicitly referenced rather it's loaded by F# compiler from same folder as run-time based on CompilerServices.TypeProviderAssembly attribute value.

@dmitry-a-morozov I think @sergey-tihon may be on the right track. After referencing the https://www.nuget.org/packages/FSharp.Data.Npgsql/0.1.17-beta package using Visual Studio 2017 I can confirm that references to both design-time and runtime assemblies have been added. My understanding is that this is not what you want you only want the runtime assembly to be added as a reference

image

Looking at https://docs.microsoft.com/en-us/dotnet/core/tools/csproj I can't immediately see a way of excluding the design time DLL from being an implicit reference. You may have to use an explicit nuspec file for your packaging, see https://docs.microsoft.com/en-us/dotnet/core/tools/csproj#nuspecfile. Let me know if this works and we can add the magic incantations to a TPSDK sample

You might also get mileage by removing this https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/master/src/DesignTime/TypeProviderAssembly.fs#L9. This is saying that the DesignTime assembly is itself a Combo type provider. I don't think that's what you want.

Can confirm that setting the explicit references list makes the IDE experience work again! https://github.com/fsprojects/SwaggerProvider/pull/104

We've got a beta going through the pipeline that we can use to do an end-to-end test. It should be about half an hour before I can report back on that. (EDIT: Worked great!)

Confirm that it looks like the fix for SwaggerProvide (explicitly specify assemblies that should be referenced from NuGet package in nuspec/paket template)

Closing this as the issue appears solved (until next issue is found...) :)

@dsyme @sergey-tihon Thank you for help.
@dsyme I can claim that we have solid-working PostgreSQL type provider with advanced features and basic docs. https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/master/README.md

Was this page helpful?
0 / 5 - 0 ratings