Skip to content

chore: make sourcePathResolver function per-thread #1

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

Merged
merged 1 commit into from
Jul 28, 2019
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
30 changes: 17 additions & 13 deletions src/adapter/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import { SourcePathResolver, Location, SourceContainer, Source } from './sources';
import { Location, SourceContainer, Source } from './sources';
import Dap from '../dap/api';
import Cdp from '../cdp/api';
import { Thread, ThreadManager, Script } from './threads';
Expand Down Expand Up @@ -71,13 +71,8 @@ export class Breakpoint {
}
}, undefined, this._disposables);

const source = this._manager._sourceContainer.source(this._source);
const url = source
? source.url() :
(this._source.path ? this._manager._sourcePathResolver.absolutePathToUrl(this._source.path) : undefined);
const promises: Promise<void>[] = [];

if (url) {
{
// For breakpoints set before launch, we don't know whether they are in a compiled or
// a source map source. To make them work, we always set by url to not miss compiled.
//
Expand All @@ -86,15 +81,17 @@ export class Breakpoint {
const lineNumber = this._lineNumber - 1;
const columnNumber = this._columnNumber - 1;
promises.push(...threadManager.threads().map(thread => {
return this._setByUrl(thread, url, lineNumber, columnNumber);
return this._setByPath(thread, lineNumber, columnNumber);
}));
threadManager.onThreadAdded(thread => {
this._setByUrl(thread, url, lineNumber, columnNumber);
this._setByPath(thread, lineNumber, columnNumber);
}, undefined, this._disposables);
}

const source = this._manager._sourceContainer.source(this._source);
const url = source ? source.url() : '';
const locations = this._manager._sourceContainer.currentSiblingLocations({
url: url || '',
url,
lineNumber: this._lineNumber,
columnNumber: this._columnNumber,
source
Expand Down Expand Up @@ -173,6 +170,15 @@ export class Breakpoint {
await Promise.all(promises);
}

async _setByPath(thread: Thread, lineNumber: number, columnNumber: number): Promise<void> {
const source = this._manager._sourceContainer.source(this._source);
const url = source ? source.url() :
(this._source.path ? thread.sourcePathResolver.absolutePathToUrl(this._source.path) : undefined);
if (!url)
return
await this._setByUrl(thread, url, lineNumber, columnNumber);
}

async _setByUrl(thread: Thread, url: string, lineNumber: number, columnNumber: number): Promise<void> {
const activeSetter = (async () => {
const result = await thread.cdp().Debugger.setBreakpointByUrl({
Expand Down Expand Up @@ -230,15 +236,13 @@ export class BreakpointManager {
private _byRef: Map<number, Breakpoint[]> = new Map();

_dap: Dap.Api;
_sourcePathResolver: SourcePathResolver;
_sourceContainer: SourceContainer;
_threadManager: ThreadManager;
_disposables: Disposable[] = [];
_perThread = new Map<string, Map<Cdp.Debugger.BreakpointId, Breakpoint>>();

constructor(dap: Dap.Api, sourcePathResolver: SourcePathResolver, sourceContainer: SourceContainer, threadManager: ThreadManager) {
constructor(dap: Dap.Api, sourceContainer: SourceContainer, threadManager: ThreadManager) {
this._dap = dap;
this._sourcePathResolver = sourcePathResolver;
this._sourceContainer = sourceContainer;
this._threadManager = threadManager;

Expand Down
10 changes: 4 additions & 6 deletions src/adapter/debugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Dap from '../dap/api';
import * as sourceUtils from '../utils/sourceUtils';
import { BreakpointManager, generateBreakpointId } from './breakpoints';
import * as errors from './errors';
import { Location, SourceContainer, SourcePathResolver } from './sources';
import { Location, SourceContainer } from './sources';
import { DummyThreadAdapter, ThreadAdapter } from './threadAdapter';
import { ExecutionContext, PauseOnExceptionsState, Thread, ThreadManager, ThreadManagerDelegate } from './threads';
import { VariableStore } from './variables';
Expand All @@ -23,7 +23,6 @@ export type SetBreakpointRequest = {
};

export interface DebugAdapterDelegate extends ThreadManagerDelegate {
sourcePathResolverFactory: () => SourcePathResolver;
adapterDisposed: () => void;
}

Expand Down Expand Up @@ -190,10 +189,9 @@ export class DebugAdapter {

async launch(delegate: DebugAdapterDelegate): Promise<void> {
this._delegate = delegate;
const sourcePathResolver = delegate.sourcePathResolverFactory();
this._sourceContainer = new SourceContainer(this._dap, sourcePathResolver);
this._threadManager = new ThreadManager(this._dap, sourcePathResolver, this._sourceContainer, delegate);
this._breakpointManager = new BreakpointManager(this._dap, sourcePathResolver, this._sourceContainer, this._threadManager);
this._sourceContainer = new SourceContainer(this._dap);
this._threadManager = new ThreadManager(this._dap, this._sourceContainer, delegate);
this._breakpointManager = new BreakpointManager(this._dap, this._sourceContainer, this._threadManager);

await this._threadManager.setPauseOnExceptionsState(this._pausedOnExceptionsState);
for (const request of this._setBreakpointRequests)
Expand Down
19 changes: 9 additions & 10 deletions src/adapter/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface SourcePathResolver {
//
export class Source {
private static _lastSourceReference = 0;

_sourcePathResolver: SourcePathResolver;
_sourceReference: number;
_url: string;
_name: string;
Expand Down Expand Up @@ -120,16 +120,17 @@ export class Source {

private _content?: Promise<string | undefined>;

constructor(container: SourceContainer, url: string, contentGetter: ContentGetter, sourceMapUrl?: string, inlineScriptOffset?: InlineScriptOffset, contentHash?: string) {
constructor(container: SourceContainer, sourcePathResolver: SourcePathResolver, url: string, contentGetter: ContentGetter, sourceMapUrl?: string, inlineScriptOffset?: InlineScriptOffset, contentHash?: string) {
this._sourceReference = ++Source._lastSourceReference;
this._sourcePathResolver = sourcePathResolver;
this._url = url;
this._contentGetter = contentGetter;
this._sourceMapUrl = sourceMapUrl;
this._inlineScriptOffset = inlineScriptOffset;
this._container = container;
this._fqname = this._fullyQualifiedName();
this._name = path.basename(this._fqname);
this._absolutePath = container._sourcePathResolver.urlToAbsolutePath(url);
this._absolutePath = sourcePathResolver.urlToAbsolutePath(url);

// Inline scripts will never match content of the html file. We skip the content check.
if (inlineScriptOffset)
Expand Down Expand Up @@ -246,7 +247,6 @@ export class Source {

export class SourceContainer {
private _dap: Dap.Api;
_sourcePathResolver: SourcePathResolver;
_brokenSourceMapReported = false;

private _sourceByReference: Map<number, Source> = new Map();
Expand All @@ -260,9 +260,8 @@ export class SourceContainer {

_fileContentOverrides = new Map<string, string>();

constructor(dap: Dap.Api, sourcePathResolver: SourcePathResolver) {
constructor(dap: Dap.Api) {
this._dap = dap;
this._sourcePathResolver = sourcePathResolver;
}

setSourceMapTimeouts(sourceMapTimeouts: SourceMapTimeouts) {
Expand Down Expand Up @@ -403,8 +402,8 @@ export class SourceContainer {
}
}

addSource(url: string, contentGetter: ContentGetter, sourceMapUrl?: string, inlineSourceRange?: InlineScriptOffset, contentHash?: string): Source {
const source = new Source(this, url, contentGetter, sourceMapUrl, inlineSourceRange, contentHash);
addSource(sourcePathResolver: SourcePathResolver, url: string, contentGetter: ContentGetter, sourceMapUrl?: string, inlineSourceRange?: InlineScriptOffset, contentHash?: string): Source {
const source = new Source(this, sourcePathResolver, url, contentGetter, sourceMapUrl, inlineSourceRange, contentHash);
this._addSource(source);
return source;
}
Expand Down Expand Up @@ -487,7 +486,7 @@ export class SourceContainer {
compiled._sourceMapSourceByUrl = new Map();
const addedSources: Source[] = [];
for (const url of map.sources) {
const sourceUrl = this._sourcePathResolver.rewriteSourceUrl(url);
const sourceUrl = compiled._sourcePathResolver.rewriteSourceUrl(url);
const baseUrl = sourceMapUrl.startsWith('data:') ? compiled.url() : sourceMapUrl;
const resolvedUrl = utils.completeUrl(baseUrl, sourceUrl) || sourceUrl;
const contentOrNull = map.sourceContentFor(url);
Expand All @@ -496,7 +495,7 @@ export class SourceContainer {
const isNew = !source;
if (!source) {
// Note: we can support recursive source maps here if we parse sourceMapUrl comment.
source = new Source(this, resolvedUrl, content !== undefined ? () => Promise.resolve(content) : () => utils.fetch(resolvedUrl));
source = new Source(this, compiled._sourcePathResolver, resolvedUrl, content !== undefined ? () => Promise.resolve(content) : () => utils.fetch(resolvedUrl));
source._compiledToSourceUrl = new Map();
}
source._compiledToSourceUrl!.set(compiled, url);
Expand Down
52 changes: 26 additions & 26 deletions src/adapter/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ export interface ExecutionContext {

export type Script = { scriptId: string, hash: string, source: Source, thread: Thread };

export interface ThreadConfiguration {
supportsCustomBreakpoints?: boolean;
defaultScriptOffset?: InlineScriptOffset;
}

export interface ThreadManagerDelegate {
copyToClipboard(text: string): void;
executionContextForest(): ExecutionContext[] | undefined;
canStopThread(thread: Thread): boolean;
stopThread(thread: Thread): void;
copyToClipboard: (text: string) => void;
}

export interface ThreadDelegate {
canStopThread(): boolean;
stopThread(): void;

supportsCustomBreakpoints?: boolean;
defaultScriptOffset?: InlineScriptOffset;
sourcePathResolver: SourcePathResolver;
}

export type ScriptWithSourceMapHandler = (script: Script, sources: Source[]) => Promise<void>;
Expand All @@ -76,17 +78,15 @@ export class ThreadManager {
readonly onThreadResumed = this._onThreadResumedEmitter.event;
readonly onExecutionContextsChanged = this._onExecutionContextsChangedEmitter.event;
readonly sourceContainer: SourceContainer;
_sourcePathResolver: SourcePathResolver;
_delegate: ThreadManagerDelegate;
_scriptWithSourceMapHandler?: ScriptWithSourceMapHandler;
_consoleIsDirty = false;

// url => (hash => Source)
private _scriptSources = new Map<string, Map<string, Source>>();

constructor(dap: Dap.Api, sourcePathResolver: SourcePathResolver, sourceContainer: SourceContainer, delegate: ThreadManagerDelegate) {
constructor(dap: Dap.Api, sourceContainer: SourceContainer, delegate: ThreadManagerDelegate) {
this._dap = dap;
this._sourcePathResolver = sourcePathResolver;
this._pauseOnExceptionsState = 'none';
this._customBreakpoints = new Set();
this.sourceContainer = sourceContainer;
Expand All @@ -97,8 +97,8 @@ export class ThreadManager {
return this._threads.values().next().value;
}

createThread(threadId: string, cdp: Cdp.Api, configuration: ThreadConfiguration): Thread {
return new Thread(this, threadId, cdp, this._dap, configuration);
createThread(threadId: string, cdp: Cdp.Api, delegate: ThreadDelegate): Thread {
return new Thread(this, threadId, cdp, this._dap, delegate);
}

setScriptSourceMapHandler(handler?: ScriptWithSourceMapHandler) {
Expand Down Expand Up @@ -208,27 +208,27 @@ export class Thread implements VariableStoreDelegate {
private _pausedVariables?: VariableStore;
private _pausedForSourceMapScriptId?: string;
private _scripts: Map<string, Script> = new Map();
private _supportsCustomBreakpoints: boolean;
private _defaultScriptOffset?: InlineScriptOffset;
private _delegate: ThreadDelegate;
private _executionContexts: Map<number, Cdp.Runtime.ExecutionContextDescription> = new Map();
readonly replVariables: VariableStore;
readonly manager: ThreadManager;
readonly sourceContainer: SourceContainer;
readonly sourcePathResolver: SourcePathResolver;
readonly threadLog = new ThreadLog();
private _eventListeners: eventUtils.Listener[] = [];
private _supportsSourceMapPause = false;
private _serializedOutput: Promise<void>;
_debuggerId?: Cdp.Runtime.UniqueDebuggerId;

constructor(manager: ThreadManager, threadId: string, cdp: Cdp.Api, dap: Dap.Api, configuration: ThreadConfiguration) {
constructor(manager: ThreadManager, threadId: string, cdp: Cdp.Api, dap: Dap.Api, delegate: ThreadDelegate) {
this.manager = manager;
this.sourceContainer = manager.sourceContainer;
this.sourcePathResolver = delegate.sourcePathResolver;
this._cdp = cdp;
this._dap = dap;
this._threadId = threadId;
this._name = '';
this._supportsCustomBreakpoints = configuration.supportsCustomBreakpoints || false;
this._defaultScriptOffset = configuration.defaultScriptOffset;
this._delegate = delegate;
this.replVariables = new VariableStore(this._cdp, this);
this.manager._addThread(this);
this._serializedOutput = Promise.resolve();
Expand Down Expand Up @@ -302,11 +302,11 @@ export class Thread implements VariableStoreDelegate {
}

canStop(): boolean {
return this.manager._delegate.canStopThread(this);
return this._delegate.canStopThread();
}

stop() {
this.manager._delegate.stopThread(this);
this._delegate.stopThread();
}

initialize() {
Expand Down Expand Up @@ -482,10 +482,10 @@ export class Thread implements VariableStoreDelegate {
const script = rawLocation.scriptId ? this._scripts.get(rawLocation.scriptId) : undefined;
let {lineNumber, columnNumber} = rawLocation;
columnNumber = columnNumber || 0;
if (this._defaultScriptOffset) {
lineNumber -= this._defaultScriptOffset.lineOffset;
if (this._delegate.defaultScriptOffset) {
lineNumber -= this._delegate.defaultScriptOffset.lineOffset;
if (!lineNumber)
columnNumber = Math.max(columnNumber - this._defaultScriptOffset.columnOffset, 0);
columnNumber = Math.max(columnNumber - this._delegate.defaultScriptOffset.columnOffset, 0);
}
// Note: cdp locations are 0-based, while ui locations are 1-based.
return this.sourceContainer.preferredLocation({
Expand All @@ -509,7 +509,7 @@ export class Thread implements VariableStoreDelegate {
async updateCustomBreakpoint(id: CustomBreakpointId, enabled: boolean): Promise<boolean> {
// Do not fail for custom breakpoints, to account for
// future changes in cdp vs stale breakpoints saved in the workspace.
if (!this._supportsCustomBreakpoints)
if (!this._delegate.supportsCustomBreakpoints)
return true;
const breakpoint = customBreakpoints().get(id);
if (!breakpoint)
Expand Down Expand Up @@ -703,7 +703,7 @@ export class Thread implements VariableStoreDelegate {
}

_onScriptParsed(event: Cdp.Debugger.ScriptParsedEvent) {
event.url = this.manager._sourcePathResolver.scriptUrlToUrl(event.url);
event.url = this.sourcePathResolver.scriptUrlToUrl(event.url);
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we don't want to dedupe scripts with different resolvers by url (see _getSourceForScript). Maybe that will never happen in practice though.


let source: Source | undefined;
if (event.url && event.hash)
Expand All @@ -727,7 +727,7 @@ export class Thread implements VariableStoreDelegate {
errors.reportToConsole(this._dap, `Could not load source map from ${event.sourceMapURL}`);
}

source = this.sourceContainer.addSource(event.url, contentGetter, resolvedSourceMapUrl, inlineSourceOffset, event.hash);
source = this.sourceContainer.addSource(this.sourcePathResolver, event.url, contentGetter, resolvedSourceMapUrl, inlineSourceOffset, event.hash);
this.manager._addSourceForScript(event.url, event.hash, source);
}

Expand Down
9 changes: 3 additions & 6 deletions src/chrome/chromeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as utils from '../utils/urlUtils';
import findChrome from './findChrome';
import * as launcher from './launcher';
import { Target, TargetManager } from './targets';
import { Thread } from '../adapter/threads';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -100,15 +99,13 @@ export class ChromeAdapter {
this._launchParams = params;

await this._debugAdapter.launch({
sourcePathResolverFactory: () => new ChromeSourcePathResolver(this._rootPath, params.url, params.webRoot),
executionContextForest: () => this._targetManager.executionContextForest(),
adapterDisposed: () => this._dispose(),
copyToClipboard: (text: string) => vscode.env.clipboard.writeText(text),
canStopThread: (thread: Thread) => this._targetManager.canStop(thread.threadId()),
stopThread: (thread: Thread) => this._targetManager.stop(thread.threadId())
executionContextForest: () => this.targetManager().executionContextForest()
});
this._debugAdapter[ChromeAdapter.symbol] = this;
this._targetManager = new TargetManager(this._connection, this._debugAdapter.threadManager());
const pathResolver = new ChromeSourcePathResolver(this._rootPath, params.url, params.webRoot);
this._targetManager = new TargetManager(this._connection, this._debugAdapter.threadManager(), pathResolver);
this._disposables.push(this._targetManager);

// Note: assuming first page is our main target breaks multiple debugging sessions
Expand Down
12 changes: 10 additions & 2 deletions src/chrome/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { URL } from 'url';
import { Thread, ThreadManager, ExecutionContext } from '../adapter/threads';
import { FrameModel, Frame } from './frames';
import { ServiceWorkerModel } from './serviceWorkers';
import { SourcePathResolver } from '../adapter/sources';

const debugTarget = debug('target');

Expand All @@ -23,15 +24,17 @@ export class TargetManager implements vscode.Disposable {
readonly frameModel = new FrameModel();
readonly serviceWorkerModel = new ServiceWorkerModel(this.frameModel);
_threadManager: ThreadManager;
_sourcePathResolver: SourcePathResolver;

private _onTargetAddedEmitter = new vscode.EventEmitter<Target>();
private _onTargetRemovedEmitter = new vscode.EventEmitter<Target>();
readonly onTargetAdded = this._onTargetAddedEmitter.event;
readonly onTargetRemoved = this._onTargetRemovedEmitter.event;

constructor(connection: CdpConnection, threadManager: ThreadManager) {
constructor(connection: CdpConnection, threadManager: ThreadManager, sourcePathResolver: SourcePathResolver) {
this._connection = connection;
this._threadManager = threadManager;
this._sourcePathResolver = sourcePathResolver;
this._browser = connection.browser();
this._browser.Target.on('targetInfoChanged', event => {
this._targetInfoChanged(event.targetInfo);
Expand Down Expand Up @@ -275,7 +278,12 @@ export class Target {
this._manager = targetManager;
this.parentTarget = parentTarget;
if (jsTypes.has(targetInfo.type))
this._thread = targetManager._threadManager.createThread(targetInfo.targetId, cdp, { supportsCustomBreakpoints: domDebuggerTypes.has(targetInfo.type) });
this._thread = targetManager._threadManager.createThread(targetInfo.targetId, cdp, {
canStopThread: () => targetManager.canStop(this._thread!.threadId()),
stopThread: () => targetManager.stop(this._thread!.threadId()),
supportsCustomBreakpoints: domDebuggerTypes.has(targetInfo.type),
sourcePathResolver: this._manager._sourcePathResolver
});
this._updateFromInfo(targetInfo);
this._ondispose = ondispose;
}
Expand Down
Loading