rnpgp / rnp

RNP: high performance C++ OpenPGP library used by Mozilla Thunderbird
https://www.rnpgp.org
Other
192 stars 54 forks source link

difference in clang-format versions pre-commit script and CI #2090

Open falko-strenzke opened 1 year ago

falko-strenzke commented 1 year ago

The CI currently seems check the code format with clang-format 11.0.0. But in the pre-commit Hook in current master, version 11.1.0 is mandated. I experienced already differences in two files, rather likely because of this difference in versions.

Which version of clang-format is the correct one and should be used for RNP development?

ni4 commented 1 year ago

Version 11.0 should be used, pre-commit hook should be fixed to v11.0.

ronaldtse commented 1 year ago

We should probably migrate all the pre-commit hooks into CI hooks.

Any thoughts @ni4 @antonsviridenko @maxirmx ?

falko-strenzke commented 1 year ago

By the way, while we are it: this RNP page still refers to clang-format-9.

falko-strenzke commented 1 year ago

Version 11.0 should be used, pre-commit hook should be fixed to v11.0.

Could you please also specify the patch version? It seems to me that even v11.0.0 and v11.0.1 are showing differences. Will we use 11.0.0 as configured in the CI?

ni4 commented 1 year ago

We should probably migrate all the pre-commit hooks into CI hooks.

To be honest I never used pre-commit hook and run clang-format manually via VS Code build tasks. Probably just fixing clang-format version to 11.0.0, or moving it to some include so it may be reused by CI and pre-commit hook, would be enough in this case.

@falko-strenzke correct version is one from the CI - 11.0.0, I'll make a PR for consolidating these things now.

ronaldtse commented 1 year ago

I feel we should just get rid of the pre-commit hooks and change them into CI jobs... Personally I dislike the scripts from a repo assuming some environment setup locally.

Since we already have the clang-format lint job in CI, I don't see why we need another one for pre-commit.

ronaldtse commented 1 year ago

We only have one pre-commit hook which is for clang-format:

@ni4 can we just remove the pre-commit hook scripts from the repository entirely in #2093 ?

falko-strenzke commented 1 year ago

I think it would still be useful to provide some means for a developer to quickly check and fix the code formatting. But the problem is indeed the version of clang-format affecting the formatting. I also observed that the pre-commit hook provided in RNP did not work reliable. At first at worked, then the next day falsely formatted code slipped through.

We are working now with a pre-commit hook based on this script. The scripts are checked into this branch in our fork. This is not an optimal solution since it always checks all files in src/ and include/, irrespectively of whether they are part of the commit. But it's simple and (hopefully) reliable.

falko-strenzke commented 1 year ago

With this approach it should be easy to only check added and modified files.

falko-strenzke commented 1 year ago

Another approch would be to optionally integrate the format check into the build system as build target. We have done that with CMake in one of our own projects and it works fine. Should that be interesting to you, we can provide the CMake code for that.

antonsviridenko commented 1 year ago

Another approch would be to optionally integrate the format check into the build system as build target. We have done that with CMake in one of our own projects and it works fine. Should that be interesting to you, we can provide the CMake code for that.

sounds interesting, I think it would be nice to have

antonsviridenko commented 1 year ago

Is it normal that minor versions of clang-format have changes in enforced code style? Maybe there is a way to impose some long-term stable code formatting?

ronaldtse commented 1 year ago

@falko-strenzke @antonsviridenko I think CMake integration could be a good solution.

There are people who tried using clang-format and clang-tidy with CMake:

falko-strenzke commented 1 year ago

Another approch would be to optionally integrate the format check into the build system as build target. We have done that with CMake in one of our own projects and it works fine. Should that be interesting to you, we can provide the CMake code for that.

sounds interesting, I think it would be nice to have

This is the code for adding one formatting target for manual invocation and one check target for automatic invocation after all other build targets:

SET(CLANG_FORMAT_BINARY "${CMAKE_SOURCE_DIR}/misc/tools/clang-format" CACHE STRING "Path to clang binary")
option(DISABLE_CLANG_FORMAT_CHECK "disable clang format target" OFF)

# get all project files
file(GLOB_RECURSE ALL_SOURCE_FILES *.cpp *.h)
foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
    string(FIND ${SOURCE_FILE} "ext_lib" PROJECT_TRDPARTY_DIR_FOUND) # search for substring in SOURCE_FILE, last arg is result set to -1 if not found
    string(FIND ${SOURCE_FILE} "${CMAKE_BINARY_DIR}/CMakeFiles/" BUILD_DIR_FOUND) # exclude also code under the build directory
    if (NOT ${PROJECT_TRDPARTY_DIR_FOUND} EQUAL -1 OR NOT ${BUILD_DIR_FOUND} EQUAL -1)
        MESSAGE("removing item from code formatting list" ${SOURCE_FILE})
        list(REMOVE_ITEM ALL_SOURCE_FILES ${SOURCE_FILE})
    endif ()
endforeach ()
foreach (SOURCE_FILE ${ALL_SOURCE_FILES})
  MESSAGE("source file for fomatting= " ${SOURCE_FILE})
endforeach ()
# add a target for formatting all source files
add_custom_target(
        clang-format-do
        COMMAND ${CLANG_FORMAT_BINARY}
        -style=file
        -i
        ${ALL_SOURCE_FILES}
)

if(DISABLE_CLANG_FORMAT_CHECK STREQUAL OFF)
    # add a target that is executed after all other targets
    add_custom_target(
            clang-format-check
            ALL
            DEPENDS <... all other relevant default build targets ...> 
            COMMAND ${CLANG_FORMAT_BINARY}
            --dry-run
            --Werror
            -style=file
            ${ALL_SOURCE_FILES}
    )
endif()
maxirmx commented 4 months ago

And yest another approach is to have clang formatting implemented at GHA

Something like this:

  clang-format:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: DoozyX/clang-format-lint-action@master
        with:
          source: '.'
          extensions: 'h,cpp,c'
          clangFormatVersion: 14
          inplace: True
      - name: Commit changes
        uses: EndBug/add-and-commit@v9
        with:
          message: clang-format fixes
          committer_name: GitHub Actions
          committer_email: actions@github.com
ni4 commented 4 months ago

@maxirmx in my workflow it was easier and more clean (no clang-format dedicated commits) to configure clang-format job in VS Code, and then, in case of need, run it and rebase.