From 9aaee33b51d413408e78cb93d7a5cf0cef5e2f28 Mon Sep 17 00:00:00 2001 From: legendecas Date: Tue, 21 Apr 2020 01:09:50 +0800 Subject: [PATCH] Properly populate cached js filename and fix misaligned columns (#528) Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- src/index.js | 11 +++++++---- test/cli.js | 4 ++++ test/integration/stack-trace.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/integration/stack-trace.js diff --git a/src/index.js b/src/index.js index 36306867..4422164d 100644 --- a/src/index.js +++ b/src/index.js @@ -394,13 +394,16 @@ module.exports = ( assets[filename + '.map'] = { source: JSON.stringify(map), permissions: defaultPermissions }; map = undefined; } + const columnOffset = -'(function (exports, require, module, __filename, __dirname) { '.length; code = `const { readFileSync, writeFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');\n` + - `const source = readFileSync(__dirname + '/${filename}.cache${ext}', 'utf-8');\n` + - `const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(__dirname + '/${filename}.cache');\n` + - `const script = new Script(wrap(source), cachedData ? { cachedData } : {});\n` + + `const basename = __dirname + '/${filename}';\n` + + `const source = readFileSync(basename + '.cache${ext}', 'utf-8');\n` + + `const cachedData = !process.pkg && require('process').platform !== 'win32' && readFileSync(basename + '.cache');\n` + + `const scriptOpts = { filename: basename + '.cache${ext}', columnOffset: ${columnOffset} }\n` + + `const script = new Script(wrap(source), cachedData ? Object.assign({ cachedData }, scriptOpts) : scriptOpts);\n` + `(script.runInThisContext())(exports, require, module, __filename, __dirname);\n` + - `if (cachedData) process.on('exit', () => { try { writeFileSync(__dirname + '/${filename}.cache', script.createCachedData()); } catch(e) {} });\n`; + `if (cachedData) process.on('exit', () => { try { writeFileSync(basename + '.cache', script.createCachedData()); } catch(e) {} });\n`; } if (sourceMap && sourceMapRegister) { diff --git a/test/cli.js b/test/cli.js index ebfe58ee..7ab9de41 100644 --- a/test/cli.js +++ b/test/cli.js @@ -13,6 +13,10 @@ args: ["run", "--v8-cache", "test/integration/test.ts"], expect: { code: 0 } }, + { + args: ["run", "--v8-cache", "test/integration/stack-trace.js"], + expect: { code: 0 } + }, { args: ["run", "test/fixtures/error.js"], expect: { code: 1 } diff --git a/test/integration/stack-trace.js b/test/integration/stack-trace.js new file mode 100644 index 00000000..073fe806 --- /dev/null +++ b/test/integration/stack-trace.js @@ -0,0 +1,29 @@ +const assert = require('assert'); +const childProcess = require('child_process'); +const path = require('path'); + +(function main() { + if (process.env.CHILD !== 'stack-trace') { + const cp = childProcess.fork(__filename, [], { + stdio: 'pipe', + env: Object.assign({}, process.env, { + CHILD: 'stack-trace', + }) + }); + let stdoutBuffers = [] + cp.stdout.on('data', data => stdoutBuffers.push(data)); + + cp.on('exit', (code) => { + const stdout = Buffer.concat(stdoutBuffers).toString('utf8') + if (code !== 0) { + process.stderr.write(stdout); + return process.exit(1); + } + assert(stdout.match(/evalmachine./) == null); + assert(stdout.match(path.basename(__filename)) != null); + }); + return; + } + + console.log(new Error('foo')); +})();