microsoft/playwright · error · Error

Cannot close a directory

Error message

Cannot close a directory

What it means

Symmetric with the write guard: WritableStreamDispatcher.close() throws when streamOrDirectory is a directory string, because there is no underlying fs.WriteStream to end(). The close path exists to flush a real stream; a directory marker has nothing to flush.

Source

Thrown at packages/playwright-core/src/server/dispatchers/writableStreamDispatcher.ts:60

  }

  async write(params: channels.WritableStreamWriteParams, progress: Progress): Promise<channels.WritableStreamWriteResult> {
    if (typeof this._object.streamOrDirectory === 'string')
      throw new Error('Cannot write to a directory');
    const stream = this._object.streamOrDirectory;
    await progress.race(new Promise<void>((fulfill, reject) => {
      stream.write(params.binary, error => {
        if (error)
          reject(error);
        else
          fulfill();
      });
    }));
  }

  async close(params: channels.WritableStreamCloseParams, progress: Progress): Promise<void> {
    if (typeof this._object.streamOrDirectory === 'string')
      throw new Error('Cannot close a directory');
    const stream = this._object.streamOrDirectory;
    await progress.race(new Promise<void>(fulfill => stream.end(fulfill)));
    if (this._object.lastModifiedMs)
      await progress.race(fs.promises.utimes(this.path(), new Date(this._object.lastModifiedMs), new Date(this._object.lastModifiedMs)));
  }

  path(): string {
    if (typeof this._object.streamOrDirectory === 'string')
      return this._object.streamOrDirectory;
    return this._object.streamOrDirectory.path as string;
  }

  override _onDispose() {
    if (typeof this._object.streamOrDirectory !== 'string')
      this._object.streamOrDirectory.destroy();
  }
}

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. Ensure the writable stream wraps an actual file (fs.WriteStream), not a directory string.
  2. Use a full file path for save targets so a real stream is created.
  3. Avoid calling close on directory-marker dispatchers.

Example fix

// before — directory path produces a directory marker
const stream = new WritableStreamDispatcher(scope, '/tmp/out/');
await stream.close(); // throws
// after
const stream = new WritableStreamDispatcher(scope, fs.createWriteStream('/tmp/out/file.bin'));
await stream.close();
Defensive patterns

Strategy: validation

Validate before calling

// Only close a stream that wraps an actual fs.WriteStream.
if (typeof streamOrDirectory !== 'string') {
  await stream.close();
}

Type guard

// Distinguish a writable stream from a directory marker.
function isWriteStream(s: fs.WriteStream | string): s is fs.WriteStream {
  return typeof s !== 'string';
}

Prevention

When it happens

Trigger: Calling close() on a WritableStreamDispatcher that was constructed with a directory path rather than a write stream; the directory branch is used internally and closing it is invalid.

Common situations: See trigger scenarios.

Related errors


AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12). Data as JSON: /api/errors/062c17d925718980. Report an issue: GitHub.