Skip to content

Bazel build file generation causes cross-target source dependency issues #35

@yume190

Description

@yume190

Background

We have a .xcodeproj project with multiple targets, such as:

  • Foo
  • Bar

The source structure is:

Foo/
  Foo.swift

Bar/
  Bar.swift

Other/
  Other.swift

Previous Approach

The previous generation strategy creates BUILD files inside each directory:

Foo/
  Foo.swift
  BUILD

Bar/
  Bar.swift
  BUILD

Other/
  Other.swift

BUILD
Module.bazel

When Foo depends on files outside its own directory:

Foo -> Bar/Bar.swift
Foo -> Other/Other.swift

The Foo/BUILD becomes:

swift_library(
    name = "Foo",
    srcs = [
        "Foo.swift",
        "//Bar:Bar.swift",
        "//:Other/Other.swift",
    ],
)

This requires additional configuration in other BUILD files:

# Bar/BUILD
exports_files(["Bar.swift"])
# root BUILD
exports_files(["Other/Other.swift"])

Problem

This approach introduces several issues:

  • Foo's dependency forces changes in other BUILD files
  • Tight coupling between targets
  • Dependencies are scattered across multiple BUILD files
  • High maintenance cost
  • Poor scalability as the project grows

Proposed Approach

Generate a separate build output workspace:

$OUTPUT/
  BUILD
  Module.bazel

  Foo/
    BUILD
    Foo/Foo.swift        (link)
    Bar/Bar.swift        (link)
    Other/Other.swift    (link)

Corresponding $OUTPUT/Foo/BUILD:

swift_library(
    name = "Foo",
    srcs = [
        "Foo/Foo.swift",
        "Bar/Bar.swift",
        "Other/Other.swift",
    ],
)

Improvements

  • Each target has a self-contained build context
  • No need to modify upstream BUILD files
  • Eliminates the need for exports_files
  • Reduces cross-package dependency complexity
  • Decouples build configuration from source layout

Core Idea

Shift from cross-package file-level dependencies
to a self-contained, per-target source aggregation model

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions