-
Notifications
You must be signed in to change notification settings - Fork 8k
CI: devicetree: Add compliance and formatting check #92334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,3 +86,35 @@ When there are differences between the `Coding Style Guidelines`_ guidelines and | |
formatting generated by code formatting tools, the `Coding Style Guidelines`_ guidelines | ||
take precedence. If there is ambiguity between formatting tools and the | ||
guidelines, maintainers may decide which style should be adopted. | ||
|
||
dts-linter | ||
============ | ||
|
||
The `dts-linter <https://www.npmjs.com/package/dts-linter>`_ can be helpful | ||
to quickly reformat large amounts of devicetree files to our `Coding Style Guidelines`_ | ||
standards. You can also run it manually like this: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be reworded to "Zephyr uses the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And then it would be tool enforcement, and absolutely unacceptable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is style enforcement in CI, not tool usage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JarmouniA The wording I used is very similar to the working above for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Even if, it is a claim that you have to prove first. Wording you suggest is unacceptable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jfischer-no can you make a proposal? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I missing something, or is this meant to be run in CI to enforce DT style!? And the contributor does not have to use it to correct the issues found, can correct them manually, or download the patch file generated by the tool and apply it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes
Correct
Correct
Correct, unless they want to extend the check to check their files too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are missing the difference between "can be helpful" and "Zephyr uses the dts-linter to enforce and auto-format Devicetree files in CI according to our Devicetree Style Guidelines". For the latter, you need formal proof that it really does so, according to our style guidelines, which you do not have, and there is a complete lack of experience in PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Keep it how it is. |
||
|
||
For individual files | ||
.. code-block:: bash | ||
|
||
npx dts-linter --format --file board.dts --file board_pinctrl.dtsi --patchFile diff.patch | ||
git apply diff.patch | ||
|
||
You can omit ``--file`` and this will format all files under the directory where the command | ||
has been called. Alternatively ``--cwd`` can also be passed set the base dir where the tool | ||
should look for files. This option is also used to make the paths relative in the patch file. | ||
|
||
You can also fix in place with | ||
.. code-block:: bash | ||
|
||
npx dts-linter --formatFixAll | ||
JarmouniA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
Editor Integration | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
* For VS Code: Install the extension from the `VS Code Marketplace <https://marketplace.visualstudio.com/items?itemName=KyleMicallefBonnici.dts-lsp>`_ or `Open VSIX <https://open-vsx.org/extension/KyleMicallefBonnici/dts-lsp>`_ | ||
* For other editors with LSP Client support: Use the devicetree-language-server `devicetree-language-server <https://www.npmjs.com/package/devicetree-language-server>`_ | ||
|
||
Make sure you follow `Devicetree Style Guidelines <https://docs.zephyrproject.org/latest/contribute/style/devicetree.html>`_ | ||
requirements to configure the editor correctly. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
Check warning on line 1 in package.json
|
||
pdgendt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"private": true, | ||
"dependencies": { | ||
"dts-linter": "^0.3.0-beta3" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -489,6 +489,105 @@ def required_false_check(self, binding): | |
"'required: false' is redundant, please remove" | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an extra empty line |
||
|
||
class DevicetreeLintingCheck(ComplianceTest): | ||
""" | ||
Checks if we are introducing syntax or formatting issues to devicetree files. | ||
""" | ||
name = "DevicetreeLinting" | ||
doc = "See https://docs.zephyrproject.org/latest/contribute/style/devicetree.html for more details." | ||
|
||
def _parse_json_output(self, cmd, cwd=None): | ||
"""Run command and parse single JSON output with issues array""" | ||
result = subprocess.run( | ||
cmd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
check=False, | ||
text=True, | ||
cwd=cwd or GIT_TOP | ||
) | ||
|
||
if not result.stdout.strip(): | ||
return None | ||
|
||
try: | ||
json_data = json.loads(result.stdout) | ||
return json_data | ||
except json.JSONDecodeError as e: | ||
raise RuntimeError(f"Failed to parse dts-linter JSON output: {e}") | ||
|
||
def run(self): | ||
# Get changed DTS files | ||
dts_files = [ | ||
file for file in get_files(filter="d") | ||
if file.endswith((".dts", ".dtsi", ".overlay")) | ||
] | ||
|
||
if not dts_files: | ||
self.skip('No DTS') | ||
|
||
temp_patch_files = [] | ||
batch_size = 500 | ||
|
||
for i in range(0, len(dts_files), batch_size): | ||
batch = dts_files[i:i + batch_size] | ||
|
||
# use a temporary file for each batch | ||
temp_patch = f"dts_linter_{i}.patch" | ||
temp_patch_files.append(temp_patch) | ||
|
||
cmd = [ | ||
"npx", "--no", "dts-linter", "--", | ||
"--outputFormat", "json", | ||
"--format", | ||
"--patchFile", temp_patch, | ||
] | ||
for file in batch: | ||
cmd.extend(["--file", file]) | ||
|
||
try: | ||
json_output = self._parse_json_output(cmd) | ||
|
||
if json_output and "issues" in json_output: | ||
cwd = json_output.get("cwd", "") | ||
logging.info(f"Processing issues from: {cwd}") | ||
|
||
for issue in json_output["issues"]: | ||
level = issue.get("level", "unknown") | ||
message = issue.get("message", "") | ||
|
||
if level == "info": | ||
logging.info(message) | ||
else: | ||
title = issue.get("title", "") | ||
file = issue.get("file", "") | ||
line = issue.get("startLine", None) | ||
col = issue.get("startCol", None) | ||
end_line = issue.get("endLine", None) | ||
end_col = issue.get("endCol", None) | ||
self.fmtd_failure(level, title, file, line, col, message, end_line, end_col) | ||
|
||
except subprocess.CalledProcessError as ex: | ||
stderr_output = ex.stderr if ex.stderr else "" | ||
if stderr_output.strip(): | ||
self.failure(f"dts-linter found issues:\n{stderr_output}") | ||
else: | ||
self.failure("dts-linter failed with no output. " | ||
"Make sure you install Node.JS and then run npm ci inside ZEPHYR_BASE") | ||
except RuntimeError as ex: | ||
self.failure(f"{ex}") | ||
|
||
# merge all temp patch files into one | ||
with open("dts_linter.patch", "wb") as final_patch: | ||
for patch in temp_patch_files: | ||
with open(patch, "rb") as f: | ||
shutil.copyfileobj(f, final_patch) | ||
|
||
# cleanup | ||
for patch in temp_patch_files: | ||
os.remove(patch) | ||
|
||
class KconfigCheck(ComplianceTest): | ||
""" | ||
Checks is we are introducing any new warnings/errors with Kconfig, | ||
|
Uh oh!
There was an error while loading. Please reload this page.