projectgraph

package
v0.0.0-...-344772a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 19 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package projectgraph links the file indexes of a project into one call graph. The link step reads only fileindex records and the manifests of the project. It imports no tree-sitter package, so it builds with no CGO.

Index

Constants

View Source
const (
	RootMain           = roots.Main
	RootManifest       = roots.Manifest
	RootFramework      = roots.Framework
	RootTest           = roots.Test
	RootLibrarySurface = roots.LibrarySurface
)
View Source
const Version = "20"

Version is the version of the link rules. Bump it when a rule that makes a node, an edge, a root or an unresolved site changes, so a diff of two reports shows the change.

Variables

This section is empty.

Functions

func MethodAliases

func MethodAliases(method string) []string

MethodAliases returns the names of the methods that a call of the method name can run by another name, as from for into of Rust. The reach query does not read the language of a site, so it takes the aliases of every language. Without them, a call of into whose From impl calls a target concerns only the targets that a method into reaches, and the target reads as not-found.

func ReachesAny

func ReachesAny(reason fileindex.UnresolvedReason) bool

ReachesAny reports whether an unresolved site of the reason can call any target. A computed name or a computed module can name every target, even when the site names the function that it calls, as import_module.

Types

type Confidence

type Confidence string
const (
	ConfidenceHigh   Confidence = "high"
	ConfidenceMedium Confidence = "medium"
	ConfidenceLow    Confidence = "low"
)

func (Confidence) Rank

func (c Confidence) Rank() int

Rank orders the confidences, low first. An unknown confidence ranks 0.

type Edge

type Edge struct {
	From       string     `json:"from"`
	To         string     `json:"to"`
	Site       Site       `json:"site"`
	Kind       EdgeKind   `json:"kind"`
	Confidence Confidence `json:"confidence"`
	Resolver   string     `json:"resolver"`
}

Edge is one resolved step from a caller to a callee. Resolver names the rule that made the edge, as python.import.

type EdgeKind

type EdgeKind string
const (
	EdgeDirect     EdgeKind = "direct"
	EdgeImport     EdgeKind = "import"
	EdgeExport     EdgeKind = "export"
	EdgeMember     EdgeKind = "member"
	EdgeAssignment EdgeKind = "assignment"
	EdgeCHA        EdgeKind = "cha"
	EdgeField      EdgeKind = "field"
	EdgeWildcard   EdgeKind = "wildcard"
	EdgeLLM        EdgeKind = "llm"
	// EdgeName joins the caller of a call with an unknown receiver to each
	// first-party definition with the name of the called method.
	EdgeName EdgeKind = "name"
	// EdgeReference joins a definition to a function, a method, a class or
	// an import that it uses as a value. The code that gets the value can
	// call it.
	EdgeReference EdgeKind = "reference"
	// EdgePackage joins an external key, as lodash//template, to the
	// definition that a unit of dependency code gives for it. It is no
	// call. It marks where a path leaves the code of its caller and goes
	// on into a package.
	EdgePackage EdgeKind = "package"
)

func (EdgeKind) Confidence

func (k EdgeKind) Confidence() Confidence

Confidence returns the confidence that an edge of the kind carries.

type Graph

type Graph struct {
	Nodes      map[string]*Node                `json:"nodes"`
	Edges      []Edge                          `json:"edges"`
	Unresolved []UnresolvedSite                `json:"unresolved"`
	Roots      []Root                          `json:"roots"`
	Files      map[string]fileindex.FileStatus `json:"files"`
	Truncated  []Truncation                    `json:"truncated,omitempty"`
	Problems   []modules.Problem               `json:"problems,omitempty"`
	// Imports holds, by language family, the module keys that the files
	// of a family with a site of a Language scope import from outside the
	// project, as jsonschema. Such a site can run a name of such a module
	// that no edge names, as a function that only the markup of a
	// component calls.
	Imports map[string][]string `json:"imports,omitempty"`
	Stats   Stats               `json:"stats"`
	// contains filtered or unexported fields
}

Graph is the call graph of one project. Edges, Unresolved, Roots and Truncated are in a fixed order. Problems lists the manifests that the link step could not read.

func Build

func Build(ctx context.Context, indexes []*fileindex.Index, opts Options) (*Graph, error)

Build links the indexes of one project into a graph. Every index must have the Version of this build, and no two indexes may share a path. The graph does not depend on the order of the indexes.

An index with a Unit is a file of dependency code. The link step resolves the imports of a unit inside the unit, as a project of its own. An import that no file of its own project or unit gives stays an external node, as lodash//template, and a package edge joins that node to the definition of each unit that gives it. Dependency code holds no root.

func (*Graph) AddEdge

func (g *Graph) AddEdge(e Edge) (bool, error)

AddEdge adds an edge to a built graph with the rule of the link step: the graph keeps one edge per caller, callee and site, and the edge of the higher confidence. It sets the confidence from the kind. It reports whether the graph changed. It never removes an unresolved site, so an edge from another source, as an LLM overlay, adds a path and leaves the uncertainty of the site. The caller must be a node of the graph, and a callee outside the project gets an external node.

func (*Graph) CallbackMethod

func (g *Graph) CallbackMethod(key string) bool

CallbackMethod reports whether code outside the graph that a Callback record stands for can call the definition of the key back, on a value that it holds, as a Go method of an exported name.

func (*Graph) IsExternal

func (g *Graph) IsExternal(key string) bool

IsExternal reports whether the key names a node outside the project.

func (*Graph) LoadGroup

func (g *Graph) LoadGroup(path string) string

LoadGroup returns the key of the files whose module top levels the program runs before the code of a file runs: the directory of the file, in a language whose program loads a whole package, as Go, and else the file alone.

func (*Graph) Runs

func (g *Graph) Runs(path string) bool

Runs reports whether the program of the graph runs a file, by the language of the file. A file of the project runs when a root of the graph is in a file of its language. A file of a unit of dependency code runs when the ecosystem of the unit installs code of its language, as pypi installs Python. A file with no known language runs, and so does every file of a graph with no root. A truncated or a failed file that does not run, as the JavaScript of a web page that a Python project ships, holds no caller that a search can reach, so its budget concerns no target.

type Node

type Node struct {
	Key      string           `json:"key"`
	Kind     NodeKind         `json:"kind"`
	Path     string           `json:"path,omitempty"`
	Line     int              `json:"line,omitempty"`
	Exported bool             `json:"exported,omitempty"`
	Test     bool             `json:"test,omitempty"`
	Language langcode.Code    `json:"language,omitempty"`
	Flags    []fileindex.Flag `json:"flags,omitempty"`
	External string           `json:"external,omitempty"`
}

Node is a definition, a module top level, a re-exported name or an external target. An external node has only its key and kind. External is the key that an importer of the package writes for a function, a method or a constructor of a unit of dependency code, as golang.org//x//net//html//render1. Language is the language of the file of a module top level, also of a file with no index, when the index step told it.

type NodeKind

type NodeKind string
const (
	NodeModule      NodeKind = "module"
	NodeFunction    NodeKind = "function"
	NodeMethod      NodeKind = "method"
	NodeConstructor NodeKind = "constructor"
	NodeClass       NodeKind = "class"
	// NodeInterface is a TypeScript interface. It has no code. A declared
	// type names it, and the classes that implement it hold the methods.
	NodeInterface NodeKind = "interface"
	// NodeVariable is a name that the top level of a module assigns. A walk
	// through it goes on to the value that it holds.
	NodeVariable NodeKind = "variable"
	// NodeExport is a name that a file imports and gives on to its own
	// importers, as a barrel file or an __init__.py does, or a type alias,
	// which gives on the type that it names. The node keeps the barrel on a
	// path.
	NodeExport   NodeKind = "export"
	NodeExternal NodeKind = "external"
)

type Options

type Options struct {
	// Root holds the manifests of the project, as pyproject.toml. A nil
	// Root reads no manifest.
	Root fs.FS
	// SysPath lists more Python import roots, relative to the project root.
	SysPath []string
	// Files is the status of each file that the index step reports. A path
	// with no index and a status that is not complete, as missing or
	// failed, becomes a module node with that status.
	Files map[string]fileindex.FileStatus
	// Languages is the language of each file of the index step, by path,
	// as indexer.Result.Languages gives it. A file with no index gets its
	// language from here. The reach query leaves out the budget of a file
	// of a language that the program does not run, as the JavaScript of a
	// web page in a Python project. A nil map leaves the language of such
	// a file unknown, and its budget counts.
	Languages map[string]langcode.Code
	// TestPaths marks the test files. A glob with no "/" matches one part
	// of the path, as tests or test_*.py.
	TestPaths []string
	// Rules go over the embedded root rules by id.
	Rules []roots.Rule
	// Units holds the files of each package unit of dependency code, by
	// unit, as Root holds the files of the project. The root of each one is
	// the directory that the paths inside the package start from, as
	// node_modules/lodash for npm:lodash@4.17.21//template.js. A unit with
	// no entry reads no manifest.
	Units map[string]fs.FS
	// Surface links an external node to each exported symbol of each unit
	// of dependency code, as an import of the package by its name would. A
	// search can then start from the symbols of a package, as a summary of
	// the package does.
	Surface bool
	// MaxEdges caps the edges of the graph. Past it the link step adds no
	// edge and records a truncation. Zero selects 5,000,000.
	MaxEdges int
	// MaxExportHops caps the hops of a walk through re-exports, bases and
	// the modules of a Go package. Zero selects 10.
	MaxExportHops int
}

Options configures the link step.

type Root

type Root struct {
	Key  string   `json:"key"`
	Kind RootKind `json:"kind"`
	Rule string   `json:"rule"`
	Site Site     `json:"site"`
}

Root is a node that runs with no caller in the graph. Rule is the id of the root rule that marked it.

type RootKind

type RootKind = roots.Kind

RootKind is the kind of a root, as the root rules name it.

type Site

type Site struct {
	Path   string `json:"path"`
	Line   int    `json:"line,omitempty"`
	Column int    `json:"column,omitempty"`
}

Site is a place in a first-party file. Lines and columns are 1-based, and 0 means not known.

func (Site) String

func (s Site) String() string

String writes the site as path:line, or as the path alone for a site with no line.

type Stats

type Stats struct {
	Version      string `json:"version"`
	Files        int    `json:"files"`
	Nodes        int    `json:"nodes"`
	Edges        int    `json:"edges"`
	Unresolved   int    `json:"unresolved"`
	Roots        int    `json:"roots"`
	RootFallback bool   `json:"root_fallback,omitempty"`
	// Languages are the languages of the files that hold a root other than
	// a test root, in a fixed order: the languages that the program runs.
	// Runs reads it.
	Languages []langcode.Code `json:"languages,omitempty"`
	Duration  time.Duration   `json:"-"`
}

Stats holds the counts of a graph. Duration is not in the JSON form, so two builds of one input give one JSON form.

type Truncation

type Truncation struct {
	Kind TruncationKind `json:"kind"`
	Key  string         `json:"key,omitempty"`
	Site Site           `json:"site"`
}

Truncation records a limit of the link step. Key is the node where the limit hit, and is empty when the limit holds for the whole graph.

type TruncationKind

type TruncationKind string

TruncationKind names the limit of the link step that a Truncation records.

const (
	// TruncationEdges marks a graph that stopped adding edges at the edge
	// limit. Every part of the graph can miss an edge.
	TruncationEdges TruncationKind = "edges"
	// TruncationExportHops marks a name whose chain of re-exports is longer
	// than the hop limit.
	TruncationExportHops TruncationKind = "export-hops"
	// TruncationPackageClauses marks a Go file whose package clause is not
	// the clause of the package of its directory. The link step does not
	// read build tags, so an import of the directory does not reach it.
	TruncationPackageClauses TruncationKind = "package-clauses"
)

type UnresolvedSite

type UnresolvedSite struct {
	Caller       string                     `json:"caller"`
	Site         Site                       `json:"site"`
	Text         string                     `json:"text"`
	Receiver     string                     `json:"receiver,omitempty"`
	Method       string                     `json:"method,omitempty"`
	Reason       fileindex.UnresolvedReason `json:"reason"`
	ImportPrefix string                     `json:"import_prefix,omitempty"`
	Unit         string                     `json:"unit,omitempty"`
	Callback     bool                       `json:"callback,omitempty"`
	Language     langcode.Code              `json:"language,omitempty"`
}

UnresolvedSite is a call site that neither the index nor the link step resolved. ImportPrefix is the key form of the static prefix of a computed import, so the reach query can set aside a site that cannot name a target. Unit names the package unit that a site enters at no known definition, as a name that the package makes at run time, or a module that the package does not have. The site of an external key has the key as its caller and the unit as its site. The code that runs there is code of the unit, so it can reach only what the code of the unit can reach. A site with no caller is a unit that no import names, and it holds for every search. Callback marks the site of an external key whose code can call into the graph only through a method of a value that it holds, as the Go standard library with no unit. The reach query applies it to the targets that a method of an exported name reaches. Language scopes a site to the targets that code of the language family calls, as a value of JavaScript that a framework calls, which runs only JavaScript code.

Directories

Path Synopsis
Package modules maps an import, as the source writes it, to a file of the project.
Package modules maps an import, as the source writes it, to a file of the project.
Package overlay reads a file of call edges that a model proposed offline and merges them into a project graph.
Package overlay reads a file of call edges that a model proposed offline and merges them into a project graph.
Package reach answers which targets a project graph reaches from its roots.
Package reach answers which targets a project graph reaches from its roots.
Package rootmatch finds the roots of a project graph: the definitions and the module top levels that the root rules mark.
Package rootmatch finds the roots of a project graph: the definitions and the module top levels that the root rules mark.
Package summary gives the summary of one package version of dependency code: for each exported symbol of the package, the external symbols of its own dependencies that the symbol reaches, and the uncertainties of the search.
Package summary gives the summary of one package version of dependency code: for each exported symbol of the package, the external symbols of its own dependencies that the symbol reaches, and the uncertainties of the search.
summarytest
Package summarytest checks a summary against the reach query of its unit graph.
Package summarytest checks a summary against the reach query of its unit graph.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL