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.
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.
VS 2017

Ionide

Environment 1
Environment 2
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)

https://github.com/demetrixbio/FSharp.Data.Npgsql/tree/visualfsharp-issue-4338
Steps:
1) from repo root
ResultType https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/visualfsharp-issue-4338/tests/SampleApp/Program.fs#L9. Compilation fails with 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

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.
@sergey-tihon
Temp fix
https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/1696cd4d8fc6dc04ba50e7f4d8b2e77059c22c46/src/DesignTime/TypeProviderAssembly.fs#L14
https://github.com/demetrixbio/FSharp.Data.Npgsql/blob/1696cd4d8fc6dc04ba50e7f4d8b2e77059c22c46/src/DesignTime/TypeProviderAssembly.fs#L41
@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

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
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