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

Added --source-root option for sourcemaps #1100

Closed
wants to merge 2 commits into from

Conversation

teabyii
Copy link

@teabyii teabyii commented Mar 30, 2018

No description provided.

@devongovett
Copy link
Member

Wait, why is this necessary? I believe we already inline the full source code into the source map so dev tools doesn't need to request it.

@Hammster
Copy link
Contributor

Hammster commented Apr 4, 2018

#1078 inlining is not enough in some usecases, and devtools is not the only consumer of sourcemaps. One example are VS code breakpoints in splitted codebases.

@devongovett
Copy link
Member

Seems like we can autogenerate a relative path to the source directory rather than needing an extra option. Is there ever a usecase to pass it in manually?

I think it would be something like path.relative(options.outDir, options.rootDir).

@Hammster
Copy link
Contributor

Hammster commented Apr 4, 2018

@devongovett indeed, i did not know about the rootDir option. Is it new ?

But i can also think of a usecase for setting the sourceRoot option manually to your network adress for when using remote debug for smartphones/TVs/web-devices. But this is more of a edgecase since normally your local browser should resolve the remote debugging information.

@devongovett
Copy link
Member

rootDir is automatically computed based on the input files.

@felixfbecker
Copy link

felixfbecker commented Feb 24, 2019

sourceRoot always being relative is not correct. Chrome devtools will resolve the sourceRoot + source URL relative to the URL of the bundled file that was requested. The source file usually is not hosted at this location. The contents may be included in the source map, but when you have two bundles that have the same file structure within their project, the resolved URIs can collide.

Example: We bundle Sourcegraph extensions with Parcel, and all extensions typically have an src/extension.ts as entrypoint. They are loaded from an endpoint like https://sourcegraph.com/static/extension/{extension id}.js The source for these will always get resolved to https://sourcegraph.com/static/src/extension.ts (for all extensions). This means that when you have multiple extensions loaded and you open devtools, all you see is dozens src/extension.ts with no way to know which is which extension.

The way to solve this if you only use source maps in local dev would be to supply a --source-root option with file:///path/to/extension/project/src/ so that each extension has an absolute URL that points to the actual location on disk.

We also want to publish source maps in production, so we would actually set it to our raw API https://github.com/raw/{owner}/{repo}/{commit}/src/ or https://sourcegraph.com/github.com/{owner}/{repo}@{commit}/-/raw/src/, so it doesn't depend on local folder structure.

Another use case for setting sourceRoot is when you want to publish with source maps for mapped stack traces (especially when tracking errors with e.g. Sentry), but not publish the source contents to protect your IP. You would need unique URLs for each file for the stack traces or you run into the same problem, and you want to jump from the stack trace to the actual file contents later, which depends on the revision you deployed so you want to encode that into the source root URL.

Please add a --source-root option.

@felixfbecker
Copy link

See the source map spec:

Resolving Sources

If the sources are not absolute URLs after prepending of the “sourceRoot”, the sources are resolved relative to the SourceMap (like resolving script src in a html document).

https://sourcemaps.info/spec.html

@felixfbecker
Copy link

felixfbecker commented Feb 24, 2019

Another thing is that sourceRoot should be set in a way so that it is truly the highest common ancestor of the source files, i.e. no element in the sources array should have to start with ../. This is because the sourceRoot gets prepended to sources (not resolved) and Chrome will keep any .. segments in the URL, resulting in a confusing tree in dev tools:

image

Note how the tree starts at .../raw/extension/src, then goes up one level .., then goes into src again. If sourceRoot was set to the highest common ancestor (.../raw/), and all sources were relative to that instead of starting with .., this tree would be as expected.

Spec reference:

Line 4: An optional source root, useful for relocating source files on a server or removing repeated values in the “sources” entry. This value is prepended to the individual entries in the “source” field.

@mischnic
Copy link
Member

mischnic commented Feb 27, 2019

@felixfbecker Please open a new issue with your comment, otherwise this will be forgotten.

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

Successfully merging this pull request may close these issues.

6 participants