★ wanayoo — archive 1999 https://github.com/NuPattern/NuPattern/issues/2Nouvelle recherche | Portail wanayoo
Skip to content
This repository has been archived by the owner. It is now read-only.

Unfolding a T4 template might fail if a project properties window is open #2

Open
fsimonazzi opened this issue Jul 8, 2014 · 0 comments
Labels
bug

Comments

@fsimonazzi
Copy link

@fsimonazzi fsimonazzi commented Jul 8, 2014

This is a weird one. When unfolding t4 templates, NuPattern tries to close windows that might have been opened when adding a file (https://github.com/NuPattern/NuPattern/blob/be18383399bf1db558e52dd98e9af40a6fc45807/Src/Common/Source/Common.VisualStudio/Solution/VsFileTemplate.cs#L67-74)

                //
                // The file may have opened anyway, if we're not supposed to open it, we'll search for
                // the matching window and close it
                //
                foreach (EnvDTE.Window w in container.DTE.Windows)
                {
                    if (newlyAddedFile.Equals(w.ProjectItem))
                    {
                        w.Close(EnvDTE.vsSaveChanges.vsSaveChangesNo);
                        break;
                    }
                }

If one of the open windows is a Project Properties page, asking the window for its ProjectItem throws an InvalidCastException.

Repro steps with the MVC sample:

  1. Create a new app
  2. Build.
  3. Open the properties of the MVC app project
  4. While the property page is still open, add a controller.

You'll get an error:
image

NuPattern.Library.Automation.EventAutomation Error: 0 : Event automation 'GenerateControllerOnInstantiate' failed.
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'EnvDTE.ProjectItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{0B48100A-473E-433C-AB8F-66B9739AB620}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
   at Microsoft.VisualStudio.Platform.WindowManagement.DTE.Window.get_ProjectItem()
   at Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase.<get_ProjectItem>b__2c()
   at Microsoft.VisualStudio.Shell.ThreadHelper.Invoke[TResult](Func`1 method)
   at Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase.get_ProjectItem()
   at NuPattern.VisualStudio.Solution.VsFileTemplate.Unfold(String name, IItemContainer parent)
   at NuPattern.VisualStudio.Solution.VsFileContentTemplate.Unfold(String name, IItemContainer parent)
   at NuPattern.VisualStudio.Solution.VsItemContainerExtensions.AddContent(IItemContainer parent, String content, String name, Boolean overwrite, Boolean openFile, Encoding encoding)
   at NuPattern.Runtime.UriProviders.TextTemplate.Unfold(String name, IItemContainer parent)
   at NuPattern.Library.Commands.GenerateModelingCodeCommand.Execute()
   at NuPattern.Library.Commands.GenerateProductCodeCommand.Execute()
   at NuPattern.Library.Automation.CommandAutomation.Execute(IDynamicBindingContext context, Boolean withAutomation)
   at NuPattern.Library.Automation.CommandAutomation.Execute()
   at NuPattern.Library.Automation.EventAutomation.ExecuteCommand()
   at NuPattern.Library.Automation.EventAutomation.<>c__DisplayClassd.<Execute>b__a()
   at NuPattern.VisualStudio.TraceSourceExtensions.DoShield(ITracer traceSource, Action action, String format, Boolean showUI, String[] args)

This is arguably an issue in VS's code, but NuPattern could catch this exception and avoid failing. From what I could see there is no way to query the window to see if it would throw when returning its ProjectItem, as the properties have values just like the window for a code editor:

        window.Caption  "Solution Explorer" string
        window.Type vsWindowTypeSolutionExplorer    EnvDTE.vsWindowType
        window.Kind "Tool"  string
        window.ProjectItem  null    EnvDTE.ProjectItem

        window.Caption  "Application1.Contracts"    string
        window.Type vsWindowTypeDocument    EnvDTE.vsWindowType
        window.Kind "Document"  string
+       window.ProjectItem  'window.ProjectItem' threw an exception of type 'System.InvalidCastException'   EnvDTE.ProjectItem {System.InvalidCastException}

        window.Caption  "EndpointConfig.cs" string
        window.Type vsWindowTypeDocument    EnvDTE.vsWindowType
        window.Kind "Document"  string
+       window.ProjectItem  COM Object  EnvDTE.ProjectItem {System.__ComObject}

A try/catch on InvalidCastException on that look should fix this.

@fsimonazzi fsimonazzi added the bug label Jul 8, 2014
@fsimonazzi fsimonazzi changed the title Unfolding a template might fail if a project properties window is open Unfolding a T4 template might fail if a project properties window is open Jul 8, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.