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

Process source maps in main process; allow disabling of processing #2474

Closed
novemberborn opened this issue Apr 26, 2020 · 2 comments
Closed

Comments

@novemberborn
Copy link
Member

Source maps get confusing real fast. I'd like to handle them more explicitly, removing the installation of source-map-support.

This means that any processing of stack traces should assume they have not been corrected for source maps. We should avoid unnecessarily processing stack traces in the worker processes. That means delaying source extraction:

source: extractSource(stack, testFile),

Instead the main process should rewrite the stack trace. We should focus on the mini and verbose reporters (#2217 notwithstanding):

return stackUtils.clean(stack)
.trim()
.split('\n')
.map(line => line.trim())
.filter(line => line !== '');

I'm really not too bothered about the stack traces that end up in the TAP output.

Line number selection attempts to find the original position of a test declaration. This does rely on a stack trace underneath. We should still convert using source maps, like we do now… though the current implementation probably breaks in certain conditions:

// FIXME: This assumes the callSite hasn't already been adjusted. It's likely
// that if `source-map-support/register` has been loaded, this would result
// in the wrong location.
const sourceCallSite = sourceMapSupport.wrapCallSite(callSite);
const start = {
line: sourceCallSite.getLineNumber(),
column: sourceCallSite.getColumnNumber() - 1 // Use 0-indexed columns.
};

It should be possible to ask our @ava/babel and @ava/typescript providers to reverse-map a call site to the original source location.

We could do the same in the snapshot manager:

ava/lib/snapshot-manager.js

Lines 399 to 405 in 1222ce9

const source = buffer.toString();
const converter = convertSourceMap.fromSource(source) || convertSourceMap.fromMapFileSource(source, testDir);
if (converter) {
const map = converter.toObject();
const firstSource = `${map.sourceRoot || ''}${map.sources[0]}`;
return path.resolve(testDir, firstSource);
}

That just leaves us with pre-compiled files that are not loaded through our providers, and files loaded with things like ts-node/register. Hopefully, those tools either already rewrite the stack traces for us, or source-map-support/register can be required by the user.

But, we do need an option to "assume stack traces are correct" which disables any source mapping. Otherwise we may and up mapping call sites twice.

My apologies for this issue being a bit of a brain dump. We can hash out the specifics together! The nice thing of this approach is that AVA won't change how your code runs (with automatic stack trace rewriting…), so your tests will better reflect what happens in live code.

@kaelzhang
Copy link

Any updates?

SourceMap problems never resolved in ava which drives people crazy

@novemberborn novemberborn added this to the 4.0 milestone Jul 25, 2021
@novemberborn
Copy link
Member Author

We'll switch to the Node.js built-in source map handling, see #2763.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants