Skip to content

Make fprime-util format work for libraries - #331

Merged
thomas-bc merged 3 commits into
nasa:develfrom
Sammy-Dabbas:issue-4926-format-libraries
Sep 1, 2026
Merged

thomas-bc merged 3 commits into
nasa:develfrom
Sammy-Dabbas:issue-4926-format-libraries

Conversation

@Sammy-Dabbas

Copy link
Copy Markdown
Contributor

Fixes nasa/fprime#4926.

Problem

fprime-util format fails in an F Prime library (e.g. fprime-zephyr) for two stacked reasons:

  1. format went through load_build(), which calls Build.find_nearest_parent_project(). A library has no root CMakeLists.txt with project(), so the command dies with "Could not detect project directory" before formatting ever runs.
  2. The style file was hardcoded to framework_path/.clang-format from settings.ini, which libraries do not have.

Fix

  • Add format to skip_build_loading() (alongside version-check) so no build/project context is required, and remove it from the now-irrelevant skip_build_cache_validation().
  • New locate_clang_format_file() in commands.py: when a parent F Prime project is detected, use the framework's .clang-format exactly as before (no behavior change for deployments); otherwise walk up the directory tree from the working path for a .clang-format, mirroring clang-format's own discovery convention so libraries can carry their own style file.
  • Guard builder.settings access in ClangFormatter.execute() so it tolerates build=None.

Testing

  • 4 new tests covering the library cases: execute with no build object, style-file discovery from inside a library, the missing-style-file error path, and an end-to-end run_code_format against a synthetic library with its own .clang-format (skipped if clang-format is unavailable).
  • Formatter suite: 11 passed (7 existing + 4 new), re-verified after rebasing onto current devel.
  • Manually exercised the CLI against synthetic libraries: format and --check behave correctly, a missing .clang-format produces a clear error, and the deployment/project case still resolves the framework style file (regression-checked).

Happy to adjust the discovery order or error text if you have a preferred convention.

format previously died in load_build() (libraries have no project root
with a CMakeLists project()) and hardcoded the style file to
framework_path/.clang-format from settings.ini (libraries have neither).

Skip build loading for format, and resolve the style file via the
framework path when a project is detected (preserving old behavior) or
by walking up from the working path for a .clang-format, matching
clang-format's own discovery convention.

Fixes nasa/fprime#4926.
@Sammy-Dabbas
Sammy-Dabbas force-pushed the issue-4926-format-libraries branch from 08f56b4 to 89caf97 Compare July 4, 2026 05:12
Comment thread src/fprime/util/commands.py Outdated
Comment thread src/fprime/util/commands.py Outdated
@Sammy-Dabbas

Copy link
Copy Markdown
Contributor Author

Thanks. clang-format is already invoked with --style=file, so it discovers the nearest .clang-format from each input file's directory on its own. I dropped the manual walk up. The locate_clang_format_file now only resolves the framework's .clang-format when a project is detected and returns None for a standalone library, and the formatter skips its pre-flight existence check when there's no explicit framework file. The end-to-end library test still passes because clang-format finds the library's config. A library shipping no .clang-format falls back to clang-format's default style rather than erroring, which matches its normal behavior.

@thomas-bc thomas-bc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looking at this in a bit more detail, I'm realizing that things have been off for a while (nothing to do with your PR - so thanks for unearthing this!). The code goes and look for path/to/fprime/.clang-format, but it's never actually used.

Since #199, we only specify --style=file and never give an explicit path. So fprime-util format always delegates to clang-format to find the .clang-format file in a parent repo.

This is intended. Projects have their own .clang-format file, and libraries that want formatting should define their own as well. There's therefore no reason to try and find the fprime/.clang-format file. We should remove the logic to attempt to do it. Libraries can then run fprime-util format and it'll behave exactly the same: let clang-format try to find a .clang-format file in a parent directory.

@Sammy-Dabbas

Copy link
Copy Markdown
Contributor Author

Done in 5e99471. The --verbose location print is left out since we no longer
locate the file ourselves. There's nothing for us to print, and clang-format
resolves it internally. Happy to add it back in some form if you'd like.

@Sammy-Dabbas
Sammy-Dabbas requested a review from thomas-bc July 8, 2026 16:35
@Sammy-Dabbas

Copy link
Copy Markdown
Contributor Author

Hi @thomas-bc, following up on this one. I pushed the changes you asked for; the framework .clang-format lookup is removed and discovery is delegated to clang-format via --style=file (5e99471, 6bac973). I left the --verbose found-location print out since the tool no longer resolves the file itself, but I am happy to add some form of it if you would like.

@thomas-bc
thomas-bc merged commit f583568 into nasa:devel Sep 1, 2026
22 checks passed
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.

fprime-util format should work for libraries

2 participants