Skip to content

file_starts_with

Byte-level prefix / suffix check. Works on any bytes (binary safe, unlike file_header).

Check-only: a fix would risk silently duplicating a near-matching prefix. Pair with file_prepend / file_append explicitly if you want auto-repair.

Binary files are checked too. Unlike the text rules, which skip binary-looking files, these two read the raw bytes of every file in scope, so they can assert a file signature on images or archives. Scope paths to the files you mean: a text prefix on **/* also fails every binary file, and a paired file_prepend / file_append refuses to edit a binary file, so those findings stay unfixed.

When to use it: when the exact bytes matter more than the text. Typical prefixes are a shebang on scripts (prefix: "#!" on **/*.sh), a file signature or magic number, and a fixed licence or generated-file banner. Typical suffixes are a generator's closing sentinel, so a hand-edited or truncated output fails, and a mandatory trailer line.

Byte-for-byte means newlines too: a suffix ending in \n requires the file's final newline, and one without it fails on a file that has one. An empty file fails any non-empty prefix or suffix. For a pattern rather than fixed bytes, use file_header / file_footer, which match a regex against the first or last lines; for "ends with a newline" alone, final_newline is the dedicated check.

OptionTypeRequiredDefaultDescription
prefixstringyesRequired prefix, matched byte-for-byte.

Plus the common paths, level, id, when, and expect_matches fields. This table is generated from the JSON Schema; option types and defaults are authoritative.

Files missing the required SPDX prefix

Section titled "Files missing the required SPDX prefix"

The rule fires on this repository:

  • src/
  • src/no_header.rs

    ✗ error spdx-header

    1:1 file does not start with the required prefix

  • src/ok.rs✓
  • src/wrong_spdx.rs

    ✗ error spdx-header

    1:1 file does not start with the required prefix

src/no_header.rs
fn x() {}
src/ok.rs
// SPDX-License-Identifier: MIT
fn main() {}
src/wrong_spdx.rs
// SPDX-License-Identifier: GPL-3.0
fn y() {}

With this .alint.yml:

version: 1
rules:
- id: spdx-header
kind: file_starts_with
paths: "src/**/*.rs"
prefix: "// SPDX-License-Identifier: MIT\n"
level: error

alint check reports:

Terminal window
--- src/no_header.rs -----------------------------------------------------------
x error spdx-header
1:1 file does not start with the required prefix
--- src/wrong_spdx.rs ----------------------------------------------------------
x error spdx-header
1:1 file does not start with the required prefix
Summary (2 violations):
x 2 errors
0 passing * 1 failing

Every file begins with the SPDX prefix

Section titled "Every file begins with the SPDX prefix"

This repository is compliant:

  • src/
  • src/a.rs✓
  • src/b.rs✓
src/a.rs
// SPDX-License-Identifier: MIT
fn main() {}
src/b.rs
// SPDX-License-Identifier: MIT
fn x() {}

With this .alint.yml:

version: 1
rules:
- id: spdx-header
kind: file_starts_with
paths: "src/**/*.rs"
prefix: "// SPDX-License-Identifier: MIT\n"
level: error

alint check reports:

Terminal window
v All 1 rule(s) passed.