Skip to content

fix(coordinator): present transfer results on the dialog's own window - #2319

Merged
datlechin merged 2 commits into
mainfrom
fix/transfer-alert-window-ownership
Aug 21, 2026
Merged

datlechin merged 2 commits into
mainfrom
fix/transfer-alert-window-ownership

Conversation

@datlechin

Copy link
Copy Markdown
Member

Fixes #2314.

The bug

A failed import shows its "Import failed" alert for about 9 milliseconds and then it is gone, so the reporter never learns why the import failed.

Root cause

A SwiftUI .sheet on macOS is a real AppKit sheet (SheetPresentationWindow). The transfer dialogs pick their alert's parent window with NSApp.keyWindow, read from inside .onChange at the moment the nested "Importing…" progress sheet is being dismissed. At that instant NSApp.keyWindow is the progress sheet. AppKit ends a sheet's children along with it, so the alert attaches, then its completion handler fires with NSApplication.ModalResponse.stop (-1000), isVisible goes false and it never renders. The completion still runs, so the app's own state resets and nothing is left on screen.

I reproduced the exact window layering (window → dialog sheet → progress sheet → alert) in a standalone AppKit/SwiftUI harness and measured five candidate targets:

alert's parent window result
NSApp.keyWindow (what we ship today) dies, completion -1000 after ~9ms
window: nil → AlertHelper.resolveWindow dies identically
.sheet(onDismiss:) + NSApp.keyWindow dies identically
the root connection window wrong, see below
the dialog's own sheet window survives until dismissed

Three things that look like fixes and are not:

  • Passing nil does not help. AlertHelper.isContentWindow is !(window is NSPanel) && styleMask.contains(.titled), and a SheetPresentationWindow is neither a panel nor untitled (mask 65), so it clears the filter and resolveWindow re-picks the same dying sheet.
  • Deferring does not help. .sheet(onDismiss:) fires 2 to 13 ms after the binding flips, while the sheet window is still visible and still key.
  • The root connection window is wrong. NSWindow.h: "If the window already has a presented sheet, it will queue up sheets presented after that… Critical sheets will skip this queuing process." presentImportFailure is .critical so it would appear, but it visibly displaces the still-open import dialog; and the three non-critical success alerts would queue behind the open dialog sheet and never appear at all.

Nothing at runtime can tell a dying sheet from a healthy one, so the rule has to hold at the call site: a dialog presents its alerts on the window it is hosted in.

The fix

Each transfer dialog captures its own NSWindow with the existing WindowAccessor and passes it to every alert and panel it raises. This is already the codebase's convention; MainContentCoordinator.swift:209 spells it out:

Direct reference to this coordinator's content window, used for presenting alerts. Avoids NSApp.keyWindow which may return a sheet window, causing stuck dialogs.

The three transfer dialogs were the ones not following it.

This is a targeted fix, not a restructure. I drafted a pendingOutcome + .sheet(onDismiss:) rewrite of the result plumbing and dropped it: the measurement shows same-transaction presentation on the correct window already works, so the rewrite was risk with no correctness gain.

Scope: 9 call sites, not 1

Both the success and the failure alerts in all three dialogs fail by this mechanism, so "Import completed with errors" (which carries the per-statement failure list) was flashing too, and so was every export result.

  • ImportDialog: success alert, failure alert, and the file open panel
  • RowImportSheet: success alert, failure alert
  • ExportDialog: success alert, showExportError, the loadDatabaseItems error, and the file save panel

The last three passed window: nil, which the table above shows fails the same way.

Also fixed: a failed import that would not say why

SQLImportPlugin threw rollbackFailed and discarded the original error. That error is the only carrier of the line number, the failing statement and the database's own message, so once a rollback also failed the user got "Transaction rollback failed" and nothing about what actually broke. Two more consequences of the same block: an import the user cancelled was reported as a rollback error, and an import that committed cleanly was reported as failed when only the foreign-key restore afterwards went wrong (the catch rolled back an already-committed transaction, which then failed).

The cause is now preferred and the rollback failure appended to it, without touching rollbackFailed's payload, which would break the PluginKit ABI. Two lifecycle fixes go with it: the .stopAndCommit path now records that it committed, so the catch no longer rolls back a committed transaction, and restoring foreign-key checks after a successful commit can no longer turn a committed import into a reported failure (it logs, matching what .stopAndCommit already did).

Making the alert stay up while it says the wrong thing would only half-answer the report, which is why this rides along.

Also: the report is copyable again

docs/features/import-export.mdx promised a "Copy Errors to Clipboard" button. Grep says it existed only in that sentence and as an orphaned string in the catalog; the button was lost when the bespoke result views became NSAlert. Both result alerts now carry a Copy Details button inside the accessory view (an NSAlert button would dismiss the alert), routed through ClipboardService.shared so it is testable. A single failure shows the statement, as before, but copies the line, the reason and the statement so the pasted text stands on its own. The docs line now matches.

Tests

  • TransferAlertWindowOwnershipTests scans Views/Import and Views/Export and fails if any alert presenter is handed NSApp.* or nil. This is the only thing that can catch a regression, since the defect is invisible to a unit test. I verified the guard actually fails: reintroducing window: NSApp.keyWindow in ImportDialog turned it red, and reverting turned it green. Two more cases close the ways it could pass while broken: one asserts every dialog that presents on hostWindow also captures it with WindowAccessor (spelling the argument proves nothing if the capture is gone and hostWindow is nil forever), and one asserts the scan really reaches all three dialogs, so it cannot pass by scanning nothing.
  • SQLImportFailureTests covers the error composition: the cause survives a failed rollback with its line and statement intact, a cancellation stays a cancellation, and an unstructured cause keeps both messages.
  • SQLImportTransactionTests drives the real SQLImportPlugin.performImport against a recording sink and pins the transaction lifecycle: stop-and-commit never rolls back what it committed and still reports the failing statement rather than a rollback error, stop-and-rollback rolls back and never commits, and a committed import survives a failed foreign-key restore. Verified these bite too: reverting both plugin fixes turns exactly those three red and leaves the fourth green.
  • TransferFailureReportTests gains the failure-report composition and the clipboard write, asserted against an injected ClipboardProvider fake (NSPasteboard.general returns nil on CI).

No UI test. Reaching the import dialog goes through an NSOpenPanel, and there is no precedent for driving one anywhere in TableProUITests and no seam that sets the import file directly. I would rather say so than land a flaky CI test.

No screenshots. The "before" state is the absence of any alert, which is not a screenshot, and scripted capture of the "after" state hit a macOS UI-automation authorization prompt I could not satisfy in this environment. The measured evidence is in the table above.

Verified

Run through .claude/skills/fix-issue/scripts/verify.sh against this branch:

  • generate PASS
  • build PASS
  • test SQLImportFailureTests SQLImportTransactionTests TransferFailureReportTests TransferAlertWindowOwnershipTests AlertWindowResolutionTests PASS, 25 executed, 25 passed
  • lint on all 8 changed paths PASS, 0 violations
  • SQLImport plugin scheme builds clean

plugins (the AllPlugins aggregate) fails locally on this machine, in the vendored oracle-nio package's @TaskLocal macro expansion (unknown attribute 'usableFromInlinenonisolated'). That is a known local-only toolchain breakage unrelated to this change; no error in the log comes from TablePro source. CI's own plugin step is what covers it.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@mintlify

mintlify Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
TablePro 🟢 Ready View Preview Aug 21, 2026, 5:23 AM

💡 Tip: Enable Workflows to automatically generate PRs for you.

@datlechin
datlechin merged commit f663af8 into main Aug 21, 2026
4 checks passed
@datlechin
datlechin deleted the fix/transfer-alert-window-ownership branch August 21, 2026 05:25
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.

导入报错的错误原因弹窗太快消失了,都没看清错误原因,错误原因的窗口就关闭了

1 participant