Skip to content

Add default file filter for format task#6864

Merged
waruqi merged 2 commits intoxmake-io:devfrom
Stehsaer:improve-format-task
Sep 28, 2025
Merged

Add default file filter for format task#6864
waruqi merged 2 commits intoxmake-io:devfrom
Stehsaer:improve-format-task

Conversation

@Stehsaer
Copy link
Contributor

Background

Currently, when using format task, xmake calls clang-format to format all files added through add_files. This is fine if the project/target only contains C or C++ files. However, when we have other files added as source files, such as binary assets (for utils.bin2c) and assembly files, clang-format will also "format" those and complely corrupt the files. Take a look at this example:

target("test")
    set_kind("binary")
    set_languages("c++11")
    add_rules("utils.bin2c", {extensions = {".gz"}})
    
    add_files("src/*.cpp")
    add_files("asset/font.ttf.gz")
    add_includedirs("include")
    add_headerfiles("include/*.hpp")
target_end()

File tree:

  • src/
    • main.cpp
    • a.cpp
  • include/
    • a.hpp
  • asset/
    • font.ttf.gz
  • xmake.lua

In the most recent xmake version (v3.0.3+HEAD.de7aca4), after executing xmake format test, asset/font.ttf.gz will be corrupted by clang-format, because xmake incorrectly added this file into the command line of clang-format.

For more info, see related discussions and issues:

How this PR fixes the issue

This PR fixes the issue by adding a default file filter when no --files option is specified for xmake format. The default file filter is set as **.c, **.cxx, **.cpp and **.cc, which covers most file extensions for C/C++ source.

Q&A

Why fix this issue?

Convenience. For projects (especially when developing for embedded sys. and computer graphics, where asm. files or art assets need to be added as source files), it is inconvenient to manually specifying --files=xxx every time. Adding a default filter can greatly improve the overall experience of using xmake, when a simple xmake format help you do all the jobs to format cxx files.

Why add default filter?

clang-format is designed for formatting C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code, and xmake is primarily designed for building C/C++ code. Adding a default filter matches the overall design goal of xmake, avoids passing unwanted files (archives, asm codes, etc.) to clang-format which causes unwanted file corruptions, and improves experience. Usage of formatting non-cxx-files as if they were cxx files is extremely rare, and in my point of view, should be done through custom tasks.

Other Changes

  • Fixed typo in xmake format task description

@Stehsaer Stehsaer changed the title Added default file filter for format task Add default file filter for format task Sep 27, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Stehsaer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the reliability and user-friendliness of the xmake format task by implementing a default file filtering mechanism. Previously, the task could inadvertently corrupt non-C/C++ files by passing them to clang-format. The new changes ensure that only relevant C/C++ source files are processed by default, thereby preventing data loss and simplifying the formatting process for developers working on projects with diverse asset types.

Highlights

  • Default File Filter for format task: Introduced a default file filter for the xmake format task, which now automatically targets C/C++ source files (**.c, **.cc, **.cxx, **.cpp) when the --files option is not explicitly provided. This prevents clang-format from processing and potentially corrupting non-C/C++ files.
  • Prevention of File Corruption: The new filter directly addresses an issue where xmake format would pass all add_files entries, including binary assets or assembly files, to clang-format, leading to their corruption.
  • Improved User Experience: Users no longer need to manually specify --files for typical C/C++ projects, streamlining the formatting workflow and making xmake format more robust for projects with mixed file types.
  • Documentation Fix: A minor typo in the description of the --files option within the xmake format task was corrected.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a default file filter for the format task to prevent clang-format from corrupting non-C/C++ files. The approach is sound, but the implementation is incomplete as it only filters source files and not header files, which could still lead to file corruption. I've suggested a fix to apply filtering to header files as well for consistency and robustness. The typo fix in the task description is correct.

@Stehsaer
Copy link
Contributor Author

Code Review

This pull request introduces a default file filter for the format task to prevent clang-format from corrupting non-C/C++ files. The approach is sound, but the implementation is incomplete as it only filters source files and not header files, which could still lead to file corruption. I've suggested a fix to apply filtering to header files as well for consistency and robustness. The typo fix in the task description is correct.

Under almost all cases, users won't add non-cxx files through add_headerfiles, because the API is specifically designed for adding cxx header files. Thus it make sense to only do filtering on source files.

@waruqi waruqi added this to the v3.0.4 milestone Sep 28, 2025
@waruqi waruqi merged commit 35be907 into xmake-io:dev Sep 28, 2025
22 checks passed
@Stehsaer Stehsaer deleted the improve-format-task branch September 28, 2025 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants