Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VS 2019 16.8.2 XAML designer crashes and / or VS crashes when C++ or shared projects are present in solution #183

Open
Gawson opened this issue Nov 26, 2020 · 3 comments

Comments

@Gawson
Copy link

Gawson commented Nov 26, 2020

After making some first steps with Avalonia, I've added Avalonia-based project to entire project solution and got Avalonia XAML editor unusable - every attempt to open axaml file ended in VS crashing. I was able to resolve this with two changes:

  1. Adding
if (result.Contains(reference))
                return;

in
Services/SolutionService.cs in FlattenProjectReferences function as shown below to avoid project reference loop and stack overflow exception while unrolling references.

        private static void FlattenProjectReferences(
            Dictionary<Project, ProjectInfo> projects,
            Project reference,
            HashSet<Project> result)
        {
            if (result.Contains(reference))
                return;

            result.Add(reference);

            if (projects.TryGetValue(reference, out var info))
            {
                foreach (var child in info.ProjectReferences)
                {
                    FlattenProjectReferences(projects, child, result);
                }
            }
        }
  1. Wrapping part of GetProjectsAsync function in the same file with try/catch because C++ projects were causing exceptions due to some unresolvable properties.

First foreach loop modified:

foreach (var project in FlattenProjects(_dte.Solution))
            {
                if (project.Object is VSProject vsProject)
                {
                    try
                    {
                        var projectInfo = new ProjectInfo
                        {
                            IsStartupProject = startupProjects?.Contains(project.UniqueName) ?? false,
                            Name = project.Name,
                            Project = project,
                            ProjectReferences = GetProjectReferences(vsProject),
                            References = GetReferences(vsProject),
                        };

                        result.Add(project, projectInfo);

                        // If the project is a .csproj and it has no references then we assume its
                        // references are not yet loaded. We might want to handle e.g. F# and VB
                        // projects here too.
                        if (IsCsproj(projectInfo) && projectInfo.References.Count == 0)
                        {
                            uninitialized.Add(vsProject, projectInfo);
                        }
                    } catch(Exception exc)
                    {
                        ;
                    }
                }
            }

I do not know this extension code deeply so I am not sure if this brakes something but those changes made Avalonia usable in huge solution with multiple projects and mixed technologies.
If this is already fixed in some branch please close this issue. Before posting I've checked code from master but it is also crashing.

@napana
Copy link

napana commented Nov 28, 2020

I bet it crashes when it access Reference.SourceProject property. I was struggling with this a lot today. Didn't know why it does not happen in any other solution but you got it, i have C project in my solution. I solved it by wrapping the access to Reference.SourceProject.

        private static IReadOnlyList<Project> GetProjectReferences(VSProject project)
        {
            return project.References
                .OfType<Reference>()
                .Where(x => IsProjectReference(x))
                .Select(x => x.SourceProject)
                .ToList();
        }

        private static IReadOnlyList<string> GetReferences(VSProject project)
        {
            return project.References
                .OfType<Reference>()
                .Where(x => IsNonProjectReference(x))
                .Select(x => x.Name).ToList();
        }

        private static bool IsProjectReference(Reference r)
        {
            try
            {
                return r.SourceProject != null;
            }
            catch (Exception ex)
            {
                // why??
                Log.Logger.Error(ex, "Error when accessing Reference.SourceProject property.");
            }

            return false;
        }

        private static bool IsNonProjectReference(Reference r)
        {
            try
            {
                return r.SourceProject == null;
            }
            catch (Exception ex)
            {
                // why??
                Log.Logger.Error(ex, "Error when accessing Reference.SourceProject property.");
            }

            return false;
        }

@Gawson
Copy link
Author

Gawson commented Nov 29, 2020

Have you checked the second problem with unrolling projects structure? There are sometimes reference loops that ends with stack overflow. I'm not sure if this is related to the main issue or not.

@napana
Copy link

napana commented Nov 29, 2020

Have you checked the second problem with unrolling projects structure? There are sometimes reference loops that ends with stack overflow. I'm not sure if this is related to the main issue or not.

No i had only problem with the designer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants