# uber-go/fx architecture

> Architecture diagram of the GitHub repository uber-go/fx, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/uber-go/fx
- Repository: https://github.com/uber-go/fx

## Overview

##### Main Components
- Core DI Container and Module Registration: From key files such as app.go, module.go, provide.go, and supply.go. This is where application components get wired together.
- Lifecycle Management: Represented by files like lifecycle.go and its internal counterpart (internal/lifecycle), showing how Fx controls initialization, startup, and shutdown of components.
- Event & Logging Infrastructure: The fxevent and fxlog packages (and their tests) form the backbone of event management and logging within the system.
- Testing and Utilities: Highlight the fxtest packages and internal test utilities (internal/testutil). They showcase how Fx enables robust test setups for applications built using this DI framework.
- Documentation & Examples: The docs/ directory along with numerous examples (e.g., docs/ex/get-started/...) illustrate various use cases. Though not part of the runtime architecture, they are key in understanding how the DI and lifecycle components are used.
- Tooling and Static Analysis: The tools/ directory (including tools/analysis and tools/cmd/fxlint) shows that Fx also provides support tools to analyze and enforce proper usage of its APIs.

##### Relationships and Interactions
- That the “DI Container” feeds into the “Lifecycle Manager,” which in turn coordinates component startup/shutdown.
- That as components initialize, the “Event & Logging” modules (fxevent, fxlog) capture and log events, making the system observable.
- Supporting interactions from “Testing Utilities” to the core modules—demonstrating how fxtest and internal packages facilitate robust testing.
- Optionally, include “Tooling” that interacts with source code (via static analysis and linting) to ensure correctness and best practices.

##### Architectural Patterns and Principles
- Separation of Concerns: Clearly split the system into its DI engine, lifecycle management, logging, testing, and documentation tools.
- Modularity: Emphasize how separate packages (e.g., fxevent, fxlog, internal modules) cooperate through well-defined interfaces.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Application Layer"
        A("Application Code"):::app
    end

    subgraph "Core Fx Components"
        DI("DI Container & Module Registration"):::core
        LC("Lifecycle Manager"):::core
        EL("Event & Logging System"):::core
    end

    subgraph "Supporting Tools"
        TU("Testing Utilities"):::test
        DOC("Documentation & Examples"):::doc
        TA("Tooling & Static Analysis"):::tool
    end

    A -->|"uses"| DI
    DI -->|"initiates"| LC
    LC -->|"triggers"| EL

    TU -.->|"tests"| DI
    TU -.->|"tests"| LC

    TA -.->|"analyzes"| DI
    TA -.->|"analyzes"| LC
    TA -.->|"monitors"| EL

    A -.->|"refersTo"| DOC

    click DI "https://github.com/uber-go/fx/blob/master/app.go"
    click DI "https://github.com/uber-go/fx/blob/master/module.go"
    click DI "https://github.com/uber-go/fx/blob/master/provide.go"
    click DI "https://github.com/uber-go/fx/blob/master/supply.go"
    
    click LC "https://github.com/uber-go/fx/blob/master/lifecycle.go"
    click LC "https://github.com/uber-go/fx/blob/master/internal/lifecycle/lifecycle.go"
    
    click EL "https://github.com/uber-go/fx/tree/master/fxevent/"
    click EL "https://github.com/uber-go/fx/tree/master/internal/fxlog/"
    
    click TU "https://github.com/uber-go/fx/tree/master/fxtest/"
    click TU "https://github.com/uber-go/fx/tree/master/internal/testutil/"
    
    click DOC "https://github.com/uber-go/fx/tree/master/docs/"
    
    click TA "https://github.com/uber-go/fx/tree/master/tools/"

    classDef app fill:#FFECB3,stroke:#E65100,stroke-width:2px;
    classDef core fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef test fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef doc fill:#D1C4E9,stroke:#6A1B9A,stroke-width:2px;
    classDef tool fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
```
