Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t patch Error.prepareStackTrace if --enable-source-maps is used. #5403

Merged
merged 38 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
4d9b901
Fixes #5382
STRd6 Apr 1, 2022
0fb2951
build:browser
STRd6 Apr 1, 2022
bebcec0
Merge remote-tracking branch 'origin/main' into 5382
STRd6 Apr 3, 2022
9354f97
Basic tests and inline source maps when native source maps are enabled
STRd6 Apr 3, 2022
c942bd0
Added exemption for node v12, native source maps don't seem to be acc…
STRd6 Apr 3, 2022
264b009
Added guard for in browser
STRd6 Apr 3, 2022
9f65df3
Use --require rather than -r
STRd6 Apr 4, 2022
05a9502
PR Feedback
STRd6 Apr 4, 2022
6c1312b
Experimental big refactoring of source mappings
STRd6 Apr 4, 2022
3cbbf48
Better tracking anonymous files
STRd6 Apr 4, 2022
b71bad6
Comment cleanup
STRd6 Apr 4, 2022
dad126e
Restored NODE_OPTIONS environment variable check
STRd6 Apr 4, 2022
44234dd
Added test for NODE_OPTIONS
STRd6 Apr 4, 2022
946a82f
Node v12 ... my old nemesis
STRd6 Apr 4, 2022
fae524b
Simplified
STRd6 Apr 5, 2022
f971278
Added default options object
STRd6 Apr 5, 2022
46a3962
Ensuring relative path for `sourceRoot` ends with separator
STRd6 Apr 5, 2022
30489cf
Added test for source mapped stack traces when executing bin/coffee
STRd6 Apr 5, 2022
0410b11
Using static class methods for SourceMap cache
STRd6 Apr 5, 2022
af44a0b
Extend browser test timeout from 30 => 60 seconds
STRd6 Apr 5, 2022
f341739
More reliable stream capturing in tests
STRd6 Apr 5, 2022
b46da93
Report errors when running browser tests
STRd6 Apr 5, 2022
608f0d6
Merge branch 'main' into 5382
STRd6 Apr 5, 2022
40529cd
Removed test pattern grepping
STRd6 Apr 5, 2022
a29f6bf
Merge branch 'main' into 5382
STRd6 Apr 5, 2022
942ce2c
Object.create null instead of empty object for the map cache
STRd6 Apr 6, 2022
1049cc6
Single quote string style
STRd6 Apr 10, 2022
4015fc5
Fixed stack trace in browser docs
STRd6 Apr 11, 2022
8590d28
Removed new stacktrace.coffee file
STRd6 Apr 11, 2022
e6ab9f6
Don't display inline maps on doc examples
STRd6 Apr 11, 2022
58afee8
attachStackTrace -> patchStackTrace
STRd6 Apr 17, 2022
8e2f090
Apply suggestions from code review
STRd6 Apr 17, 2022
2467d7d
Coding style cleanup
STRd6 Apr 17, 2022
219f76a
Updated comment on registerCompiled export
STRd6 Apr 17, 2022
267d91c
doc:test
STRd6 Apr 17, 2022
d24c75b
Fixed test guard conditional
STRd6 Apr 17, 2022
0d64fcc
Slightly more robust version checking
STRd6 Apr 17, 2022
c56d9a2
Comment about spawn vs fork
STRd6 Apr 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cakefile
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ task 'build:browser', 'merge the built scripts into a single file for use in a b
return #{fs.readFileSync "./package.json"};
})();
"""
for name in ['helpers', 'rewriter', 'lexer', 'parser', 'scope', 'nodes', 'sourcemap', 'coffeescript', 'browser']
for name in ['helpers', 'rewriter', 'lexer', 'parser', 'scope', 'nodes', 'sourcemap', 'stacktrace', 'coffeescript', 'browser']
code += """
require['./#{name}'] = (function() {
var exports = {}, module = {exports: exports};
Expand Down
2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/v2/browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions docs/v2/index.html

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion documentation/site/docs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ window.gtag 'config', window.GA_TRACKING_ID

# Initialize the CoffeeScript docs interactions
$(document).ready ->
CoffeeScript.attachStackTrace()

# Format dates for the user’s locale, e.g. 'December 24, 2009' or '24 décembre 2009'
$('time').each (index, el) ->
date = el.dateTime or $(el).text()
Expand Down Expand Up @@ -105,7 +107,7 @@ $(document).ready ->
if window.localStorage?
window.localStorage.setItem 'tryCoffeeScriptCode', coffee
catch exception
output = CoffeeScript.compile coffee, bare: yes
output = CoffeeScript.compile coffee, bare: yes, inlineMap: true
lastCompilationElapsedTime = Math.max(200, Date.now() - lastCompilationStartTime)
catch exception
output = "#{exception}"
Expand Down
2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-legacy/coffeescript.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/coffeescript-browser-compiler-modern/coffeescript.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions lib/coffeescript/coffeescript.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 4 additions & 85 deletions lib/coffeescript/register.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions lib/coffeescript/stacktrace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/coffeescript.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ exports.helpers = helpers
{registerCompiled} = SourceMap
exports.registerCompiled = registerCompiled

exports.attachStackTrace = require('./stacktrace').attach

# Function that allows for btoa in both nodejs and the browser.
base64encode = (src) -> switch
when typeof Buffer is 'function'
Expand Down Expand Up @@ -60,7 +62,7 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
# Clone `options`, to avoid mutating the `options` object passed in.
options = Object.assign {}, options

generateSourceMap = options.sourceMap or options.inlineMap
generateSourceMap = options.sourceMap or options.inlineMap or not options.filename?
filename = options.filename or helpers.anonymousFileName()

checkShebangLine filename, code
Expand Down Expand Up @@ -162,7 +164,7 @@ exports.compile = compile = withPrettyErrors (code, options = {}) ->
if options.inlineMap
encoded = base64encode JSON.stringify v3SourceMap
sourceMapDataURI = "//# sourceMappingURL=data:application/json;base64,#{encoded}"
sourceURL = "//# sourceURL=#{options.filename ? 'coffeescript'}"
sourceURL = "//# sourceURL=#{filename}"
js = "#{js}\n#{sourceMapDataURI}\n#{sourceURL}"
Comment on lines +168 to 169
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, not as part of this PR but related: #5237


registerCompiled filename, code, map
Expand Down
76 changes: 4 additions & 72 deletions src/register.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,17 @@ child_process = require 'child_process'
helpers = require './helpers'
path = require 'path'

{ getSourceMap } = require "./sourcemap"
stacktrace = require './stacktrace'

# Check if Node's built-in source map stack trace transformations are enabled.
nodeSourceMapsSupportEnabled =
nodeSourceMapsSupportEnabled = process? and (
process.execArgv.includes('--enable-source-maps') or
process.env.NODE_OPTIONS?.includes('--enable-source-maps')
)

unless Error.prepareStackTrace or nodeSourceMapsSupportEnabled
cacheSourceMaps = true

# Based on http://v8.googlecode.com/svn/branches/bleeding_edge/src/messages.js
# Modified to handle sourceMap
formatSourcePosition = (frame, getSourceMapping) ->
filename = undefined
fileLocation = ''

if frame.isNative()
fileLocation = "native"
else
if frame.isEval()
filename = frame.getScriptNameOrSourceURL()
fileLocation = "#{frame.getEvalOrigin()}, " unless filename
else
filename = frame.getFileName()

filename or= "<anonymous>"

line = frame.getLineNumber()
column = frame.getColumnNumber()

# Check for a sourceMap position
source = getSourceMapping filename, line, column
fileLocation =
if source
"#{filename}:#{source[0]}:#{source[1]}"
else
"#{filename}:#{line}:#{column}"

functionName = frame.getFunctionName()
isConstructor = frame.isConstructor()
isMethodCall = not (frame.isToplevel() or isConstructor)

if isMethodCall
methodName = frame.getMethodName()
typeName = frame.getTypeName()

if functionName
tp = as = ''
if typeName and functionName.indexOf typeName
tp = "#{typeName}."
if methodName and functionName.indexOf(".#{methodName}") isnt functionName.length - methodName.length - 1
as = " [as #{methodName}]"

"#{tp}#{functionName}#{as} (#{fileLocation})"
else
"#{typeName}.#{methodName or '<anonymous>'} (#{fileLocation})"
else if isConstructor
"new #{functionName or '<anonymous>'} (#{fileLocation})"
else if functionName
"#{functionName} (#{fileLocation})"
else
fileLocation

# Based on [michaelficarra/CoffeeScriptRedux](http://goo.gl/ZTx1p)
# NodeJS / V8 have no support for transforming positions in stack traces using
# sourceMap, so we must monkey-patch Error to display CoffeeScript source
# positions.
Error.prepareStackTrace = (err, stack) ->
getSourceMapping = (filename, line, column) ->
sourceMap = getSourceMap filename, line, column

answer = sourceMap.sourceLocation [line - 1, column - 1] if sourceMap?
if answer? then [answer[0] + 1, answer[1] + 1] else null

frames = for frame in stack
break if frame.getFunction() is exports.run
" at #{formatSourcePosition frame, getSourceMapping}"

"#{err.toString()}\n#{frames.join '\n'}\n"
stacktrace.attach(CoffeeScript.run)

# Load and run a CoffeeScript file for Node, stripping any `BOM`s.
loadFile = (module, filename) ->
Expand Down
Loading