Repository: microsoft/playwright
File: packages/playwright-core/src/client/tracing.ts (TypeScript)

Code before:
  async start(options: { name?: string, title?: string, snapshots?: boolean, screenshots?: boolean, sources?: boolean, _live?: boolean } = {}) {
    await this._wrapApiCall(async () => {
      this._includeSources = !!options.sources;
      this._isLive = !!options._live;
      await this._channel.tracingStart({
        name: options.name,
        snapshots: options.snapshots,
        screenshots: options.screenshots,
        live: options._live,
      });
      const { traceName } = await this._channel.tracingStartChunk({ name: options.name, title: options.title });
      await this._startCollectingStacks(traceName, this._isLive);
    });
  }

  async startChunk(options: { name?: string, title?: string } = {}) {
    await this._wrapApiCall(async () => {
      const { traceName } = await this._channel.tracingStartChunk(options);
      await this._startCollectingStacks(traceName, this._isLive);
    });
  }

  async group(name: string, options: { location?: { file: string, line?: number, column?: number } } = {}) {
    await this._wrapApiCall(async zone => {
      if (options.location)
        zone.frames.push({ file: options.location.file, line: options.location.line ?? 0, column: options.location.column ?? 0 });
      await this._channel.tracingGroup({ name, location: options.location });
    });
  }

  async groupEnd() {
    await this._channel.tracingGroupEnd();
  }

  private async _startCollectingStacks(traceName: string, live: boolean) {
    if (!this._isTracing) {
      this._isTracing = true;
      this._connection.setIsTracing(true);
    }
    const result = await this._connection.localUtils()?.tracingStarted({ tracesDir: this._tracesDir, traceName, live });
    this._stacksId = result?.stacksId;
  }

  async stopChunk(options: { path?: string } = {}) {
    await this._wrapApiCall(async () => {
      await this._doStopChunk(options.path);
    });
  }

  async stop(options: { path?: string } = {}) {
    await this._wrapApiCall(async () => {

Review comment (suggestion):
This looks scary, I hope we don't do anything like this in other places. If we want to fix it, we should pass the overriding location to the _wrapApiCall and use it in the API zone.

Code after:

  async start(options: { name?: string, title?: string, snapshots?: boolean, screenshots?: boolean, sources?: boolean, _live?: boolean } = {}) {
    await this._wrapApiCall(async () => {
      this._includeSources = !!options.sources;
      this._isLive = !!options._live;
      await this._channel.tracingStart({
        name: options.name,
        snapshots: options.snapshots,
        screenshots: options.screenshots,
        live: options._live,
      });
      const { traceName } = await this._channel.tracingStartChunk({ name: options.name, title: options.title });
      await this._startCollectingStacks(traceName, this._isLive);
    });
  }

  async startChunk(options: { name?: string, title?: string } = {}) {
    await this._wrapApiCall(async () => {
      const { traceName } = await this._channel.tracingStartChunk(options);
      await this._startCollectingStacks(traceName, this._isLive);
    });
  }

  async group(name: string, options: { location?: { file: string, line?: number, column?: number } } = {}) {
    if (options.location)
      this._additionalSources.add(options.location.file);
    await this._channel.tracingGroup({ name, location: options.location });
  }

  async groupEnd() {
    await this._channel.tracingGroupEnd();
  }

  private async _startCollectingStacks(traceName: string, live: boolean) {
    if (!this._isTracing) {
      this._isTracing = true;
      this._connection.setIsTracing(true);
    }
    const result = await this._connection.localUtils()?.tracingStarted({ tracesDir: this._tracesDir, traceName, live });
    this._stacksId = result?.stacksId;
  }

  async stopChunk(options: { path?: string } = {}) {
    await this._wrapApiCall(async () => {
      await this._doStopChunk(options.path);
    });
  }

  async stop(options: { path?: string } = {}) {
    await this._wrapApiCall(async () => {
      await this._doStopChunk(options.path);