Skip to content

dev: enforce formatting matching upstream style #49

Open

Description

@ovitrif

nit: we should find a way to enforce a formatting style that is as closely matching the code style by spiral, so that:

  1. our AI tools no longer change formatting with each iteration
  2. changes in files that originate from upstream are both uniformly formatted and do not update lines of code that don't contain logic changes

But this is for another PR.

Originally posted by @ovitrif in #48 (comment)

Activity

  1. ovitrif commented on Mar 2, 2026

    @ovitrif
    CollaboratorAuthor

    @claude What solutions could we adopt for addressing this issue?!

  2. claude commented on Mar 2, 2026

    @claude

    Claude finished @ovitrif's task in 1m 25s —— View job


    Solutions for Coding Style Enforcement

    Here's a breakdown of the current state and concrete solutions, ordered from highest to lowest impact.


    Current State

    • A rustfmt.toml exists with specific settings (hard tabs, max_width = 100, compressed fn params, etc.)
    • A format check exists in .github/workflows/rust.yml (cargo fmt --all -- --check)
    • Critical gap: rust.yml is triggered only on workflow_dispatch (manual), not on every PR or push — so format checks are not automatically enforced
    • A nightly rustfmt bot workflow exists but is also manual-only now

    Recommended Solutions

    1. Add a Dedicated fmt-check.yml CI Workflow (Highest Priority)

    The most impactful fix. Add a lightweight workflow that runs on every PR and push:

    name: Format Check
    
    on:
      push:
        branches: [main]
      pull_request:
    
    jobs:
      fmt:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: dtolnay/rust-toolchain@stable
            with:
              components: rustfmt
          - run: cargo fmt --all -- --check

    Make this a required status check in GitHub branch protection rules. This alone eliminates the "AI tools reformat each iteration" problem — any PR that introduces formatting changes will fail CI.

    2. Align rustfmt.toml with Upstream (spiral/ldk-node)

    Currently our rustfmt.toml has 14 settings. Upstream may have different settings, causing spurious diffs when merging upstream changes.

    Action: Compare rustfmt.toml against upstream's at https://github.com/lightningdevkit/ldk-node/blob/main/rustfmt.toml. If they differ, decide:

    • Option A: Adopt upstream's exact config — minimizes merge friction, but may change our current formatting
    • Option B: Keep our config but document the divergence explicitly — requires manual reformatting when pulling upstream changes

    For upstream merge hygiene, Option A is strongly recommended.

    3. Pre-commit Hook via pre-commit Framework

    Add .pre-commit-config.yaml to the repo:

    repos:
      - repo: local
        hooks:
          - id: cargo-fmt
            name: cargo fmt
            entry: cargo fmt --all --
            language: system
            types: [rust]
            pass_filenames: false

    Contributors run pre-commit install once after cloning. This catches formatting issues before they ever reach CI or review. Works well alongside AI tools since it runs as part of the git commit flow.

    4. Editor-Level Enforcement via .editorconfig and VS Code Settings

    Add .editorconfig for consistent baseline across all editors:

    root = true
    
    [*.rs]
    indent_style = tab
    indent_size = 4
    end_of_line = lf
    charset = utf-8
    trim_trailing_whitespace = true
    insert_final_newline = true
    max_line_length = 100

    And optionally add .vscode/settings.json:

    {
      "editor.formatOnSave": true,
      "rust-analyzer.rustfmt.extraArgs": [],
      "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust-analyzer"
      }
    }

    This makes VS Code (and JetBrains with the EditorConfig plugin) auto-format on save, preventing formatting drift during editing.

    5. Document the Formatting Policy in CLAUDE.md / CONTRIBUTING

    CLAUDE.md already states ALWAYS run cargo fmt before committing. Strengthening this with:

    • Pinned rustfmt version (via rust-toolchain.toml) to ensure everyone uses the same formatter version
    • Explicit note that format-only commits are expected when pulling upstream changes

    6. Pinned Rust Toolchain via rust-toolchain.toml

    Add a rust-toolchain.toml to pin the formatter version:

    [toolchain]
    channel = "stable"
    components = ["rustfmt", "clippy"]

    This ensures cargo fmt produces identical output on all machines and in CI, eliminating "it formats differently on my machine" issues.


    Recommended Implementation Order

    Priority Solution Effort Impact
    1 Dedicated fmt-check.yml + required status check Low High — blocks unformatted PRs
    2 Align rustfmt.toml with upstream Low-Medium High — eliminates upstream merge noise
    3 rust-toolchain.toml for version pinning Low Medium — consistency across environments
    4 .editorconfig + VS Code settings Low Medium — prevents drift during editing
    5 pre-commit hook Low Medium — catches issues before CI

    Note on AI Tools

    The pre-commit hook and required CI check are the most effective tools against AI-introduced formatting changes. If cargo fmt --check fails in CI, any PR with formatting changes — regardless of source — will be blocked until fixed.

  3. changed the title [-]dev: Setup coding style enforcement to match upstream formatting[/-] [+]dev: setup formatting enforcement matching upstream [/+] on Mar 2, 2026
  4. changed the title [-]dev: setup formatting enforcement matching upstream [/-] [+]dev: enforce formatting matching upstream [/+] on Mar 2, 2026
  5. changed the title [-]dev: enforce formatting matching upstream [/-] [+]dev: enforce formatting matching upstream style[/+] on Mar 2, 2026
  6. added theissue type on Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions