# golang/tools architecture

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

- Interactive diagram: https://gitdiagram.com/golang/tools
- Repository: https://github.com/golang/tools

## Overview

##### Project type and purpose
- This is a monorepo module (“golang.org/x/tools”) containing both:
a. A suite of CLI tools under cmd/ (e.g. goimports, callgraph, stringer, digraph, etc.).
b. A collection of Go libraries and frameworks for static analysis (under go/), plus the gopls LSP server module (under gopls/).
- Purpose: provide reusable analysis libraries (AST, SSA IR, call graphs, CFG, go/packages, go/analysis framework) and deliver end-user tools and an LSP server to editors.

##### High-level architecture components
a. CLI tools layer (cmd/)
- Each command is a small “main” program that imports the shared libraries.
- Examples: cmd/goimports → uses go/analysis/… and go/ast/astutil; cmd/callgraph → uses go/callgraph; cmd/stringer → uses go/types.
b. Library layer (go/*)
- go/ast, go/ast/inspector
- go/packages (loading/parsing/type-checking)
- go/analysis (framework for analyzers)
- go/ssa (intermediate representation)
- go/callgraph, go/cfg
- gcexportdata/gccgoexportdata (compiler export formats)
c. LSP server (gopls/)
- Protocol layer: JSON-RPC over StdIO/HTTP (gopls/internal/lsprpc + protocol definitions).
- Server core: command dispatchers (gopls/internal/cmd), workspace management, snapshot/caching (gopls/internal/cache & gopls/internal/work), diagnostics pipeline.
- Language analysis back-end: invokes go/packages, go/analysis passes, SSA, call graphs, etc., to compute completions, diagnostics, refactorings.
- Integration with editors: clients send LSP requests → gopls translates to internal commands → analysis libraries → response back via protocol.
d. Support subsystems
- File watching and overlays (gopls/internal/filewatcher)
- Caching/memoization (gopls/internal/cache)
- Telemetry, progress reporting, logging (gopls/internal/telemetry, gopls/internal/debug)
- Utilities (internal/, imports/, container/, etc.) used by both CLI tools and gopls.

##### Relationships & data flow
- Editors/clients ↔ JSON-RPC ↔ gopls LSP protocol layer
- Protocol layer → dispatch to server core commands → workspace state → analysis pipeline → library APIs (go/packages, go/analysis, go/ssa) → results → protocol layer → back to editor
- CLI tools → import libraries directly → run analysis on disk files → print to stdout

##### Key architectural patterns & design principles
- Layered design: presentation (CLI or LSP), server core, analysis libraries, utility packages
- Modular analyzers: go/analysis framework supports pluggable “passes”
- Caching layer for expensive operations (parsing, type-checking) using snapshots
- Decoupling between protocol I/O and analysis logic
- Reuse of common primitives (AST utils, graph algorithms) across all tools

## Diagram (Mermaid)

```mermaid
graph TB
    %% External entity
    EditorClient["Editor/Client"]:::external

    %% LSP Server Layer
    subgraph "LSP Server"
        direction TB
        subgraph "Protocol Layer"
            LSPRPC["JSON-RPC (gopls/internal/lsprpc)"]:::protocol
            ProtocolDefs["Protocol Definitions"]:::protocol
        end
        subgraph "Server Core"
            CmdDispatchers["Command Dispatchers"]:::protocol
            ServerCore["Workspace & Dispatch"]:::protocol
            MainEntry["gopls Main"]:::protocol
        end
        subgraph "Caching & Snapshots"
            SnapshotManager["Snapshot Manager"]:::protocol
            CacheCore["Cache Core"]:::protocol
        end
        subgraph "Diagnostics & Work"
            WorkPipeline["Diagnostics Pipeline"]:::protocol
        end
    end

    %% Support Subsystems
    subgraph "Support Subsystems"
        direction TB
        FileWatcher["File Watcher & Overlay"]:::protocol
        Telemetry["Telemetry"]:::util
        Logging["Debug/Log"]:::util
        Progress["Progress Reporting"]:::util
    end

    %% Library Layer
    subgraph "Analysis Libraries"
        direction TB
        AstUtil["AST Utilities"]:::library
        Inspector["Inspector"]:::library
        PackagesLoader["Package Loader"]:::library
        AnalysisFramework["Analysis Framework"]:::library
        SSA["SSA IR"]:::library
        CallGraph["Call Graph"]:::library
        CFG["Control Flow Graph"]:::library
        GCCGO["GCCGO Export Data"]:::library
    end

    %% CLI Tools Layer
    subgraph "CLI Tools"
        direction TB
        goimports["cmd/goimports"]:::cli
        callgraphCmd["cmd/callgraph"]:::cli
        stringerCmd["cmd/stringer"]:::cli
    end

    %% Utility Packages
    subgraph "Utility Packages"
        direction TB
        Imports["imports"]:::util
        Container["container/intsets"]:::util
        Util["immutable utils"]:::util
    end

    %% Relationships
    EditorClient -->|"JSON-RPC request/response"| LSPRPC
    LSPRPC -->|dispatch| CmdDispatchers
    CmdDispatchers -->|init workspace| ServerCore
    ServerCore -->|watch/files| FileWatcher
    ServerCore -->|cache snapshots| SnapshotManager
    SnapshotManager --> CacheCore
    ServerCore -->|run diagnostics| WorkPipeline
    WorkPipeline -->|analysis calls| AnalysisFramework
    AnalysisFramework --> AstUtil
    AnalysisFramework --> Inspector
    AnalysisFramework --> PackagesLoader
    AnalysisFramework --> SSA
    AnalysisFramework --> CallGraph
    AnalysisFramework --> CFG
    AnalysisFramework --> GCCGO
    WorkPipeline -->|results| ServerCore
    ServerCore -->|JSON-RPC response| LSPRPC
    LSPRPC -->|send| EditorClient

    goimports -->|uses| AstUtil
    goimports -->|uses| AnalysisFramework
    callgraphCmd -->|uses| CallGraph
    stringerCmd -->|uses| PackagesLoader
    goimports -->|stdout| goimports
    callgraphCmd -->|stdout| callgraphCmd
    stringerCmd -->|stdout| stringerCmd

    ServerCore -->|telemetry| Telemetry
    ServerCore -->|logging| Logging
    ServerCore -->|progress| Progress

    goimports -->|util| Imports
    callgraphCmd -->|util| Container
    ServerCore -->|util| Util

    %% Click Events - CLI Tools
    click goimports "https://github.com/golang/tools/blob/master/cmd/goimports/goimports.go"
    click callgraphCmd "https://github.com/golang/tools/blob/master/cmd/callgraph/main.go"
    click stringerCmd "https://github.com/golang/tools/blob/master/cmd/stringer/stringer.go"

    %% Click Events - Libraries
    click AstUtil "https://github.com/golang/tools/tree/master/go/ast/astutil/"
    click Inspector "https://github.com/golang/tools/blob/master/go/ast/inspector/inspector.go"
    click PackagesLoader "https://github.com/golang/tools/blob/master/go/packages/packages.go"
    click AnalysisFramework "https://github.com/golang/tools/blob/master/go/analysis/analysis.go"
    click SSA "https://github.com/golang/tools/blob/master/go/ssa/builder.go"
    click CallGraph "https://github.com/golang/tools/blob/master/go/callgraph/callgraph.go"
    click CFG "https://github.com/golang/tools/blob/master/go/cfg/cfg.go"
    click GCCGO "https://github.com/golang/tools/blob/master/go/gccgoexportdata/gccgoexportdata.go"

    %% Click Events - LSP Server
    click LSPRPC "https://github.com/golang/tools/blob/master/gopls/internal/lsprpc/lsprpc.go"
    click ProtocolDefs "https://github.com/golang/tools/blob/master/gopls/internal/protocol/protocol.go"
    click CmdDispatchers "https://github.com/golang/tools/blob/master/gopls/internal/cmd/cmd.go"
    click ServerCore "https://github.com/golang/tools/blob/master/gopls/internal/server/server.go"
    click SnapshotManager "https://github.com/golang/tools/blob/master/gopls/internal/cache/snapshot.go"
    click CacheCore "https://github.com/golang/tools/blob/master/gopls/internal/cache/cache.go"
    click WorkPipeline "https://github.com/golang/tools/blob/master/gopls/internal/work/diagnostics.go"
    click MainEntry "https://github.com/golang/tools/blob/master/gopls/main.go"

    %% Click Events - Support
    click FileWatcher "https://github.com/golang/tools/blob/master/gopls/internal/filewatcher/filewatcher.go"
    click Telemetry "https://github.com/golang/tools/blob/master/gopls/internal/telemetry/telemetry_test.go"
    click Logging "https://github.com/golang/tools/blob/master/gopls/internal/debug/log/log.go"
    click Progress "https://github.com/golang/tools/blob/master/gopls/internal/progress/progress.go"

    %% Click Events - Utility
    click Imports "https://github.com/golang/tools/blob/master/internal/imports/imports.go"
    click Container "https://github.com/golang/tools/blob/master/container/intsets/sparse.go"
    click Util "https://github.com/golang/tools/blob/master/gopls/internal/util/immutable/immutable.go"

    %% Styles
    classDef protocol fill:#AED6F1,stroke:#1F618D,color:#1B2631
    classDef library fill:#A9DFBF,stroke:#196F3D,color:#145A32
    classDef cli fill:#F5B041,stroke:#B9770E,color:#7E5109
    classDef util fill:#D5D8DC,stroke:#566573,color:#1B2631
    classDef external fill:#FFFFFF,stroke:#000000,color:#000000
```
