def METHOD_NAME(project_dir, env_names, debug=False):
    # pylint: disable=import-outside-toplevel
    from platformio import app
    from platformio.run.cli import cli as cmd_run
    args = ["--project-dir", project_dir, "--target", "__idedata"]
    if debug:
        args.extend(["--target", "__debug"])
    for name in env_names:
        args.extend(["-e", name])
    app.set_session_var("pause_telemetry", True)
    result = CliRunner().invoke(cmd_run, args)
    app.set_session_var("pause_telemetry", False)
    if result.exit_code != 0 and not isinstance(
        result.exception, exception.ReturnErrorCode
    ):
        raise result.exception
    if '"includes":' not in result.output:
        raise exception.UserSideException(result.output)
    return _get_cached_build_metadata(project_dir, env_names)