Repository: peggyjs/peggy (js)

Issue:
request: output a line when compilation ends in failure in watch mode
Hi team,

I request Peggy in the watch mode (`peggy -w`) print out a line of text when compilation ends in failure.

Current `peggy -w` outputs a line when it succeeds in compilation but does not when it fails. In this case it is difficult for a tool that observes `peggy -w` output (in my case VS Code problem matcher) to judge when compilation ends.

__Background__

It looks current Peggy in the _watch_ mode works as follows:

* When file change is detected (and also at the first time the watch starts), print `"/full/path/to/file.peggy" changed...`
* When compilation is ended successfully, print `Wrote: "rel/path/to/file.js"`. When one or more errors are detected, instead print multi-lined error message like:

```
 --> rel/path/to/file.peggy:1:33
  |
1 | literal = match:"foo" rest:rest { return {match, rest}; 
  |                                 ^
```

However, error(s) can be multiple and thus this cannot be used as a sign of the end of compilation. In _watch_ mode, return code of the command line interface is unavailable and so, printed message is only the hint; it should contain the sign of start/end and success/failure.

__How the feature should work__

Just outputting some text at the end of error messages would be fine. Compared with the case of no error, the text such as `Failed in writing: "file.js"` might be OK.

__Use case(s) behind it__

I am now trying to integrate `peggy -w` task in VS Code build system for some extension authoring. To do so, I need to define a regular expression patterns for _Problem Matcher_ so that the problem matcher can detect whether error occurs during compilation, and (in watch mode) when compilation starts and ends, etc. See [VS Code | Tasks | Background / Watching Tasks](https://code.visualstudio.com/docs/debugtest/tasks#_background-watching-tasks). Currently I cannot create a nice regular expression pattern for `background.endPattern`.

I think this modification will make Peggy's watch mode more friendly with VS Code build system and probably some other editors/IDEs.

Additionally, if the error is expressed in a single line per error (as shown as ESLint example in the above link), it would be better for a VS Code problem matcher but it might be a topic that should be discussed in another issue.

Pull request:
fix 621
- **Test on node 24, not node 23.**
- **Write a message after failing a compile in watch mode.  Fixes #621.**


Patch:
diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index 81fabd8..b01f7df 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -12,7 +12,7 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [20, 22, 23]
+        node-version: [20, 22, 24]
         os: [ubuntu-latest, windows-latest, macos-latest]
 
     runs-on: ${{ matrix.os }}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8224fa1..238dcea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,10 @@ Released: TBD
 ### Bug fixes
 
 - Tweak TypeScript output from --dts to make error type more clear. [#623](https://github.com/peggyjs/peggy/pull/623)
+- Test on Node 24, but not Node 23.
+  [#624](https://github.com/peggyjs/peggy/pull/624)
+- Write a message after failing a compile in watch mode.
+  [#624](https://github.com/peggyjs/peggy/pull/624)
 
 ### Documentation
 
diff --git a/bin/peggy-cli.js b/bin/peggy-cli.js
index 2288be4..635478c 100644
--- a/bin/peggy-cli.js
+++ b/bin/peggy-cli.js
@@ -642,6 +642,7 @@ declare function ParseFunction<Options extends ParseOptions<StartRuleNames>>(
 
         if (this.lastError) {
           PeggyCLI.print(this.std.err, this.lastError);
+          PeggyCLI.print(this.std.err, `Failed writing: "${this.progOptions.outputFile}"`);
         } else if (!hasTest) {
           PeggyCLI.print(this.std.err, `Wrote: "${this.progOptions.outputFile}"`);
         }


Test patch:
diff --git a/test/cli/run.spec.ts b/test/cli/run.spec.ts
index c21f502..fb0b993 100644
--- a/test/cli/run.spec.ts
+++ b/test/cli/run.spec.ts
@@ -1465,9 +1465,9 @@ error: WARN(check): An expression may not consume any input and may always match
         args: ["-w", bad],
         exitCode: 0,
         onstderr(_s, cli) {
-          // This is brittle.  Stderr gets the "file added" message, then
-          // the error, then a newline.
-          if (++count === 4) {
+          // This is brittle.  Stderr gets the "file added" message, then the
+          // error, then a newline, then the failed message, then a newline.
+          if (++count === 6) {
             cli.stopWatching();
           }
         },