Source maps not supported in `eval` and `new Function`

Version

v18.0.0

Platform

Microsoft Windows NT 10.0.19044.0 x64

Subsystem

No response

What steps will reproduce the bug?

eval

node --enable-source-maps example.js where example.js the following file:

const comment = '//#';
const code = `// Generated by CoffeeScript 2.7.0
(function() {
  throw new Error('hello');

}).call(this);

${comment} sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2ltcGxlLmpzIiwic291cmNlUm9vdCI6IlxcIiwic291cmNlcyI6WyJzaW1wbGUuY29mZmVlIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTtFQUFBLE1BQU0sSUFBSSxLQUFKLENBQVUsT0FBVjtBQUFOIiwic291cmNlc0NvbnRlbnQiOlsidGhyb3cgbmV3IEVycm9yICdoZWxsbydcbiJdfQ==
${comment} sourceURL=simple.coffee
`;
eval(code);

new Function

node --enable-source-maps example.js where example.js the following file: (only the last line differs from the previous example)

const comment = '//#';
const code = `// Generated by CoffeeScript 2.7.0
(function() {
  throw new Error('hello');

}).call(this);

${comment} sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2ltcGxlLmpzIiwic291cmNlUm9vdCI6IlxcIiwic291cmNlcyI6WyJzaW1wbGUuY29mZmVlIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQTtFQUFBLE1BQU0sSUFBSSxLQUFKLENBQVUsT0FBVjtBQUFOIiwic291cmNlc0NvbnRlbnQiOlsidGhyb3cgbmV3IEVycm9yICdoZWxsbydcbiJdfQ==
${comment} sourceURL=simple.coffee
`;
new Function(code)();

Source

This example was generated from a one-line simple.coffeethrow new Error 'hello' — via coffee -M -c simple.coffee. I replaced //# with a string interpolation so the source map isn't applied to example.js itself.

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

eval

This produces the following stack trace:

Error: hello
    at Object.eval (simple.coffee:3:9)
    at eval (simple.coffee:5:4)
    at Object.<anonymous> (C:\...\example.js:11:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_ma
in:77:12)
    at node:internal/main/run_main_module:17:47

The line numbers for simple.coffee (lines 3 and 5) are incorrect (both should be line 1).

new Function

This produces the following stack trace:

Error: hello
    at eval (simple.coffee:5:9)
    at eval (simple.coffee:7:4)
    at Object.<anonymous> (C:\...\example.js:11:19)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_ma
in:77:12)
    at node:internal/main/run_main_module:17:47

The line numbers for simple.coffee (lines 5 and 7) are incorrect (both should be line 1), and different from the eval case.

What do you see instead?

I expect everything to refer to line 1 of simple.coffee, as that's the only line.

Running npm install coffeescript and coffee simple.coffee, where simple.coffee is the following file:

produces the correct line numbers (via a specialized mapping implemented by CoffeeScript, I believe):

Error: hello
    at Object.<anonymous> (C:\...\simple.coffee:1:7)
    at Object.<anonymous> (C:\...\simple.coffee:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.CoffeeScript.run (C:\...\node_modules\coffeescript\lib\coffeescript\index.js:67:23)
    at compileScript (C:\...\node_modules\coffeescript\lib\coffeescript\command.js:285:29)
    at compilePath (C:\...\node_modules\coffeescript\lib\coffeescript\command.js:237:14)
    at Object.exports.run (C:\...\node_modules\coffeescript\lib\coffeescript\command.js:158:20)
    at Object.<anonymous> (C:\...\node_modules\coffeescript\bin\coffee:22:45)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:827:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Additional information

The source map spec explicitly mentions code "being evaluated as a string with the eval() function or via new Function()" so I assume this is supposed to work.