Skip to content

[VSIntegration] ShadowDesigner: solve the "save solution?" prompt th… - #2183

Merged
RobertvanderHulst merged 1 commit into
devfrom
fix/shadow-designer-solution-save-prompt-v2
Nov 18, 2026
Merged

RobertvanderHulst merged 1 commit into
devfrom
fix/shadow-designer-solution-save-prompt-v2

Conversation

@fforay

@fforay fforay commented Nov 17, 2026

Copy link
Copy Markdown
Member

Description

Problem

After the shadow WinForms designer was used, closing the solution (or VS) always asked to save the solution file. Answering No made VS 2026 report on the next open: "The following projects from the solution were not found: ...{Project}.ShadowDesigner{Project}.ShadowDesigner.csproj". VS 2022 shows no dialog, but has the same stale entry.

Cause:

  1. Adding the companion C# project makes the solution dirty.
  2. After each designer save, the bridge saved all documents (first File.SaveAll, on dev now SaveDirtyDocuments), so the solution file was saved with the companion in it, along with any unsaved change of the user's.
  3. Removing the companion on close made the solution dirty again, so VS showed the save prompt.
  4. No kept the saved companion entry while its folder was deleted after close.

With an unsaved solution change (for example a new solution folder), Ctrl+S in the designer also failed with "A recursive save operation is in progress for '....slnx'": the save of all documents runs inside VS's own OnAfterSave callback and tries to save the solution while VS is still in the first save.

Changes

Solution close (ShadowDesignerCleanup.cs, SolutionWiring.cs)

  • The cleanup runs in OnQueryCloseSolution, which fires for every close: Close Solution, exiting VS, opening another solution.
  • Adding the companion (EnsureProjectInSolution) and removing it on close are saved right away only when the solution had no other unsaved changes. The companion then never causes a prompt, and the user's own unsaved changes still get the normal prompt.
  • New helpers in SolutionWiring: IsSolutionDirty (VSPROPID_IsSolutionDirty), GetSolutionFile, SaveSolutionFile (SaveSolutionElement(SLNSAVEOPT_SaveIfDirty), skipped for a never-saved solution).

Solution file cleanup after close (new SolutionFileCleanup.cs)

  • When the prompt was shown and the user answered No, the file on disk can still list the companion. After close, only the companion entries are removed:
    • .slnx: the companion <Project> elements, and the "Shadow Designer (generated)" <Folder> once it is empty.
    • .sln: the companion Project ... EndProject blocks with their configuration and NestedProjects lines, and the folder once nothing is nested in it.
  • The rest of the file, its encoding (BOM) and line endings are kept. A file that needs no change is not rewritten.

Saving before a sync (CompanionSaveWatcher.cs)

  • OnAfterSave and OnBeforeDocumentWindowShow now save only the companion's own dirty .cs/.resx documents (SaveCompanionDocuments), which is all the syncs read from disk. The user's solution and other documents are no longer saved behind their back, and the recursive save error is gone.
  • SaveDirtyDocuments (all documents) is kept for Debug/Start, where VS saves everything for the build anyway. Its saving flag is now reset in a finally; before, a failed or cancelled save left it set and nothing was saved again for the rest of the session.
  • OnAfterSave called TryHandleResxOnlySave(path) also when path is null or empty, so path.EndsWith could throw inside the RDT callback. That call is removed.

Caret in a new event handler (CompanionSaveWatcher.cs)

  • Double-clicking a control without a handler created the stub, but the caret landed at the end of the file (a second double-click worked). EventHandlerSync writes the stub to the .prg on disk, but an already open .prg is only reloaded by VS when the UI thread is idle, which never happens while the new-handler redirect runs (JoinableTaskFactory.Run in OnBeforeDocumentWindowShow). The line number from the file then pointed past the end of the old buffer.
  • RedirectNowAsync now reloads the open .prg first (ReloadOpenDocument: IVsPersistDocData.ReloadDocData(RDD_IgnoreNextFileChange)), only when it has no unsaved changes. NavigateToMethodAsync searches the METHOD name( line in the editor buffer instead of the file, with a short poll as a fallback.

Relation to 289fec7 and 884ce65

289fec7 addressed the same stale-companion problem. Its close handling is replaced here, for these reasons:

289fec7 This PR
When the cleanup runs Intercept of the Close Solution command only; exiting VS or opening another solution bypasses it OnQueryCloseSolution, every close
Saving the solution Save Solution on every close, so the user's own unsaved solution changes are saved without asking Saved without asking only when the companion is the only change
.slnx cleanup Removes the whole "Shadow Designer (generated)" folder (user projects in it too); rewritten with XmlDocument on every close Removes only the companion entries and the folder once empty; formatting kept, unchanged file not rewritten
.sln cleanup Blanks the matching line and the next one; configuration and NestedProjects lines stay; BOM dropped; lines[i + 1] can run past the end Removes the whole block and the lines naming its GUID; BOM and line endings kept
Saving before a sync SaveDirtyDocuments = all documents, from inside OnAfterSave (recursive .slnx save) Companion .cs/.resx only

Kept as they are: the EnvDTE removal (884ce65), the save before Debug/Start, the event migration fix in RoslynToCodeDom, and the new IVsTextView-based navigation (extended as described above).

…t might leave a stale companion. Move the caret inside a new event handler

After using the designer, closing the solution always asked to save it, and answering No made VS 2026 report the companion project as not found on the next open.
@RobertvanderHulst
RobertvanderHulst merged commit 408b08c into dev Nov 18, 2026
1 check passed
@RobertvanderHulst
RobertvanderHulst deleted the fix/shadow-designer-solution-save-prompt-v2 branch November 18, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants