From cbe60bd53ddc7fe6c72ec8ddb0b1cffde0fcae9a Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 17 Mar 2017 12:28:22 +0100 Subject: [PATCH] Use the generated snapshot source map in `source-map-support` This will report the correct file and line numbers on stack traces instead of always showing `:absoluteLineNumber`. As a result, it will also fix the `notifications` package, which will be able again to identify which package threw an exception and to create an issue on its repository. --- script/lib/generate-startup-snapshot.js | 16 ++++++++++++---- script/package.json | 2 +- src/atom-environment.coffee | 12 +++++++++--- src/compile-cache.js | 13 +++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/script/lib/generate-startup-snapshot.js b/script/lib/generate-startup-snapshot.js index 0021cf1ab..b32336a8b 100644 --- a/script/lib/generate-startup-snapshot.js +++ b/script/lib/generate-startup-snapshot.js @@ -58,12 +58,12 @@ module.exports = function (packagedAppPath) { relativePath == path.join('..', 'node_modules', 'tmp', 'lib', 'tmp.js') ) } - }).then((snapshotScriptContent) => { - fs.writeFileSync(snapshotScriptPath, snapshotScriptContent) + }).then(({snapshotScript, sourceMap}) => { + fs.writeFileSync(snapshotScriptPath, snapshotScript) process.stdout.write('\n') console.log('Verifying if snapshot can be executed via `mksnapshot`') - vm.runInNewContext(snapshotScriptContent, undefined, {filename: snapshotScriptPath, displayErrors: true}) + vm.runInNewContext(snapshotScript, undefined, {filename: snapshotScriptPath, displayErrors: true}) const generatedStartupBlobPath = path.join(CONFIG.buildOutputPath, 'snapshot_blob.bin') console.log(`Generating startup blob at "${generatedStartupBlobPath}"`) @@ -72,15 +72,23 @@ module.exports = function (packagedAppPath) { [snapshotScriptPath, '--startup_blob', generatedStartupBlobPath] ) - let startupBlobDestinationPath + let startupBlobDestinationPath, startupBlobSourceMapDestinationPath if (process.platform === 'darwin') { startupBlobDestinationPath = `${packagedAppPath}/Contents/Frameworks/Electron Framework.framework/Resources/snapshot_blob.bin` + startupBlobSourceMapDestinationPath = path.join(packagedAppPath, 'Contents', 'Resources', 'snapshot_sourcemap.json') + } else if (process.platform === 'linux') { + startupBlobDestinationPath = path.join(packagedAppPath, 'snapshot_blob.bin') + startupBlobSourceMapDestinationPath = path.join(packagedAppPath, 'resources', 'snapshot_sourcemap.json') } else { startupBlobDestinationPath = path.join(packagedAppPath, 'snapshot_blob.bin') + startupBlobSourceMapDestinationPath = path.join(packagedAppPath, 'resources', 'snapshot_sourcemap.json') } console.log(`Moving generated startup blob into "${startupBlobDestinationPath}"`) fs.unlinkSync(startupBlobDestinationPath) fs.renameSync(generatedStartupBlobPath, startupBlobDestinationPath) + + console.log(`Moving generated startup blob sourcemap into "${startupBlobSourceMapDestinationPath}"`) + fs.writeFileSync(startupBlobSourceMapDestinationPath, sourceMap) }) } diff --git a/script/package.json b/script/package.json index 9441213ac..107415ea5 100644 --- a/script/package.json +++ b/script/package.json @@ -8,7 +8,7 @@ "csslint": "1.0.2", "donna": "1.0.13", "electron-chromedriver": "~1.3", - "electron-link": "0.0.18", + "electron-link": "0.0.19", "electron-mksnapshot": "~1.3", "electron-packager": "7.3.0", "electron-winstaller": "2.5.1", diff --git a/src/atom-environment.coffee b/src/atom-environment.coffee index 7594f5de9..65b47a74e 100644 --- a/src/atom-environment.coffee +++ b/src/atom-environment.coffee @@ -755,9 +755,15 @@ class AtomEnvironment extends Model @lastUncaughtError = Array::slice.call(arguments) [message, url, line, column, originalError] = @lastUncaughtError - {line, column} = mapSourcePosition({source: url, line, column}) + {line, column, source} = mapSourcePosition({source: url, line, column}) - eventObject = {message, url, line, column, originalError} + mappedURL = '' + if url is '' + mappedURL = source + else + mappedURL = url + + eventObject = {message, originalUrl: url, url: mappedURL, line, column, originalError} openDevTools = true eventObject.preventDefault = -> openDevTools = false @@ -767,7 +773,7 @@ class AtomEnvironment extends Model if openDevTools @openDevTools().then => @executeJavaScriptInDevTools('DevToolsAPI.showPanel("console")') - @emitter.emit 'did-throw-error', {message, url, line, column, originalError} + @emitter.emit 'did-throw-error', {message, originalURL: url, url: mappedURL, line, column, originalError} uninstallUncaughtErrorHandler: -> @window.onerror = @previousWindowErrorHandler diff --git a/src/compile-cache.js b/src/compile-cache.js index 9b1966fc8..36275c5ed 100644 --- a/src/compile-cache.js +++ b/src/compile-cache.js @@ -113,6 +113,7 @@ function writeCachedJavascript (relativeCachePath, code) { } var INLINE_SOURCE_MAP_REGEXP = /\/\/[#@]\s*sourceMappingURL=([^'"\n]+)\s*$/mg +let snapshotSourceMap = null exports.install = function (nodeRequire) { sourceMapSupport.install({ @@ -122,6 +123,18 @@ exports.install = function (nodeRequire) { // source-map-support module, but we've overridden it to read the javascript // code from our cache directory. retrieveSourceMap: function (filePath) { + if (filePath === '') { + if (snapshotSourceMap == null) { + const snapshotSourceMapContent = fs.readFileSync(path.join(process.resourcesPath, 'snapshot_sourcemap.json'), 'utf8') + snapshotSourceMap = JSON.parse(snapshotSourceMapContent) + } + + return { + map: snapshotSourceMap, + url: path.join(process.resourcesPath, 'app', 'static', 'index.js') + } + } + if (!cacheDirectory || !fs.isFileSync(filePath)) { return null }