0

I want to compile code (pawn language), pipe all the compiler output, and then match all diagnostics using regex.

The thing is, for some reason, the compiler sometimes crashes with this error:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

which seems to be a common thing in C++ programs, after googling about it.

So if I do this:

child_process.execFile('./amxxpc', ['source.sma'], (error, stdout, stderr) => {
  ...
})

stdout ends up being an empty string, stderr is the std::bad_alloc error I pasted above, and error looks like this:

    at ChildProcess.exithandler (node:child_process:419:12)
    at ChildProcess.emit (node:events:513:28)
    at maybeClose (node:internal/child_process:1091:16)
    at ChildProcess._handle.onexit (node:internal/child_process:302:5) {
  code: null,
  killed: false,
  signal: 'SIGABRT',
  cmd: './amxxpc source.sma'
}

So I don't get why stdout is an empty string. If I run the exact same command in a terminal, and I ignore stderr, I get some diagnostics in stdout, which I want to be able to read when spawning it with child_process

> ./amxxpc source.sma 2>/dev/null
AMX Mod X Compiler 1.9.0.5294
Copyright (c) 1997-2006 ITB CompuPhase
Copyright (c) 2004-2013 AMX Mod X Team

source.sma(2) : error 029: invalid expression, assumed zero
source.sma(2 -- 4) : warning 215: expression has no effect
source.sma(4) : warning 217: loose indentation
source.sma(4) : error 029: invalid expression, assumed zero
source.sma(4) : error 017: undefined symbol "plugin_init"
source.sma(4) : fatal error 107: too many error messages on one line

Compilation aborted.
4 Errors.

just in case, this is the pawn code:

foo() {]

public plugin_init() {
    foo();
}
Mis
  • 23
  • 4

1 Answers1

0

So, apparently, when the compiler is executed in a real terminal, TTY, it flushed the stdout buffer on every line, whereas if its output is being piped, it doesn't flush "as often" as if it was a real TTY. It gets aborted before flushing, and the stdout buffer is lost.

So as a solution, I'm using the node-pty package to emulate a TTY terminal, so the program flushes to stdout before getting aborted. Now I can get the compiler diagnostics, even if it gets aborted shortly after that.

Mis
  • 23
  • 4