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

Relative sourceRoot can cause issues with file discovery #99

Open
j03m opened this issue Apr 29, 2020 · 2 comments
Open

Relative sourceRoot can cause issues with file discovery #99

j03m opened this issue Apr 29, 2020 · 2 comments

Comments

@j03m
Copy link
Contributor

j03m commented Apr 29, 2020

The following is a legal sourceMap. Note the condition that causes a problem, is the combination of a relative sourceRoot along with a sources member that contains a path that an additional level deep in the directory structure. In this case, subdir/foo.ts.

{"version":3,"sources":["subdir/foo.ts"],"names":[ ... ],"mappings":" ... ","file":"foo.js","sourceRoot":"../../../../../../../../rootdir"}

Give the discovery of a covered file in c8 that was [somepath]/rootdir/subdir/foo.js when we attempt to resolve the location of the source file we will hit this block of code: https://github.com/istanbuljs/v8-to-istanbul/blob/master/lib/v8-to-istanbul.js#L79 we will end up creating a path that is: [somepath]/rootdir/subdir/subdir/foo.ts which is invalid.

Taking a look at this for a PR.

@j03m
Copy link
Contributor Author

j03m commented Apr 29, 2020

I think the correct code here would be:

this.path = join(resolve(dirname(this.path), sourceRoot), sourcePath)

Where dirname(this.path) would get the directory for the src file in question. resolve would then resolve against the relative sourceRoot getting the absolute path to the source directory. From there the join would attach the source file and be teh right path to the source file.

I'll need to test this though to make sure I'm not just fixing this case.

@robpalme
Copy link

Agreed - it looks like the bug is due to sourceRoot being ignored if it is relative. So an equivalent fix would be:

const candidatePath = join(sourceRoot, sourcePath)

if (isAbsolute(candidatePath)) {
  this.path = candidatePath
} else {
-  this.path = resolve(dirname(this.path), sourcePath)
+  this.path = resolve(dirname(this.path), candidatePath)
}

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

No branches or pull requests

2 participants