Skip to content

fix(check): edit plugin.xml inside the build instead of on the UI thread - #1665

Open
joaodinissf wants to merge 1 commit into
masterfrom
fix/check-plugin-xml-write-cancel
Open

joaodinissf wants to merge 1 commit into
masterfrom
fix/check-plugin-xml-write-cancel

Conversation

@joaodinissf

@joaodinissf joaodinissf commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

What

The Check builder participant now edits plugin.xml inside the build, on the build thread. It no longer goes through Display.syncExec and PDEModelUtility.

  • New PluginXmlEditor (package-private, com.avaloq.tools.ddk.check.ui.builder): reads the file into a private document, loads PDE's text model of it (org.eclipse.pde.internal.core.text.plugin.PluginModel), lets the extension helpers change the model, applies the resulting text edits, and writes the file only if its content changed.
  • CheckExtensionGenerator.updateExtensions and removeExtensions use it for the standard plugin.xml. modifyModel and its syncExec are gone. The build's monitor is still checked before the edit.
  • CheckExtensionGeneratorAutoBuildTest in com.avaloq.tools.ddk.check.ui.test covers the edit during an auto-build.

This replaces the first version of this PR, which kept the syncExec and passed PDE a NullProgressMonitor. That stopped the loop described below, but it was a workaround: every save still interrupted the build, so N catalogs needing an edit cost N + 1 runs of the auto-build, and the write stayed on the UI thread.

Why

With auto-build on, a Check catalog whose extensions are missing or outdated in the project's plugin.xml makes the project rebuild in a loop, and plugin.xml never gets the extensions:

  1. The participant runs PDEModelUtility.modifyModel in Display.syncExec. On the UI thread, PDE saves the edited file. Workspace.prepareOperation sees that the file's rule conflicts with the build rule and interrupts the running auto-build.
  2. The UI thread then waits for the workspace rule with the monitor it was given. That is the build's monitor, and the Xtext builder's monitor reports an interrupted auto-build as canceled. ThreadJob.joinRun checks the monitor first, so the save throws OperationCanceledException and nothing is written.
  3. The Xtext builder handles the cancellation by restoring its queued deltas, and the auto-build job reschedules itself because it was interrupted. The next run sees the same deltas, makes the same edit and is canceled the same way.

The cause is the save from another thread while the build runs. A workspace operation on the build thread itself does not interrupt the build: AutoBuildJob.interrupt ignores a request that comes from the build's own thread. That is how the participant already writes MANIFEST.MF, a fragment.xml, a plug-in file with a non-standard name and a new plugin.xml.

How it works

PDEModelUtility.modifyModel does three things when no editor is open: it loads the text model from the file's text buffer, collects the text edits that the model changes produce (XMLTextChangeListener), and applies and commits them. PluginXmlEditor does the same with a private Document instead of the shared file buffer, and writes the result with IFile.setContents. The text model and the listener are in org.eclipse.pde.core, so the edit needs neither the UI thread nor a workbench.

Because the edits are the same text edits as before, the parts of plugin.xml that do not change keep their formatting and comments. The file is read and written in its own charset. If the edits leave the content as it was, the file is not written.

Behaviour that changes

  • One run of the auto-build for any number of catalogs. Nothing interrupts the build any more.
  • Open PDE manifest editor without unsaved changes. Before, the edit was applied to the editor's model: the editor became dirty and nothing reached the disk until the user saved; closing the editor without saving lost the extensions. Now the file is written and the editor stays clean.
  • Open PDE manifest editor with unsaved changes. Before, the edit joined the user's unsaved changes in the editor. Now the file is written under the editor, and the user gets the usual "file changed on disk" conflict when saving. If they overwrite, the extensions are missing until the catalog is built again.
  • Text editor with unsaved changes. Before, PDEModelUtility committed the shared text buffer, which saved the user's unsaved text as a side effect. Now the build writes only its own edit; the editor keeps its unsaved text and the user gets the same conflict when saving.
  • Errors. If plugin.xml cannot be parsed into the text model, cannot be edited or cannot be encoded in its charset, the edit fails with a CoreException, which CheckBuilderParticipant logs. PDE logged and swallowed these. MANIFEST.MF is still updated in that case, as before.
  • Local history is kept for the write, as the buffer commit did.

How tested

CheckExtensionGeneratorAutoBuildTest runs in the workbench of com.avaloq.tools.ddk.check.ui.test, with auto-build on, in a plug-in project that has a plugin.xml. Each test first builds one catalog explicitly, then makes its change and waits for the auto-build: it joins the jobs of ResourcesPlugin.FAMILY_AUTO_BUILD until none has been pending for 1 s, for at most 60 s. It asserts that the wait ended before its bound, that the auto-build job ran at most twice, and the content of plugin.xml.

The same test class was run against three versions of the product code: this PR, the first version of this PR (the NullProgressMonitor workaround) and master. "Runs" is the number of runs of the auto-build job for the change.

test change and assertion this PR workaround master
testAddCatalog one catalog added; extensions present pass, 1 run pass, 2 runs fails at the 60 s bound, 252 runs
testChangeCatalog the catalog drops its grammar; validator extension updated pass, 1 pass, 2 fails at the bound, 257
testAddCatalogsTogether three catalogs added in one workspace operation; all extensions present pass, 1 fails, 4 runs fails at the bound, 238
testKeepHandWrittenContent a comment and an oddly formatted extension added by hand, then a catalog; the hand-written text is unchanged, byte for byte pass, 1 fails at the bound, 144 fails at the bound, 251
testUnchangedExtensions a catalog's message changes, its extensions do not; plugin.xml keeps its modification stamp pass, 1 pass, 1 pass, 1
testWindowsLineEndings plugin.xml has CRLF line endings, a catalog is added; the file has CRLF only pass, 1 pass fails at the bound, 250
testByteOrderMark plugin.xml starts with a byte order mark, a catalog is added; one mark, prolog unchanged pass, 1 not run in its final form fails at the bound, 238
testEditorWithoutChanges a text editor is open on plugin.xml, a catalog is added; the editor shows the new extensions and is not dirty pass, 1 pass, 2 fails at the bound, 240
testEditorWithUnsavedChanges the same with unsaved text in the editor; the file has the extensions and not the unsaved text, the editor keeps its text and stays dirty pass, 1 fails: the unsaved text is saved fails at the bound, 253
testManifestEditorWithoutChanges a PDE manifest editor is open, a catalog is added; the file has the extensions and the editor is not dirty pass, 1 fails: extensions not in the file fails: extensions not in the file

testUnchangedExtensions passes everywhere; it guards against an implementation that always writes.

Same output as the old path. After each test, the content of plugin.xml was compared between this PR and the workaround, which still edits through PDEModelUtility. The files are byte-identical in every case, apart from the two intended differences above: the extensions that the old path left in the manifest editor's model, and the unsaved editor text that it saved.

Local run on macOS with OpenJDK 21: ./mvnw verify checkstyle:check pmd:pmd pmd:cpd pmd:check pmd:cpd-check spotbugs:check -f ./ddk-parent/pom.xml -pl :ddk-target,:com.avaloq.tools.ddk.check.ui.test -am. All 50 tests of the module pass.

No version bump: com.avaloq.tools.ddk.check.ui is at 17.3.6 on master, above the 17.3.5 of the last release.

Not tested

  • Removing extensions. removeExtensions goes through the same PluginXmlEditor, but no test asserts a removal. I tried two: a catalog that no longer validates and a deleted catalog. In both the extensions stay in plugin.xml, with this PR and with the old path alike (identical files), so this is existing behaviour and outside this change.
  • A PDE manifest editor with unsaved changes. The clean editor is covered; the dirty one is described above from reading the code.
  • The DDK builder. The test runtime uses the stock Xtext builder and Xtext's RegistryBuilderParticipant. DDK's RegistryBuilderParticipant catches every exception of a participant, including a cancellation. The edit no longer causes a cancellation, so that path is not reached by it, but a build with several catalogs under the DDK builder was not run.
  • A headless build. The edit itself uses no UI class, but the participant was not run without a workbench.
  • A plugin.xml that is not UTF-8. The code reads and writes the file's charset and refuses to write a character that charset cannot represent; the tests use UTF-8 only.

Alternatives considered

  • Keep the syncExec and pass a NullProgressMonitor (the first version of this PR). It ends the loop but keeps one interrupted run per catalog and the write on the UI thread.
  • Save through the workspace plug-in model, as the participant does for a plug-in file with a non-standard name. That serializes the whole model and would reformat a hand-edited plugin.xml and drop its comments.
  • Call PDEModelUtility directly from the build thread. It looks for open editors through the workbench and, if one has the file open, defers the edit with Display.execute, which is asynchronous off the UI thread. The edit would race the build.
  • Collect the edits of all catalogs into one modification per build. Not needed once nothing interrupts the build.

🤖 Generated with Claude Code

@rubenporras rubenporras left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks very wrong on first sight. Are you sure? Are there not other options?

@joaodinissf
joaodinissf force-pushed the fix/check-plugin-xml-write-cancel branch from 9341722 to c556fbd Compare November 28, 2026 14:02
@joaodinissf joaodinissf changed the title fix(check): let the plugin.xml update complete during an auto-build fix(check): edit plugin.xml inside the build instead of on the UI thread Nov 28, 2026
@joaodinissf

joaodinissf commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Fair challenge. The first version was a workaround: it stopped the loop, but each save still interrupted the build (N new catalogs cost N+1 builds) and the write stayed in a syncExec on the UI thread. I have replaced it with a proper fix, now pushed to this PR.

The loop came from saving plugin.xml from the UI thread while the auto-build runs: the save interrupts the build, Xtext's monitor reports that as cancelled, and we passed the same monitor to PDE, so the save cancelled itself and the build restarted without writing the file. The PR now edits plugin.xml on the build thread, with the same PDE text model and text edits on a private copy of the file. Formatting and comments are kept, nothing interrupts the build, and one build handles any number of catalogs. The description lists what is tested and what is not; it stays a draft while I add a few more tests.

Posted by Claude Code on behalf of @joaodinissf.

CheckExtensionGenerator updated plugin.xml through PDEModelUtility in a
syncExec on the UI thread. Saving the file from another thread
interrupts a running auto-build. The Xtext builder's monitor reports an
interrupted auto-build as canceled, and that monitor was passed to PDE,
so the save canceled itself while waiting for the workspace rule. The
build ended canceled, kept its deltas and was rescheduled, and the next
run did the same: a project with a Check catalog whose extensions were
missing or outdated in plugin.xml was rebuilt over and over and never
got them, until a full or explicit build wrote the file.

PluginXmlEditor now makes the edit on the build thread. It loads PDE's
text model of the file from a private copy of the content, applies the
changes as text edits, as PDEModelUtility does when no editor is open,
and writes the file only if its content changed. Formatting and comments
of the untouched parts are kept as before. Nothing is saved from another
thread, so the build is not interrupted: one run of the auto-build
handles any number of catalogs. An editor that has plugin.xml open sees
the write as a change on disk; the build no longer edits the model of
an open manifest editor and no longer saves a text editor's unsaved
content.

CheckExtensionGeneratorAutoBuildTest covers a catalog added to a project
with a plugin.xml, a catalog whose extension changes, three catalogs
added together, hand-written content, unchanged extensions, Windows line
endings, a byte order mark, a text editor with and without unsaved
changes, and an open manifest editor.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@joaodinissf
joaodinissf force-pushed the fix/check-plugin-xml-write-cancel branch from c556fbd to baf2791 Compare November 28, 2026 14:47
@joaodinissf
joaodinissf marked this pull request as ready for review November 28, 2026 14:59

This branch has not been deployed

No deployments
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