Logs a message to the Pulumi CLI's debug channel, associating it with a resource
    and stream_id if provided.

    :param str msg: The message to send to the Pulumi CLI.
    :param Optional[Resource] resource: If provided, associate this message with the given resource in the Pulumi CLI.
    :param Optional[int] stream_id: If provided, associate this message with a stream of other messages.

def debug(msg: str, resource: Optional['Resource'] = None, stream_id: Optional[int] = None) -> None:
    """
    Logs a message to the Pulumi CLI's debug channel, associating it with a resource
    and stream_id if provided.

    :param str msg: The message to send to the Pulumi CLI.
    :param Optional[Resource] resource: If provided, associate this message with the given resource in the Pulumi CLI.
    :param Optional[int] stream_id: If provided, associate this message with a stream of other messages.
    """
    engine = get_engine()
    if engine is not None:
        _log(engine, engine_pb2.DEBUG, msg, resource, stream_id)
    else:
        print("debug: " + msg, file=sys.stderr)