Skip to content

Conversation

benfdking
Copy link
Contributor

Summary

  • add --fix flag to sqlmesh lint to apply automatic fixes and fail on remaining errors
  • implement fix application for lint violations
  • document new lint fix option and add tests

Testing

  • pre-commit run --files docs/examples/sqlmesh_cli_crash_course.md docs/guides/linter.md docs/reference/cli.md sqlmesh/cli/main.py sqlmesh/core/context.py tests/cli/test_cli.py
  • pytest tests/cli/test_cli.py::test_lint_fix tests/cli/test_cli.py::test_lint_fix_unfixable_error -q

https://chatgpt.com/codex/tasks/task_e_6899c89c18fc8330a9329492edcfeaff

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a --fix flag to the sqlmesh lint command that automatically applies available fixes for lint violations and fails if errors remain after fixes are applied.

  • Added --fix flag to the lint CLI command
  • Implemented fix application logic in the context's lint_models method
  • Added comprehensive tests for both successful fixes and unfixable errors

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sqlmesh/cli/main.py Added --fix flag option to lint command
sqlmesh/core/context.py Implemented fix application logic and recursive linting after fixes
tests/cli/test_cli.py Added test cases for fix functionality and unfixable error handling
docs/reference/cli.md Updated CLI documentation to include the new --fix option
docs/guides/linter.md Updated linter guide to mention the fix functionality
docs/examples/sqlmesh_cli_crash_course.md Added example usage of --fix flag

content = f.read()
content = content.replace(
"SELECT\n id,\n item_id,\n event_date,\nFROM",
"SELECT *\nFROM",
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

The string replacement pattern is fragile and depends on exact whitespace matching. Consider using a more robust approach like regex replacement or a more specific pattern that's less likely to break with formatting changes.

Suggested change
"SELECT *\nFROM",
content = re.sub(
r"SELECT\s*id,\s*item_id,\s*event_date,\s*FROM",
"SELECT *\nFROM",
content,
flags=re.MULTILINE,

Copilot uses AI. Check for mistakes.

"SELECT *\nFROM",
)
with open(model_path, "w", encoding="utf-8") as f:
f.write(content)
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

This string replacement pattern is duplicated from the previous test. Consider extracting this into a helper function to reduce code duplication and improve maintainability.

Suggested change
f.write(content)
replace_select_columns_with_star(model_path)

Copilot uses AI. Check for mistakes.

@@ -636,6 +636,7 @@ Usage: sqlmesh lint [OPTIONS]

Options:
--model TEXT A model to lint. Multiple models can be linted. If no models are specified, every model will be linted.
--fix Apply fixes for lint errors. Fails if errors remain after fixes are applied.
Copy link

@simon-pactum simon-pactum Aug 19, 2025

Choose a reason for hiding this comment

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

Would be nice to fix and still exit non-zero, makes it easier to use the same script to both fix things locally and error in CI, without needing an extra layer of pre-commit or something to detect if files were changed.

Similar discussion over at ruff, where they ended up introducing --exit-non-zero-on-fix astral-sh/ruff#8191

Just my 2c as a user - this isn't a review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants