{"id":16682331,"url":"https://github.com/jpsim/swiftlint-bazel-example","last_synced_at":"2025-12-26T10:08:00.295Z","repository":{"id":51333598,"uuid":"520099255","full_name":"jpsim/swiftlint-bazel-example","owner":"jpsim","description":"Example SwiftLint integration with custom native rules using Bazel","archived":false,"fork":false,"pushed_at":"2023-11-22T10:51:08.000Z","size":633,"stargazers_count":15,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-14T03:08:48.846Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Starlark","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jpsim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-01T12:22:57.000Z","updated_at":"2024-11-18T08:29:29.000Z","dependencies_parsed_at":"2023-11-22T11:49:18.929Z","dependency_job_id":null,"html_url":"https://github.com/jpsim/swiftlint-bazel-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpsim%2Fswiftlint-bazel-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpsim%2Fswiftlint-bazel-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpsim%2Fswiftlint-bazel-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpsim%2Fswiftlint-bazel-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpsim","download_url":"https://codeload.github.com/jpsim/swiftlint-bazel-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243392326,"owners_count":20283565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T14:07:03.726Z","updated_at":"2025-12-26T10:07:55.240Z","avatar_url":"https://github.com/jpsim.png","language":"Starlark","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SwiftLint Bazel Example\n\nThis is an example repo that integrates [SwiftLint][SwiftLint] with\n[Bazel][Bazel].\n\n## Pre-Requisites\n\n* Install Bazel or Bazelisk (recommended): https://github.com/bazelbuild/bazelisk#installation\n* Install Xcode 15 or later: https://developer.apple.com/xcode/\n* You must be running macOS 13 or later\n\n## Basic Integration\n\n_Please see [SwiftLint's bazel instructions][swiftlint-bazel-instructions]\nif these instructions become out of date._\n\nMake sure that you're using bzlmod by adding this line to your\n`.bazelrc`:\n\n```\ncommon --enable_bzlmod\n```\n\nThen put this in your `MODULE.bazel`:\n\n```python\nbazel_dep(name = \"swiftlint\", version = \"0.54.0\", repo_name = \"SwiftLint\")\n```\n\nThen you can run SwiftLint with this command:\n\n```console\nbazel run -c opt @SwiftLint//:swiftlint -- --help\n```\n\n## Custom Rules\n\n### Setup\n\nYou can wire up SwiftLint to use some custom, private, native SwiftLint\nrules by adding this to your `MODULE.bazel`:\n\n```python\nextra_rules = use_extension(\"@SwiftLint//bazel:extensions.bzl\", \"extra_rules\")\nextra_rules.setup(srcs = \"@swiftlint-bazel-example//swiftlint_extra_rules:extra_rules\")\n```\n\nFinally, you'll need to tell Bazel what source files you want to compile\nas part of SwiftLint by placing `BUILD` configuration into directory with\ncustom rules sources.\n\nDuring build, Bazel includes custom rules through `func extraRules() -\u003e [Rule.Type]`,\nwhich should exist in sources. Easiest way is to manually write this function in\nseparate source file, but for example below this function is autogenerated.\n\n\u003e :warning:  Starting from SwiftLint 0.52.0 extra rules reside in separate\n\u003e module. This requires `public` keyword for `func extraRules()`.\n\nExample of `swiftlint_extra_rules/BUILD` configuration to include custom\nSwiftLint rules sources as well as autogenerated `extra_rules.swift` :\n\n```python\nfilegroup(\n    name = \"extra_rules\",\n    srcs = glob([\"**/*.swift\"]) + [\"extra_rules.swift\"],\n    visibility = [\"//visibility:public\"],\n)\n\ngenrule(\n    name = \"extra_rules_func\",\n    srcs = glob([\"*.swift\"]),\n    outs = [\"extra_rules.swift\"],\n    cmd = \"\"\"\nset -euo pipefail\n\necho \"public func extraRules() -\u003e [any Rule.Type] {\" \u003e\u003e $(OUTS)\necho \"  [\" \u003e\u003e $(OUTS)\nfor file in $(SRCS); do\n  filename=$$(basename -- \"$$file\")\n  echo \"    $${filename%.*}.self,\" \u003e\u003e $(OUTS)\ndone\necho \"  ]\" \u003e\u003e $(OUTS)\necho \"}\" \u003e\u003e $(OUTS)\n    \"\"\",\n)\n```\n\n### Writing Rules\n\nThen any Swift sources you add in the `swiftlint_extra_rules` directory\nwill be compiled as part of the `SwiftLintFramework` module just as if\nthose files had been in the official upstream SwiftLint git repository.\n\nThis means you have access to all the internal APIs in that module.\nThese internal APIs can and will change over time, so you may need to\nadjust the custom code you write accordingly whenever you update the\nversion of SwiftLint you target.\n\nThen build and run SwiftLint and you should see your rule being applied:\n\n```console\n$ bazel run -c opt @SwiftLint//:swiftlint\nLinting Swift files in current working directory\nLinting 'ForbiddenVarRule.swift' (2/3)\nLinting 'file.swift' (1/3)\nLinting 'ExtraRules.swift' (3/3)\nfile.swift:1:14: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals. (colon)\nfile.swift:1:5: warning: Forbidden Var Violation: Can't name a variable 'forbidden' (forbidden_var)\nDone linting! Found 2 violations, 0 serious in 3 files.\n```\n\n### Testing Rules\n\nYou can validate the triggering, non-triggering and correction examples\nin your rule's description by running SwiftLint's \"extra rules\" tests:\n\n```console\nbazel test --test_output=streamed @SwiftLint//Tests:ExtraRulesTests\n```\n\n## Developing in Xcode\n\nYou can use the excellent [rules_xcodeproj][rules_xcodeproj] project to\ngenerate an Xcode project giving you Xcode's IDE functionality to help\nyou develop your rules.\n\nYou can add it to the bottom of your `MODULE.bazel` file:\n\n```python\nbazel_dep(name = \"rules_xcodeproj\", version = \"1.4.0\")\n```\n\nAnd define a `BUILD` file with this configuration:\n\n```python\nload(\"@rules_xcodeproj//xcodeproj:defs.bzl\", \"xcodeproj\")\n\nxcodeproj(\n    name = \"swiftlint_xcodeproj\",\n    project_name = \"SwiftLint\",\n    top_level_targets = [\n        \"@SwiftLint//:swiftlint\",\n        \"@SwiftLint//Tests:ExtraRulesTests\",\n    ],\n)\n```\n\nGenerate and open the Xcode project with this command:\n\n```console\nbazel run :swiftlint_xcodeproj \u0026\u0026 xed .\n```\n\nAt which point you should have access to most Xcode features you'd want\nwhen developing.\n\n![Xcode](xcode.png)\n\n[SwiftLint]: https://github.com/realm/SwiftLint\n[Bazel]: https://bazel.build\n[swiftlint-bazel-instructions]: https://github.com/realm/SwiftLint#using-bazel\n[rules_xcodeproj]: https://github.com/buildbuddy-io/rules_xcodeproj\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpsim%2Fswiftlint-bazel-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpsim%2Fswiftlint-bazel-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpsim%2Fswiftlint-bazel-example/lists"}