vm: Error.prepareStackTrace behaves inconsistently across versions #21270
Description
- Version: 8.11.2
- Platform: Linux 4.16.4-1-ARCH
- Subsystem: vm
In Node 6.x (tested on v6.14.2), the following results in the prepareStackTrace
defined inside the context being used:
const { runInNewContext } = require('vm');
console.log(runInNewContext(`() => {
Error.prepareStackTrace = () => 'hello';
return new Error();
}`)().stack);
The stack is logged as hello
.
In Node 8.x (tested with 8.11.2), it will just output a normal stack, ignoring the prepareStackTrace
defined in the context.
It seems that in Node 8, the prepareStackTrace
from the currently running context (i.e. the one where .stack
is called) is used, whereas in Node 6, the one where the Error was instantiated is used.
Note that this only happens when the Error is passed along, and not when it's a thrown exception. I suspect that's because when it's thrown, DecorateErrorStack
is run, which gets the stack before it's passed to the main context:
Lines 735 to 747 in faf417b
Note also that when the stack is retrieved inside the new context, the in-context prepareStackTrace
is always used, regardless of version.