def fetch_targets_from_bazelproject()

in bsp_server/scip_sync_util/scip_sync.py [0:0]


def fetch_targets_from_bazelproject(cwd: str) -> Tuple[set[str], set[str]]:
    print("Reading targets from .bazelproject file...")

    bazel_project_file = os.path.join(cwd, ".ijwb", ".bazelproject")
    if not os.path.exists(bazel_project_file):
        print(".bazelproject file not found")
        return set(), set()

    targets = set()
    excludes = set()
    try:
        sections = scip_utils.parse_bazelproject(bazel_project_file)
        if TARGETS in sections:
            for t in sections[TARGETS]:
                if _should_exclude_path(t):
                    # remove - from start
                    excludes.add(t[1:])
                else:
                    targets.add(t)

        derive_from_dirs = (
            sections.get(DERIVE_TARGETS_FROM_DIRECTORIES, ["false"])[0].lower()
            == "true"
        )

        if derive_from_dirs and DIRECTORIES in sections:
            non_excluded_dirs = []
            excluded_dirs = []
            for d in sections[DIRECTORIES]:
                if _should_exclude_path(d):
                    # remove - from start
                    excluded_dirs.append(d[1:])
                else:
                    non_excluded_dirs.append(d)

            print(f"Loading targets from directories: {non_excluded_dirs}")
            if non_excluded_dirs:
                targets = targets | set(
                    convert_directories_to_targets(non_excluded_dirs)
                )
            if excluded_dirs:
                excludes = excludes | set(convert_directories_to_targets(excluded_dirs))

        return targets, excludes

    except Exception as e:
        print(f"Error fetching targets from bazelproject: {e}")
        return set(), set()