Lint and format your code
Enhance your development workflow by integrating with popular linters and formatters like SQLFluff, sqlfmt, Black, and Prettier. Leverage these powerful tools directly in the dbt Cloud IDE without interrupting your development flow.
Details
What are linters and formatters?
Linters analyze code for errors, bugs, and style issues, while formatters fix style and formatting rules. Read more about when to use linters or formatters in the FAQsIn the dbt Cloud IDE, you can perform linting, auto-fix, and formatting on five different file types:
- SQL — Lint and fix with SQLFluff, and format with sqlfmt
- YAML, Markdown, and JSON — Format with Prettier
- Python — Format with Black
Each file type has its own unique linting and formatting rules. You can customize the linting process to add more flexibility and enhance problem and style detection.
By default, the IDE uses sqlfmt rules to format your code, making it convenient to use right away. However, if you have a file named .sqlfluff
in the root directory of your dbt project, the IDE will default to SQLFluff rules instead.
Lint
With the dbt Cloud IDE, you can seamlessly use SQLFluff, a configurable SQL linter, to warn you of complex functions, syntax, formatting, and compilation errors. This integration allows you to run checks, fix, and display any code errors directly within the Cloud IDE:
- Works with Jinja and SQL,
- Comes with built-in linting rules. You can also customize your own linting rules.
- Empowers you to enable linting with options like Lint (displays linting errors and recommends actions) or Fix (auto-fixes errors in the IDE).
- Displays a Code Quality tab to view code errors, provides code quality visibility and management, and displays the SQLFluff version used.
Linting doesn't support ephemeral models in dbt v1.5 and lower. Refer to the FAQs for more info.
Enable linting
Linting is available on all branches, including your protected primary git branch. Since the dbt Cloud IDE prevents commits to the protected branch, it prompts you to commit those changes to a new branch.
- To enable linting, open a
.sql
file and click the Code Quality tab. - Click on the
</> Config
button on the bottom right side of the console section, below the File editor. - In the code quality tool config pop-up, you have the option to select sqlfluff or sqlfmt.
- To lint your code, select the sqlfluff radio button. (Use sqlfmt to format your code)
- Once you've selected the sqlfluff radio button, go back to the console section (below the File editor) to select the Lint or Fix dropdown button:
- Lint button — Displays linting issues in the IDE as wavy underlines in the File editor. You can hover over an underlined issue to display the details and actions, including a Quick Fix option to fix all or specific issues. After linting, you'll see a message confirming the outcome. Linting doesn't rerun after saving. Click Lint again to rerun linting.
- Fix button — Automatically fixes linting errors in the File editor. When fixing is complete, you'll see a message confirming the outcome.
- Use the Code Quality tab to view and debug any code errors.
Customize linting
SQLFluff is a configurable SQL linter, which means you can configure your own linting rules instead of using the default linting settings in the IDE. You can exclude files and directories by using a standard .sqlfluffignore
file. Learn more about the syntax in the .sqlfluffignore syntax docs.
To configure your own linting rules:
- Create a new file in the root project directory (the parent or top-level directory for your files). Note: The root project directory is the directory where your
dbt_project.yml
file resides. - Name the file
.sqlfluff
(make sure you add the.
beforesqlfluff
). - Create and add your custom config code.
- Save and commit your changes.
- Restart the IDE.
- Test it out and happy linting!
Refer to the SQLFluff config file to add the dbt code (or dbtonic) rules we use for our own projects:
dbtonic config code example provided by dbt Labs
[sqlfluff]
templater = dbt
# This change (from jinja to dbt templater) will make linting slower
# because linting will first compile dbt code into data warehouse code.
runaway_limit = 10
max_line_length = 80
indent_unit = space
[sqlfluff:indentation]
tab_space_size = 4
[sqlfluff:layout:type:comma]
spacing_before = touch
line_position = trailing
[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = lower
[sqlfluff:rules:aliasing.table]
aliasing = explicit
[sqlfluff:rules:aliasing.column]
aliasing = explicit
[sqlfluff:rules:aliasing.expression]
allow_scalar = False
[sqlfluff:rules:capitalisation.identifiers]
extended_capitalisation_policy = lower
[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = lower
[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = lower
[sqlfluff:rules:ambiguous.column_references] # Number in group by
group_by_and_order_by_style = implicit
For more info on styling best practices, refer to How we style our SQL.
Format
In the dbt Cloud IDE, you can format your code to match style guides with a click of a button. The IDE integrates with formatters like sqlfmt, Prettier, and Black to automatically format code on five different file types — SQL, YAML, Markdown, Python, and JSON:
- SQL — Format with sqlfmt, which provides one way to format your dbt SQL and Jinja.
- YAML, Markdown, and JSON — Format with Prettier.
- Python — Format with Black.
The Cloud IDE formatting integrations take care of manual tasks like code formatting, enabling you to focus on creating quality data models, collaborating, and driving impactful results.
Format SQL
To format your SQL code, dbt Cloud integrates with sqlfmt, which is an uncompromising SQL query formatter that provides one way to format the SQL query and Jinja.
By default, the IDE uses sqlfmt rules to format your code, making the Format button available and convenient to use immediately. However, if you have a file named .sqlfluff in the root directory of your dbt project, the IDE will default to SQLFluff rules instead.
Formatting is available on all branches, including your protected primary git branch. Since the dbt Cloud IDE prevents commits to the protected branch, it prompts you to commit those changes to a new branch.
- Open a
.sql
file and click on the Code Quality tab. - Click on the
</> Config
button on the right side of the console. - In the code quality tool config pop-up, you have the option to select sqlfluff or sqlfmt.
- To format your code, select the sqlfmt radio button. (Use sqlfluff to lint your code).
- Once you've selected the sqlfmt radio button, go to the console section (located below the File editor) to select the Format button.
- The Format button auto-formats your code in the File editor. Once you've auto-formatted, you'll see a message confirming the outcome.
Format YAML, Markdown, JSON
To format your YAML, Markdown, or JSON code, dbt Cloud integrates with Prettier, which is an opinionated code formatter. Formatting is available on all branches, including your protected primary git branch. Since the dbt Cloud IDE prevents commits to the protected branch, it prompts you to commit those changes to a new branch.
- Open a
.yml
,.md
, or.json
file. - In the console section (located below the File editor), select the Format button to auto-format your code in the File editor. Use the Code Quality tab to view code errors.
- Once you've auto-formatted, you'll see a message confirming the outcome.
You can add a configuration file to customize formatting rules for YAML, Markdown, or JSON files using Prettier. The IDE looks for the configuration file based on an order of precedence. For example, it first checks for a "prettier" key in your package.json
file.
For more info on the order of precedence and how to configure files, refer to Prettier's documentation. Please note, .prettierrc.json5
, .prettierrc.js
, and .prettierrc.toml
files aren't currently supported.
Format Python
To format your Python code, dbt Cloud integrates with Black, which is an uncompromising Python code formatter. Formatting is available on all branches, including your protected primary git branch. Since the dbt Cloud IDE prevents commits to the protected branch, it prompts you to commit those changes to a new branch.
- Open a
.py
file. - In the console section (located below the File editor), select the Format button to auto-format your code in the File editor.
- Once you've auto-formatted, you'll see a message confirming the outcome.