diff --git a/docs/remote-debugging-headless-mps.md b/docs/remote-debugging-headless-mps.md new file mode 100644 index 0000000..f20ce83 --- /dev/null +++ b/docs/remote-debugging-headless-mps.md @@ -0,0 +1,145 @@ +# Remote debugging of headless MPS running under Ant + +**Question this answers:** can a Java remote debugger attach to MPS when a build runs headless through the Ant MPS tasks +(`ant generate`), and where must the JDWP agent be placed? + +**Short answer:** yes. The Ant MPS tasks (``, ``, ``, ``, `launchtests`) are subclasses +of `jetbrains.mps.build.ant.MpsLoadTask`, which by default **forks a dedicated worker JVM** per task and writes the +task's nested `` at the head of that forked JVM's command line. A standard JDWP `-agentlib` placed in +`` therefore opens a listening socket on the worker JVM; with `suspend=y` the worker blocks before its `main` +starts, so a debugger attaching from an IDE catches startup and every later step. When a script sets `fork="false"` +instead, the worker runs inside the Ant JVM, `` is rejected, and the JDWP agent must be placed on the Ant +process itself (e.g. `ANT_OPTS`). + +Verified against these sources: + +- **MPS 2024.1.2** (checkout): `core/tool/ant`, `core/tool/builder`, + `plugins/mps-build/languages/build.mps/generator/template/.../main@generator.mps` +- **JetBrains/MPS `master`** (as of September 2026): same `core/tool/ant/source_gen` paths; the fork semantics and the + placement of `` are unchanged. + +The tasks ship in the `ant-mps` library, `lib/ant/lib/ant-mps.jar` in the MPS distribution. The distribution's own +`build.xml` loads them as `taskdef resource="jetbrains/mps/build/ant/antlib.xml"` +(`core/tool/ant/resources/jetbrains/mps/build/ant/antlib.xml`). + +## How the Ant tasks run MPS + +`antlib.xml` maps these tasks to their classes: + +| Ant element | Class (`jetbrains.mps.build.ant...`) | +| ------------- | ---------------------------------------------------------------------------------- | +| `` | `.generation.GenerateTask` (worker `...make.GeneratorWorker`) | +| `` | `.MakeTask` | +| `` | `.generation.GenTestTask` | +| `` | `.generation.MpsRunnerTask` | +| `launchtests` | `.junit.LaunchTestTask` | +| `` | `.JvmArgs` (nestable ``; deprecated single `` is `.Arg`) | + +All extend `org.apache.tools.ant.Task` through `MpsLoadTask`, whose execution model is: + +```java +// MpsLoadTask, 2024.1.2 (core/tool/ant/source_gen/jetbrains/mps/build/ant/MpsLoadTask.java) +private boolean myFork = true; // default: fork +setFork(boolean) // opt-out +addConfiguredJvmArgs(JvmArgs) // nested ; throws BuildException when !myFork + +if (myFork) { + // command line, in order: + // java + // <- the nested + // -classpath (@argfile on newer master) + // [--add-opens=... when open-packages] [-Djna.boot.library.path=...] + //