forked from quarylabs/sqruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBUILD.bazel
More file actions
575 lines (526 loc) · 15.7 KB
/
BUILD.bazel
File metadata and controls
575 lines (526 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@aspect_bazel_lib//lib:run_binary.bzl", "run_binary")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//:prettier/package_json.bzl", prettier_bin = "bin")
load("@rules_rust//rust:defs.bzl", "rust_clippy", "rustfmt_test")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("@rules_shellcheck//:def.bzl", "shellcheck_test")
load(":cargo_build.bzl", "cargo_run", "cargo_test", "cargo_vendor", "cargo_vendor_provider", "python_venv", "python_venv_provider", "uv_python_install", "uv_python_venv")
# Link all npm packages into node_modules
npm_link_all_packages(name = "node_modules")
# Prettier configuration and pnpm workspace files
exports_files([
".prettierrc.json",
"package.json",
"pnpm-lock.yaml",
"pnpm-workspace.yaml",
])
# Files to check with prettier (root package only)
filegroup(
name = "prettier_srcs",
srcs = glob(
[
".github/**/*.yaml",
".github/**/*.yml",
],
) + [
"README.md",
"//editors/code:prettier_srcs",
"//playground:prettier_srcs",
],
)
# Prettier format check test
prettier_bin.prettier_test(
name = "prettier_check",
size = "small",
args = [
"--check",
"--config",
".prettierrc.json",
"editors",
"playground",
"*.md",
".github",
],
chdir = package_name(),
data = [
":node_modules/prettier",
":prettier_srcs",
".prettierrc.json",
"//editors/code:prettier_srcs",
"//playground:prettier_srcs",
],
)
# Prettier format (for fixing locally)
prettier_bin.prettier_binary(
name = "prettier_fix",
args = [
"--write",
"--config",
".prettierrc.json",
"editors",
"playground",
"*.md",
".github",
],
chdir = package_name(),
data = [
":node_modules/prettier",
":prettier_srcs",
".prettierrc.json",
"//editors/code:prettier_srcs",
"//playground:prettier_srcs",
],
env = {"JS_BINARY__CHDIR": "$$BUILD_WORKSPACE_DIRECTORY"},
)
# ESLint check for all TypeScript packages
# Runs ESLint on both playground and editors/code
# Usage: bazel test //:eslint_check
test_suite(
name = "eslint_check",
tests = [
"//editors/code:eslint_check",
"//playground:eslint_check",
],
)
# TypeScript check for all TypeScript packages
# Runs tsc --noEmit on both playground and editors/code
# Usage: bazel test //:typescript_check
test_suite(
name = "typescript_check",
tests = [
"//editors/code:typescript_check",
"//playground:typescript_check",
],
)
# Shellcheck test for shell scripts
shellcheck_test(
name = "shellcheck",
size = "small",
data = glob([".hacking/scripts/*.sh"]),
severity = "warning",
)
# Check that LICENSE and README.md are synced to editors/code
sh_test(
name = "check_files_match",
size = "small",
srcs = [".hacking/scripts/check_files_match.sh"],
data = [
"LICENSE",
"README.md",
"//editors/code:LICENSE",
"//editors/code:README.md",
],
)
# Ratchet check for GitHub Actions version pinning
sh_test(
name = "ratchet_check",
size = "small",
srcs = [".hacking/scripts/ratchet_check.sh"],
data = [
"@ratchet//:ratchet",
] + glob([".github/workflows/*.yml"]),
)
# Verify all pinned GitHub Actions SHAs actually exist
sh_test(
name = "verify_action_shas",
size = "small",
srcs = [".hacking/scripts/verify_action_shas.sh"],
data = glob([".github/workflows/*.yml"]),
tags = ["requires-network"],
)
# Check that versions match across Cargo.toml, package.json, pyproject.toml
sh_test(
name = "check_versions_match",
size = "small",
srcs = [".hacking/scripts/check_versions_match.sh"],
data = [
"Cargo.toml",
"//editors/code:package.json",
"//crates/cli-python:pyproject.toml",
"pyproject.toml",
],
)
# Python ruff check - format and lint
sh_test(
name = "ruff_check",
size = "small",
srcs = [".hacking/scripts/ruff_check.sh"],
data = [
"pyproject.toml",
"//crates/cli-python:python_srcs",
"@aspect_rules_lint//lint:ruff_bin",
] + glob([".hacking/**/*.py"]),
env = {
"RUFF": "$(rlocationpath @aspect_rules_lint//lint:ruff_bin)",
},
)
# Python ruff fix - format and lint fix (for fixing locally and in CI)
sh_binary(
name = "ruff_fix",
srcs = [".hacking/scripts/ruff_fix.sh"],
data = [
"pyproject.toml",
"//crates/cli-python:python_srcs",
"@aspect_rules_lint//lint:ruff_bin",
] + glob([".hacking/**/*.py"]),
env = {
"RUFF": "$(rlocationpath @aspect_rules_lint//lint:ruff_bin)",
},
)
# All Rust targets for linting
RUST_TARGETS = [
"//crates/cli:sqruff",
"//crates/cli-lib:sqruff-cli-lib",
"//crates/lib:sqruff-lib",
"//crates/lib-core:sqruff-lib-core",
"//crates/lib-dialects:sqruff-lib-dialects",
"//crates/lib-wasm:sqruff-wasm",
"//crates/lineage:lineage",
"//crates/lsp:sqruff-lsp",
"//crates/sqlinference:sqruff-sqlinference",
]
# Common Cargo source files for cargo-based checks
# Used by cargo_machete, cargo_check, cargo_hack_check, rust_lint
filegroup(
name = "cargo_srcs",
srcs = [
"Cargo.lock",
"Cargo.toml",
"rust-toolchain.toml",
"//crates/cli:machete_srcs",
"//crates/cli-lib:machete_srcs",
"//crates/cli-python:machete_srcs",
"//crates/lib:machete_srcs",
"//crates/lib-core:machete_srcs",
"//crates/lib-dialects:machete_srcs",
"//crates/lib-wasm:machete_srcs",
"//crates/lineage:machete_srcs",
"//crates/lsp:machete_srcs",
"//crates/sqlinference:machete_srcs",
],
)
# Vendor cargo dependencies and install Rust toolchain (cached until Cargo.lock changes)
# Installs Rust via rustup with clippy and rustfmt components
cargo_vendor(
name = "cargo_vendor",
manifests = [":cargo_srcs"],
rust_version = "1.94.1",
components = [
"clippy",
"rustfmt",
],
)
# Provider wrapper for vendored dependencies
cargo_vendor_provider(
name = "cargo_deps",
vendor = ":cargo_vendor",
)
# Pre-cache Python venv with all dev dependencies (cached until pyproject.toml changes)
# Uses the Bazel-managed Python 3.12 interpreter and uv for fast pip installs
python_venv(
name = "python_venv",
srcs = [
"pyproject.toml",
],
python = ["@python_3_12//:files"],
uv = ["@uv//:uv"],
)
# Provider wrapper for Python venv
python_venv_provider(
name = "python_deps",
venv = ":python_venv",
)
# Rust format check
rustfmt_test(
name = "rustfmt_check",
targets = RUST_TARGETS,
)
# Rust clippy check
rust_clippy(
name = "clippy_check",
deps = RUST_TARGETS,
)
# Codegen docs check - verify generated docs are up to date
# Uses vendored Rust toolchain for hermetic builds (same as cargo_test)
cargo_test(
name = "codegen_docs_check",
srcs = [
":cargo_srcs",
"docs/reference/cli.md",
"docs/reference/rules.md",
"docs/reference/templaters.md",
],
vendor = ":cargo_deps",
python_venv = ":python_deps",
script = """
# Build the codegen binary
cargo build --bin sqruff -F codegen-docs --locked --offline
# Create temp directory for generated output
GENDIR=$(mktemp -d)
mkdir -p "$GENDIR/docs/reference"
# Run codegen from the gendir
(cd "$GENDIR" && GITHUB_ACTIONS=false "$CARGO_TARGET_DIR/debug/sqruff")
# Compare the generated files with the originals
FAILED=0
for doc in cli.md rules.md templaters.md; do
if ! diff -q "docs/reference/$doc" "$GENDIR/docs/reference/$doc" > /dev/null 2>&1; then
echo "ERROR: docs/reference/$doc is out of date"
echo "Run 'cargo run --bin sqruff -F codegen-docs' to update"
echo ""
diff "docs/reference/$doc" "$GENDIR/docs/reference/$doc" || true
FAILED=1
fi
done
if [ $FAILED -eq 1 ]; then
exit 1
fi
echo "All generated docs are up to date"
""",
)
# Codegen docs fix - regenerate docs and copy them back to the workspace
# Uses BUILD_WORKSPACE_DIRECTORY to write to the real workspace, mirroring
# how @rules_rust//:rustfmt operates.
cargo_run(
name = "codegen_docs_fix",
srcs = [
":cargo_srcs",
"docs/reference/cli.md",
"docs/reference/rules.md",
"docs/reference/templaters.md",
],
vendor = ":cargo_deps",
python_venv = ":python_deps",
script = """
# Build the codegen binary
cargo build --bin sqruff -F codegen-docs --locked --offline
# Run codegen into the real workspace
(cd "$BUILD_WORKSPACE_DIRECTORY" && GITHUB_ACTIONS=false "$CARGO_TARGET_DIR/debug/sqruff")
echo "Docs regenerated!"
""",
)
# Cargo machete - check for unused dependencies
# Note: Files are copied to a temp directory because cargo-machete doesn't follow symlinks
sh_test(
name = "cargo_machete",
size = "small",
srcs = [".hacking/scripts/cargo_machete.sh"],
data = [
":cargo_srcs",
"@cargo_machete//:cargo-machete",
"@rules_rust//rust/toolchain:current_cargo_files",
],
env = {
"CARGO": "$(rlocationpath @rules_rust//rust/toolchain:current_cargo_files)",
"MACHETE": "$(rlocationpath @cargo_machete//:cargo-machete)",
"RUSTUP_TOOLCHAIN": "stable",
},
tags = ["exclusive"],
)
# Cargo check - type-check all crates with all features, tests, and benches
# This replicates the "Rust Check" GitHub action job
# Uses vendored dependencies for hermetic builds
cargo_test(
name = "cargo_check",
srcs = [":cargo_srcs"],
vendor = ":cargo_deps",
script = "cargo check --all --all-features --tests --benches --locked --offline",
)
# Cargo build release - build release binaries with all features
# This replicates the "Compile" GitHub action job
# Uses vendored dependencies for hermetic builds
cargo_test(
name = "cargo_build_release",
size = "enormous",
srcs = [":cargo_srcs"],
vendor = ":cargo_deps",
script = "cargo build --locked --release --all-features --offline",
)
# Cargo clippy - run clippy with all features
# Uses vendored dependencies for hermetic builds
cargo_test(
name = "cargo_clippy",
srcs = [":cargo_srcs"],
vendor = ":cargo_deps",
script = "cargo clippy --all --all-features --offline -- -D warnings",
)
# Cargo fmt check - verify code formatting
# Uses vendored dependencies for hermetic builds
cargo_test(
name = "cargo_fmt_check",
srcs = [":cargo_srcs"],
vendor = ":cargo_deps",
script = "cargo fmt --all -- --check",
)
# Cargo test - run all tests exactly like the GitHub Action
# Uses vendored Rust toolchain and pre-cached Python venv for fully sandboxed execution
# Python venv provides maturin, pytest, jinja2, dbt, etc.
cargo_test(
name = "cargo_test",
size = "enormous",
srcs = [
":cargo_srcs",
"pyproject.toml",
"uv.lock",
"//crates/cli:test_fixtures",
"//crates/cli-python:machete_srcs",
"//crates/cli-python:pyproject.toml",
"//crates/cli-python:python_srcs",
"//crates/cli-python:test_fixtures",
"//crates/lib:test_fixtures",
"//crates/lib-dialects:test_fixtures",
],
vendor = ":cargo_deps",
python_venv = ":python_deps",
script = """
# Build Python extension (maturin uses the vendored cargo + pre-cached venv)
(cd crates/cli-python && maturin develop --locked --offline)
# Run cargo tests (same as GitHub Action)
cargo test --manifest-path ./crates/cli/Cargo.toml --locked --offline
# Crates that don't need the Python/maturin setup run natively via rust_test:
# //crates/lib-core:sqruff-lib-core-test
# //crates/sqlinference:sqruff-sqlinference-test
# //crates/lineage:lineage-test
cargo test --all --all-features --exclude sqruff --exclude sqruff-lib-core --exclude sqruff-sqlinference --exclude lineage --locked --offline
""",
)
# Cargo hack check - verify each feature compiles in isolation
# This ensures no feature combination is broken
# Uses vendored dependencies for hermetic builds
cargo_test(
name = "cargo_hack_check",
size = "enormous",
srcs = [":cargo_srcs"],
tools = ["@cargo_hack//:cargo-hack"],
vendor = ":cargo_deps",
script = "cargo hack check --each-feature --exclude-features=codegen-docs --offline",
)
# Zensical docs build - verify documentation builds successfully
sh_test(
name = "zensical_build",
size = "large",
srcs = [".hacking/scripts/zensical_build.sh"],
data = [
"pyproject.toml",
"uv.lock",
"zensical.toml",
"@uv//:uv",
] + glob(["docs/**"]),
env = {
"UV": "$(rlocationpath @uv//:uv)",
},
tags = ["exclusive"],
)
# Zensical docs serve - build docs and optionally serve locally
# Usage: bazel run //:zensical_serve
# bazel run //:zensical_serve -- --serve
sh_binary(
name = "zensical_serve",
srcs = [".hacking/scripts/zensical_serve.sh"],
)
# Zensical docs genrule tool - builds docs to output directory
sh_binary(
name = "zensical_genrule_tool",
srcs = [".hacking/scripts/zensical_genrule.sh"],
data = [
"pyproject.toml",
"uv.lock",
"zensical.toml",
"@uv//:uv",
] + glob(["docs/**"]),
env = {
"UV": "$(rlocationpath @uv//:uv)",
},
)
# Build zensical docs as output directory
run_binary(
name = "docs_build",
srcs = [
"pyproject.toml",
"uv.lock",
"zensical.toml",
] + glob(["docs/**"]),
outs = [],
args = ["$(BINDIR)/docs_site"],
out_dirs = ["docs_site"],
tool = ":zensical_genrule_tool",
)
# Combined website: playground at root, docs at /docs
# Usage: bazel build //:website
copy_to_directory(
name = "website",
srcs = [
"//playground:build",
":docs_build",
],
out = "website",
replace_prefixes = {
"playground/dist": "",
"docs_site": "docs",
},
)
# Verify .sqlfluff-sha points to a real commit in sqlfluff/sqlfluff
sh_test(
name = "verify_sqlfluff_sha",
size = "small",
srcs = [".hacking/scripts/verify_sqlfluff_sha.sh"],
data = [".sqlfluff-sha"],
tags = ["requires-network"],
)
# Python tests using uv - three incremental cached steps per version:
# 1. uv_python_install: download and cache the Python runtime (re-runs only when version changes)
# 2. uv_python_venv: sync dependencies into a cached venv (re-runs only when pyproject.toml/uv.lock change)
# 3. sh_test: run pytest using the cached venv (runs every time)
[
uv_python_install(
name = "pytest_py{}_python".format(version.replace(".", "")),
python_version = version,
uv = ["@uv//:uv"],
)
for version in [
"3.10",
"3.11",
"3.12",
"3.13",
]
]
[
uv_python_venv(
name = "pytest_py{}_venv".format(version.replace(".", "")),
srcs = [
"pyproject.toml",
"uv.lock",
],
python = [":pytest_py{}_python".format(version.replace(".", ""))],
uv = ["@uv//:uv"],
)
for version in [
"3.10",
"3.11",
"3.12",
"3.13",
]
]
[
sh_test(
name = "pytest_py{}".format(version.replace(".", "")),
size = "large",
srcs = [".hacking/scripts/pytest_uv.sh"],
data = [
":pytest_py{}_venv".format(version.replace(".", "")),
"//crates/cli-python:python_srcs",
"//crates/cli-python:test_fixtures",
],
env = {
"PYTHON_VENV": "$(rlocationpath :pytest_py{}_venv)".format(version.replace(".", "")),
},
tags = ["exclusive"],
)
for version in [
"3.10",
"3.11",
"3.12",
"3.13",
]
]