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

feat: apply pathMapping when loading sourcemaps #1241

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions src/adapter/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ export class SourceContainer {
contentHash?: string,
): Promise<Source> {
const absolutePath = await this.sourcePathResolver.urlToAbsolutePath({ url });

this.logger.verbose(LogTag.RuntimeSourceCreate, 'Creating source from url', {
inputUrl: url,
absolutePath,
Expand Down
17 changes: 16 additions & 1 deletion src/common/sourceMaps/sourceMapFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IRootDapApi } from '../../dap/connection';
import { sourceMapParseFailed } from '../../dap/errors';
import { MapUsingProjection } from '../datastructure/mapUsingProjection';
import { IDisposable } from '../disposable';
import { ILogger, LogTag } from '../logging';
import { ISourcePathResolver } from '../sourcePathResolver';
import { fileUrlToAbsolutePath } from '../urlUtils';
import { ISourceMapMetadata, SourceMap } from './sourceMap';
Expand Down Expand Up @@ -49,13 +50,16 @@ export class SourceMapFactory implements ISourceMapFactory {
@inject(ISourcePathResolver) private readonly pathResolve: ISourcePathResolver,
@inject(IResourceProvider) private readonly resourceProvider: IResourceProvider,
@inject(IRootDapApi) protected readonly dap: Dap.Api,
@inject(ILogger) private readonly logger: ILogger,
) {}

/**
* @inheritdoc
*/
public async load(metadata: ISourceMapMetadata): Promise<SourceMap> {
const basic = await this.parseSourceMap(metadata.sourceMapUrl);
const basic =
(await this.parsePathMappedSourceMap(metadata.sourceMapUrl)) ||
(await this.parseSourceMap(metadata.sourceMapUrl));

// The source-map library is destructive with its sources parsing. If the
// source root is '/', it'll "helpfully" resolve a source like `../foo.ts`
Expand All @@ -80,6 +84,17 @@ export class SourceMapFactory implements ISourceMapFactory {
);
}

public async parsePathMappedSourceMap(url: string) {
const localSourceMapUrl = await this.pathResolve.urlToAbsolutePath({ url });
if (!localSourceMapUrl) return;

try {
return this.parseSourceMap(localSourceMapUrl);
} catch (error) {
this.logger.info(LogTag.SourceMapParsing, 'Parsing path mapped source map failed.', error);
}
}

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
allThreadsStopped : false
description : Paused on breakpoint
reason : breakpoint
threadId : <number>
}
foo @ ${workspaceFolder}/web/tmp/app.ts:2:3
<anonymous> @ ${workspaceFolder}/web/tmp/app.ts:5:1
36 changes: 36 additions & 0 deletions src/test/breakpoints/breakpointsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ describe('breakpoints', () => {
await waitForPause(p);
p.assertLog();
});

itIntegrates("source map that's path mapped", async ({ r }) => {
const cwd = r.workspacePath('web/tmp');

after(() =>
del([`${forceForwardSlashes(cwd)}/**`], {
force: true /* delete outside cwd */,
}),
);

createFileTree(cwd, {
'app.ts': await readfile(r.workspacePath('web/pathMapped/app.ts')),
'app.js': (await readfile(r.workspacePath('web/pathMapped/app.js'))).replace(
'app.js.map',
'mappedPath/app.js.map',
),
'index.html': await readfile(r.workspacePath('web/pathMapped/index.html')),
mappedDir: {
'app.js.map': await readfile(r.workspacePath('web/pathMapped/app.js.map')),
},
});

const p = await r.launchUrl('tmp/index.html', {
pathMapping: {
'/mappedPath/': '${workspaceFolder}/web/tmp/mappedDir/',
},
});
const source: Dap.Source = {
path: p.workspacePath('web/tmp/app.ts'),
};

await p.dap.setBreakpoints({ source, breakpoints: [{ line: 2 }] });
p.load();
await waitForPause(p);
p.assertLog();
});
});

describe('launched', () => {
Expand Down
5 changes: 5 additions & 0 deletions testWorkspace/web/pathMapped/app.js

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

1 change: 1 addition & 0 deletions testWorkspace/web/pathMapped/app.js.map

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

5 changes: 5 additions & 0 deletions testWorkspace/web/pathMapped/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo() {
void(0); // break here
}

foo();
1 change: 1 addition & 0 deletions testWorkspace/web/pathMapped/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="app.js"></script>