# ewhauser/shuck architecture

> Architecture diagram of the GitHub repository ewhauser/shuck, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ewhauser/shuck
- Repository: https://github.com/ewhauser/shuck

## Overview

1. **Repository shape**
   - This is a Rust monorepo centered on the `shuck` CLI binary, with reusable analysis crates underneath it.
   - The top-level product is a shell script linter/formatter, but the repo also contains docs generation, benchmarks, fuzzing targets, and a website that publishes rule/reference content.

2. **User-facing entry point**
   - The executable lives in `crates/shuck`, with subcommands for `check`, `format`, `clean`, and stdin-oriented formatting/checking flows.
   - That crate is the orchestration layer: it handles args/config, file discovery, cache lookup, and dispatches into parsing, analysis, linting, and formatting pipelines.

3. **Source acquisition and discovery**
   - Input comes from paths, directories, or stdin.
   - Directory runs use built-in discovery rules based on file extensions and shebangs, while respecting ignore files and skipping common vendor/cache directories.
   - This discovery boundary matters because everything downstream assumes a set of shell-script sources, not arbitrary text files.

4. **Parsing layer**
   - `crates/shuck-parser` is the front line for shell syntax. It tokenizes and parses shell scripts into syntax structures, with dedicated parsers for words, commands, redirects, heredocs, and arithmetic.
   - It also has fuzz targets and corpus-style tests, indicating the parser is treated as a core correctness boundary.
   - The parser is dialect-aware enough to support bash/sh/mksh/zsh-oriented constructs used later by semantic and lint logic.

5. **Syntax and indexing support**
   - `crates/shuck-ast` defines shared syntax data types such as AST nodes, names, spans, and tokens.
   - `crates/shuck-indexer` builds auxiliary indexes over source text: line maps, region maps, and comment indexes.
   - These indexes are used by diagnostics, suppression handling, and comment-aware formatting/lint logic to translate parser structures back to precise source locations.

6. **Semantic analysis layer**
   - `crates/shuck-semantic` builds higher-level models from parsed syntax: bindings, declarations, scopes, references, control-flow, call graph, dataflow, runtime/contract facts, and zsh option state.
   - This is the main “understanding” layer between syntax and rules. It turns shell code into analyzable program facts instead of raw ASTs.
   - Many lint rules consume these semantic facts rather than walking syntax directly, which keeps rule logic focused on meaning rather than parsing details.

7. **Lint engine and rules**
   - `crates/shuck-linter` is organized around rule families: correctness, style, performance, portability, and security.
   - The linter collects facts, applies rule selectors/rule sets, and emits violations with severity, code, and span information.
   - Suppression support is integrated, including native `shuck:` directives and ShellCheck-compatible `shellcheck disable=SCxxxx` comments, with a mapping layer for compatibility.
   - The large fixture and snapshot sets under `resources/test` and `src/rules/*/snapshots` show that each rule is validated as a small, repeatable analysis unit.

8. **Formatting pipeline**
   - `crates/shuck-formatter` is a separate subsystem from linting, with its own formatting model, context, streaming logic, comment handling, simplification passes, and output word/command formatting.
   - It consumes parsed shell structures and formatting options, then emits rewritten scripts while preserving or intentionally adjusting comments, redirects, and whitespace.
   - The formatter has oracle fixtures and dedicated tests, which suggests its correctness boundary is “textual equivalence and formatting invariants,” not just AST validity.

9. **Caching and incremental behavior**
   - `crates/shuck-cache` provides shared caching primitives used by the CLI to avoid re-linting unchanged files.
   - The README makes clear the cache is per-file, shared across runs, and stored outside the project tree by default.
   - Architecturally, cache lookup sits between discovery and analysis, allowing the tool to short-circuit expensive parse/semantic/lint work on stable inputs.

10. **Diagnostics and output**
   - Violations are converted into user-facing diagnostics with code frames or concise one-line output.
   - The output layer is part of the CLI, but the data comes from the linter’s violation model plus source indexes/spans, which is why precise span handling is a key cross-cutting concern.
   - Exit codes distinguish clean runs, lint/parse failures, and runtime/IO failures.

11. **Validation and quality gates**
   - The repo uses cargo tests, snapshot tests, fuzz targets, benchmark suites, and large-corpus conformance testing.
   - The `scripts/`, `specs/`, and `docs/rules/` directories indicate a strong documentation-and-spec boundary: rules are documented as first-class artifacts and validated against real-world shell corpora.
   - CI workflows cover test, fuzz, release, website publishing, and tag/release automation.

12. **Website and published rule docs**
   - `website/` is a separate Next.js app that renders rule documentation and general project docs from the generated rule metadata/content.
   - `website/scripts/generate-rules.ts` and the `docs/rules/*.yaml` files suggest a docs-generation flow from rule definitions into the public site.
   - This is an external-facing read model of the linter’s rules, distinct from the Rust analysis runtime.

13. **Overall data flow**
   - The main runtime flow is: discover input → read source → parse → build indexes/semantic facts → apply rules and suppressions → optionally format/fix → emit diagnostics or rewritten text.
   - The main architecture boundary is between syntactic parsing and semantic/rule analysis; the CLI orchestrates, but most real logic is isolated in reusable crates.

Main entry points: [`crates/shuck-linter`](https://github.com/ewhauser/shuck/tree/main/crates/shuck-linter) (Linter), [`crates/shuck-parser`](https://github.com/ewhauser/shuck/tree/main/crates/shuck-parser) (Parser), [`crates/shuck-indexer`](https://github.com/ewhauser/shuck/tree/main/crates/shuck-indexer) (Indexes), [`crates/shuck-formatter`](https://github.com/ewhauser/shuck/tree/main/crates/shuck-formatter) (Formatter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_runtime["Rust runtime"]
  node_node_cli["shuck CLI<br/>binary orchestrator"]
  node_node_discover["Discovery<br/>input selection<br/>[discover.rs]"]
  node_node_cache[("Cache<br/>incremental store")]
  node_node_parser["Parser<br/>syntax frontend"]
  node_node_ast["AST<br/>syntax model"]
  node_node_indexer["Indexes<br/>source maps"]
  node_node_semantic["Semantic facts<br/>analysis model"]
  node_node_linter["Linter<br/>rule engine"]
  node_node_formatter["Formatter<br/>rewrite engine"]
  node_node_output["Diagnostics<br/>reporting<br/>[check_output.rs]"]
end

subgraph group_group_quality["Quality gates"]
  node_node_fuzz["Fuzzing<br/>inputs"]
  node_node_bench["Benchmarks<br/>performance suite"]
  node_node_corpus["Corpus tests<br/>conformance data"]
  node_node_scripts["Scripts<br/>automation"]
end

subgraph group_group_docs["Docs and site"]
  node_node_specs["Specs<br/>design docs"]
  node_node_rules_docs["Rule docs<br/>generated docs"]
  node_node_site_gen["Docs generator<br/>site build step<br/>[generate-rules.ts]"]
  node_node_site["Website<br/>Next.js app"]
end

node_node_cli -->|"find inputs"| node_node_discover
node_node_discover -->|"cache probe"| node_node_cache
node_node_discover -->|"read source"| node_node_parser
node_node_cache -->|"short-circuit"| node_node_output
node_node_parser -->|"build syntax"| node_node_ast
node_node_parser -->|"source spans"| node_node_indexer
node_node_ast -->|"syntax input"| node_node_semantic
node_node_indexer -->|"location data"| node_node_semantic
node_node_semantic -->|"facts"| node_node_linter
node_node_indexer -->|"suppression spans"| node_node_linter
node_node_linter -->|"violations"| node_node_output
node_node_ast -->|"format input"| node_node_formatter
node_node_indexer -->|"comments"| node_node_formatter
node_node_formatter -->|"rewrites"| node_node_output
node_node_linter -.->|"validated by"| node_node_scripts
node_node_parser -.->|"fuzzed by"| node_node_fuzz
node_node_formatter -.->|"fuzzed by"| node_node_fuzz
node_node_linter -.->|"fixtures"| node_node_corpus
node_node_parser -.->|"conformance"| node_node_corpus
node_node_cli -.->|"guided by"| node_node_specs
node_node_linter -.->|"documents"| node_node_rules_docs
node_node_site_gen -->|"reads"| node_node_rules_docs
node_node_site -->|"builds from"| node_node_site_gen
node_node_scripts -->|"runs"| node_node_bench
node_node_scripts -.->|"publishes"| node_node_site

click node_node_cli "https://github.com/ewhauser/shuck/tree/main/crates/shuck"
click node_node_discover "https://github.com/ewhauser/shuck/blob/main/crates/shuck/src/discover.rs"
click node_node_cache "https://github.com/ewhauser/shuck/tree/main/crates/shuck-cache"
click node_node_parser "https://github.com/ewhauser/shuck/tree/main/crates/shuck-parser"
click node_node_ast "https://github.com/ewhauser/shuck/tree/main/crates/shuck-ast"
click node_node_indexer "https://github.com/ewhauser/shuck/tree/main/crates/shuck-indexer"
click node_node_semantic "https://github.com/ewhauser/shuck/tree/main/crates/shuck-semantic"
click node_node_linter "https://github.com/ewhauser/shuck/tree/main/crates/shuck-linter"
click node_node_formatter "https://github.com/ewhauser/shuck/tree/main/crates/shuck-formatter"
click node_node_output "https://github.com/ewhauser/shuck/blob/main/crates/shuck/src/commands/check_output.rs"
click node_node_fuzz "https://github.com/ewhauser/shuck/tree/main/fuzz"
click node_node_bench "https://github.com/ewhauser/shuck/tree/main/crates/shuck-benchmark"
click node_node_corpus "https://github.com/ewhauser/shuck/tree/main/crates/shuck-parser/tests/testdata/oils"
click node_node_specs "https://github.com/ewhauser/shuck/tree/main/specs"
click node_node_rules_docs "https://github.com/ewhauser/shuck/tree/main/docs/rules"
click node_node_site_gen "https://github.com/ewhauser/shuck/blob/main/website/scripts/generate-rules.ts"
click node_node_site "https://github.com/ewhauser/shuck/tree/main/website"
click node_node_scripts "https://github.com/ewhauser/shuck/tree/main/scripts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_cli,node_node_discover,node_node_cache,node_node_parser,node_node_ast,node_node_indexer,node_node_semantic,node_node_linter,node_node_formatter,node_node_output toneBlue
class node_node_fuzz,node_node_bench,node_node_corpus,node_node_scripts toneAmber
class node_node_specs,node_node_rules_docs,node_node_site_gen,node_node_site toneMint
```

## Components

### Rust runtime

Main CLI pipeline and reusable analysis crates

- [shuck CLI](https://github.com/ewhauser/shuck/tree/main/crates/shuck): binary orchestrator · Entry point for check, format, clean, stdin flows, config, and exit codes · `crates/shuck`
- [Discovery](https://github.com/ewhauser/shuck/blob/main/crates/shuck/src/discover.rs): input selection · Finds shell-script inputs from paths, directories, stdin, shebangs, extensions, and ignore rules · `crates/shuck/src/discover.rs`
- [Cache](https://github.com/ewhauser/shuck/tree/main/crates/shuck-cache): incremental store · Per-file cache used to skip unchanged analysis work · `crates/shuck-cache`
- [Parser](https://github.com/ewhauser/shuck/tree/main/crates/shuck-parser): syntax frontend · Tokenizes and parses shell syntax including words, commands, redirects, heredocs, and arithmetic · `crates/shuck-parser`
- [AST](https://github.com/ewhauser/shuck/tree/main/crates/shuck-ast): syntax model · Shared syntax types for nodes, names, spans, and tokens · `crates/shuck-ast`
- [Indexes](https://github.com/ewhauser/shuck/tree/main/crates/shuck-indexer): source maps · Line, region, and comment indexes for span resolution and comment-aware behavior · `crates/shuck-indexer`
- [Semantic facts](https://github.com/ewhauser/shuck/tree/main/crates/shuck-semantic): analysis model · Builds bindings, scopes, references, control flow, call graph, dataflow, and runtime facts · `crates/shuck-semantic`
- [Linter](https://github.com/ewhauser/shuck/tree/main/crates/shuck-linter): rule engine · Applies rule sets, suppressions, and emits violations across correctness, style, performance, portability, and security · `crates/shuck-linter`
- [Formatter](https://github.com/ewhauser/shuck/tree/main/crates/shuck-formatter): rewrite engine · Formats shell scripts with its own context, simplification, streaming, and comment handling pipeline · `crates/shuck-formatter`
- [Diagnostics](https://github.com/ewhauser/shuck/blob/main/crates/shuck/src/commands/check_output.rs): reporting · Turns violations and spans into code frames, one-line output, and exit codes · `crates/shuck/src/commands/check_output.rs`

### Quality gates

Tests, fuzzing, benchmarks, and corpus validation

- [Fuzzing](https://github.com/ewhauser/shuck/tree/main/fuzz): inputs · Parser, lexer, arithmetic, formatter, and linter no-panic fuzz targets · `fuzz`
- [Benchmarks](https://github.com/ewhauser/shuck/tree/main/crates/shuck-benchmark): performance suite · Criterion-style benchmarks for lexer, parser, semantic, linter, formatter, and hot paths · `crates/shuck-benchmark`
- [Corpus tests](https://github.com/ewhauser/shuck/tree/main/crates/shuck-parser/tests/testdata/oils): conformance data · Large shell corpora, oracle fixtures, snapshots, and conformance metadata · `crates/shuck-parser/tests/testdata/oils`
- [Scripts](https://github.com/ewhauser/shuck/tree/main/scripts): automation · CI-adjacent scripts for corpus download, fuzz setup, benchmarking, profiling, and release automation · `scripts`

### Docs and site

Rule docs generation and published website

- [Specs](https://github.com/ewhauser/shuck/tree/main/specs): design docs · Architecture and behavior specs for parser, indexer, semantic model, linter, suppressions, formatter, run flow, and zsh support · `specs`
- [Rule docs](https://github.com/ewhauser/shuck/tree/main/docs/rules): generated docs · YAML rule references consumed by documentation and validation tooling · `docs/rules`
- [Docs generator](https://github.com/ewhauser/shuck/blob/main/website/scripts/generate-rules.ts): site build step · Generates website rule content from rule metadata · `website/scripts/generate-rules.ts`
- [Website](https://github.com/ewhauser/shuck/tree/main/website): Next.js app · Publishes rule reference content and project documentation · `website`

## Connections

- shuck CLI → Discovery: find inputs · Selects files and stdin sources before analysis
- Discovery → Cache: cache probe · Checks whether a file can be skipped
- Discovery → Parser: read source · Feeds shell text into parsing
- Cache → Diagnostics: short-circuit · Avoids reprocessing unchanged files
- Parser → AST: build syntax · Produces shared syntax structures
- Parser → Indexes: source spans · Provides text positions for indexing
- AST → Semantic facts: syntax input · Semantic analysis consumes AST structures
- Indexes → Semantic facts: location data · Supports comments, regions, and precise spans
- Semantic facts → Linter: facts · Rules consume semantic models rather than raw syntax
- Indexes → Linter: suppression spans · Supports directives and precise diagnostics
- Linter → Diagnostics: violations · Converts rule findings into user-facing diagnostics
- AST → Formatter: format input · Formatter operates on parsed shell structures
- Indexes → Formatter: comments · Preserves and repositions comments and whitespace
- Formatter → Diagnostics: rewrites · Emits formatted text for stdout or files
- Linter → Scripts: validated by · Rules are exercised through corpus and automation scripts
- Parser → Fuzzing: fuzzed by · Parser and lexer are major fuzz targets
- Formatter → Fuzzing: fuzzed by · Formatter consistency and validity are fuzzed
- Linter → Corpus tests: fixtures · Rule families are validated by snapshots and corpora
- Parser → Corpus tests: conformance · Parser is checked against large external corpora
- shuck CLI → Specs: guided by · Runtime behavior is documented by architectural specs
- Linter → Rule docs: documents · Rule metadata is published as YAML reference docs
- Docs generator → Rule docs: reads · Generates site content from rule documents
- Website → Docs generator: builds from · Website includes generated rule content
- Scripts → Benchmarks: runs · Automation drives benchmark and profiling workflows
- Scripts → Website: publishes · Supports website deployment and release automation
