roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Completion missing members of lambda parameter in fault tolerance case

Open Pilchie opened this issue 10 years ago • 113 comments

In the code below, Task should be shown in completion from one of the overloads of ThenInclude but it doesn't.

This case is interesting because there are two applicable overloads. The ideal thing here would be to merge their individual members for the completion lists, but we don’t do that – we just pick one overload as the “best so far”, and it’s the one with ICollection<TPreviousProperty>, not the one with just TPreviousProperty.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace ThenIncludeIntellisenseBug
{
    class Program
    {
        static void Main(string[] args)
        {
            var registrations = new List<Registration>().AsQueryable();

            // type "a => a." and only ICollection<T> and Enumerable members appear
            var reg = registrations
              .Include(r => r.Activities).ThenInclude(a => a.$$
        }
    }

    internal class Registration
    {
        public ICollection<Activity> Activities { get; set; }
    }

    public class Activity
    {
        public Task Task { get; set; }
    }

    public class Task
    {
        public string Name { get; set; }
    }

    public interface IIncludableQueryable<out TEntity, out TProperty> : IQueryable<TEntity>
    {
    }

    public static class EntityFrameworkQuerybleExtensions
    {
        public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
         this IQueryable<TEntity> source,
          Expression<Func<TEntity, TProperty>> navigationPropertyPath)
         where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }

        public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }

        public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, TPreviousProperty> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class
        {
            return default(IIncludableQueryable<TEntity, TProperty>);
        }
    }
}

Pilchie avatar Jan 29 '16 00:01 Pilchie

Is this something we can fix on the IDE side, or do we need changes/additions to compiler APIs.

Pilchie avatar Jan 29 '16 17:01 Pilchie

@Pilchie This was on your list of places where the compiler doesn't help much. I'm not sure what you would want of the compiler here. The compiler doesn't make up types that combine the members of existing types for the purposes of error recovery. Is that what you would want? @rchande Please feel free to suggest what we could do to help here.

gafter avatar Jun 02 '16 00:06 gafter

@Pilchie @gafter @rchande Could you please provide an update on this issue? It is marked as blocked but from reading the comments it seems there was some outstanding question on whether this is really a bug on Roslyn, or something that would need to be fixed on the way the IDE uses Roslyn to get the complete list of applicable members. If the latter, what would be the best way to get some movement on this?

Customers keep hitting this limitation, as you can see by the number of GitHub issues that reference this one (there is about twice as many questions on email and other websites about the same problem).

divega avatar Feb 08 '17 07:02 divega

@gafter Given the code:

var reg = registrations
              .Include(r => r.Activities).ThenInclude(a => a.$$

When the IDE asks "what is the SymbolInfo for 'a'?" I think it would be good if we could get a SymbolInfo with no Symbol, but two candidate symbols. One would be The parameter called 'a' with type 'ICollection<TPreviousProperty>' and the other would be The parameter called 'a' with type 'TPreviousProperty'

The IDE could then get the members of each and union them in a single list to show users.

CyrusNajmabadi avatar Feb 08 '17 07:02 CyrusNajmabadi

Hey guys,

Just wanted to let you know I'm hitting this issue. I'm using the latest VS 2017 Community RC.

colltoaction avatar Feb 12 '17 23:02 colltoaction

I am also feeling this issue. Also using the latest VS 2017 Community RC.

Shadetheartist avatar Feb 22 '17 18:02 Shadetheartist

This issue is also present in VSCode using the C# extension.

0xdeafcafe avatar Mar 21 '17 14:03 0xdeafcafe

Same issue reported here: https://developercommunity.visualstudio.com/content/problem/50498/intellisense-not-working-for-ef-core-theninclude.html

divega avatar Apr 30 '17 16:04 divega

Any updates? Please fix! :)

spottedmahn avatar Apr 30 '17 17:04 spottedmahn

Fixing this would probably require a few weeks of effort.

There is no mechanism right now for the semantic model to “merge” results from distinct (erroneous) trial bindings of a lambda. There is no mechanism for binding an expression with the type of a variable being “either this or that”. The GetTypeInfo API, which the IDE currently uses to get the type from which its completion list is populated, has no mechanism for returning more than one type. This would require some effort in the compiler and in the IDE code and possibly some new APIs. It also risks degrading the IDE experience for the cases that we already handle well (because instead of returning what we think is the “best” result, we’d return all possible results). If we make the completion story better, we are likely to degrade the quality of data we provide when you hover over the parameter (e.g. its type).

The most likely way we’d handle this would be for an unbound lambda’s error-recovery binding to use a new kind of error type for the parameters that merge the members of all the possible types from the parameters of the delegates the lambda could bind to. Error types today don't have any members, but these new types would have lots of members merged from different types. In order to maintain the invariant that a types's members have a ContainingType that is the type from which the member was fetched, we may have to invent a number of other kinds of error symbols.

gafter avatar May 01 '17 17:05 gafter

Reported again in https://developercommunity.visualstudio.com/content/problem/50498/intellisense-not-working-for-ef-core-theninclude.html

Pilchie avatar May 02 '17 00:05 Pilchie

@Pilchie That's what https://github.com/dotnet/roslyn/issues/8237#issuecomment-298243181 says.

gafter avatar May 02 '17 01:05 gafter

There is no mechanism right now for the semantic model to “merge” results from distinct (erroneous) trial bindings of a lambda...

@gafter then the solution that @CyrusNajmabadi described at https://github.com/dotnet/roslyn/issues/8237#issuecomment-278251000 does not apply?

divega avatar May 02 '17 02:05 divega

@divega I'm saying that there is no mechanism to do that today. We could add such a mechanism, but that could be a lot of work.

gafter avatar May 02 '17 23:05 gafter

For example, if we expose two different parameters as @CyrusNajmabadi suggests, each would have to have a corresponding lambda (even though there is only one lambda in the source).

gafter avatar May 02 '17 23:05 gafter

Hey guys - just want to say thanks for problem solving this for us in the community. Hopefully you guys can find a solution that is a reasonable number of hours to enchance It.

spottedmahn avatar May 03 '17 02:05 spottedmahn

This really does need to get fixed, I'm glad I found the bug reference after only 30 minutes of trying to figure out why it wasn't giving me the options I expected and wasn't matching EFCore documentation. This bug is likely a huge source of wasted time and frustration for anyone trying to use Include().ThenInclude().... or at least someone should update the ThenInclude documentation to indicate that there is a intellisense bug to stop people like me from pulling their hair out.

dguisinger avatar May 03 '17 17:05 dguisinger

@gafter, @Pilchie, @CyrusNajmabadi and others:

TL;DR: Even getting the union of all possible members is not ideal for this API. However we believe a (hopefully) simpler solution would be sufficient to fix the EF Core experience: somehow change Intellisense to give preference to the other overload from the one it is using now.

Details

Today @smitpatel (one of the EF Core developers) came up with an insight on this issue that I think may create an opportunity for a stopgap solution. Hopefully this will save you two weeks :smile::

It turns out that from the perspective of the usability of this particular API, we are not really interested in Intellisense showing the union of all the possible members in the completion list. It should be sufficient for the vast majority of real-world EF Core scenarios if Intellisense gave preference to the "right" overload of ThenInclude().

E.g. currently given this query:

var reg = registrations
              .Include(r => r.Activities).ThenInclude(a => a.$$

The following overload is (seemingly) arbitrarily picked:

public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, TPreviousProperty> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class

... and hence Intellisense shows the members of ICollection<Activity> in the completion list.

We actually need this one to be picked:

 public static IIncludableQueryable<TEntity, TProperty> ThenInclude<TEntity, TPreviousProperty, TProperty>(
            this IIncludableQueryable<TEntity, ICollection<TPreviousProperty>> source,
            Expression<Func<TPreviousProperty, TProperty>> navigationPropertyPath) where TEntity : class

... so that the members of Activity are displayed in the completion list.

I am not sure if it would be acceptable to plainly invert the choice in the code used by Intellisense or if we would need a way (e.g. an attribute) to designate the preferred overload. If the later, I think it could have other uses besides this scenario, like addressing the question in
http://stackoverflow.com/questions/1904876/can-visual-studios-c-sharp-intellisense-be-given-a-hint-to-display-a-certain-me.

There is of course a corner case in which an entity type happens to implement ICollection<T>, e.g.:

public class Registration : ICollection<Foo> { ... }
...
var act = activities
              .Include(r => r.Registration).ThenInclude(a => a.$$

From the perspective of everything that is possible to express in the language, I believe it is still valid to say that ideally Intellisense should show the union of all the possible members, but pragmatically I believe this scenario is kind of fringe and it should be ok if Intellisense always preferred the overload that takes an IIncludableQueryable<TEntity, ICollection<TPreviousProperty>>.

I would say even more: if you come up with a solution that displays the union of all the possible members in the completion list, the usability of this EF Core API is still going to be bad unless the members corresponding to our preferred overload are displayed at the top of the completion list.

cc @ajcvickers

divega avatar May 04 '17 00:05 divega

@dguisinger good point about the documentation. I have crated an issue in our docs: https://github.com/aspnet/EntityFramework.Docs/issues/394.

divega avatar May 04 '17 00:05 divega

It should be sufficient for the vast majority of real-world EF Core scenarios if Intellisense gave preference to the "right" overload of ThenInclude().

Do you have a suggestion on how the compiler would know that one particular overload was better than the other?

CyrusNajmabadi avatar May 04 '17 00:05 CyrusNajmabadi