Skip to content

Add exports-files-discouraged buildifier lint check - #1494

Open
alexeagle wants to merge 2 commits into
bazel-contrib:mainfrom
alexeagle:lint/exports-files-discouraged
Open

Add exports-files-discouraged buildifier lint check#1494
alexeagle wants to merge 2 commits into
bazel-contrib:mainfrom
alexeagle:lint/exports-files-discouraged

Conversation

@alexeagle

@alexeagle alexeagle commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

exports_files is a design smell: it leaks an InputArtifact to be in the srcs of an unrelated Bazel package. This breaks the principle of locality that allows BUILD files to be maintained in isolation.

With agents writing more BUILD files I've noticed this pattern increasingly used, when the right answer is to move the rule that needs to consume the source file.

Detail

  • Adds a new default-on buildifier lint check exports-files-discouraged that flags exports_files() as a package encapsulation violation ("feature envy").
  • When exports_files() is present, the check also warns if visibility is missing or not scoped to a package (:__pkg__ / :__subpackages__) or package_group, and if there is no comment explaining why encapsulation is being violated.
  • Suggests exposing files via a rule (e.g. filegroup) so the owning package can govern access and later swap the label for a generated artifact.

Test plan

  • bazel test //warn:warn_test --test_filter=TestExportsFilesDiscouraged
  • bazel test //warn/docs:docs_test
  • Verified disable comment # buildifier: disable=exports-files-discouraged suppresses all findings

Discourage exports_files() as a package encapsulation violation, require
scoped visibility and justification comments when it is used, and suggest
exposing files via rules such as filegroup instead.
@alexeagle
alexeagle requested a review from a team as a code owner August 19, 2026 17:19
@alexeagle
alexeagle requested review from oreflow and removed request for a team August 19, 2026 17:19
@oreflow
oreflow removed their request for review August 24, 2026 05:14
@alexeagle
alexeagle requested a review from fmeum August 26, 2026 21:06
Comment thread warn/warn_bazel_test.go
},
scopeBuild)

checkFindings(t, "exports-files-discouraged", `

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case, the comment and visibility should be enough that buildifier exits zero

Comment thread warn/warn_bazel.go
return nil
}

func isPackageOrPackageGroupVisibility(label string) bool {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the root package would have to use visibility = //:__subpackages__ - that's probably okay.

Alternatively maybe this rule should not evaluate the root BUILD file since it's common that linters and other repo-wide tooling need their config files in the root?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant