package modfile

Import Path
	golang.org/x/mod/modfile (on go.dev)

Dependency Relation
	imports 14 packages, and imported by 4 packages

Involved Source Files print.go read.go Package modfile implements a parser and formatter for go.mod files. The go.mod syntax is described in https://pkg.go.dev/cmd/go/#hdr-The_go_mod_file. The [Parse] and [ParseLax] functions both parse a go.mod file and return an abstract syntax tree. ParseLax ignores unknown statements and may be used to parse go.mod files that may have been developed with newer versions of Go. The [File] struct returned by Parse and ParseLax represent an abstract go.mod file. File has several methods like [File.AddNewRequire] and [File.DropReplace] that can be used to programmatically edit a file. The [Format] function formats a File back to a byte slice which can be written to a file. work.go
Package-Level Type Names (total 31, in which 27 are exported)
/* sort exporteds by: | */
A Comment represents a single // comment. Start Position // an end of line (not whole line) comment // without trailing newline
A CommentBlock represents a top-level block of comments separate from any rule. Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression Start Position Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*CommentBlock) Span() (start, end Position) *CommentBlock : Expr
Comments collects the comments associated with an expression. For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. func (*Comments).Comment() *Comments func Expr.Comment() *Comments
Err error Filename string ModPath string Pos Position Verb string (*Error) Error() string (*Error) Unwrap() error *Error : error
( ErrorList) Error() string ErrorList : error
An Exclude is a single exclude statement. Mod module.Version Syntax *Line
An Expr represents an input element. Comment returns the comments attached to the expression. This method would normally be named 'Comments' but that would interfere with embedding a type of the same name. Span returns the start and end position of the expression, excluding leading or trailing comments. *CommentBlock *FileSyntax *Line *LineBlock *LParen *RParen
A File is the parsed, interpreted form of a go.mod file. Exclude []*Exclude Go *Go Godebug []*Godebug Ignore []*Ignore Module *Module Replace []*Replace Require []*Require Retract []*Retract Syntax *FileSyntax Tool []*Tool Toolchain *Toolchain (*File) AddComment(text string) AddExclude adds an exclude statement to the mod file. Errors if the provided version is not a canonical version string (*File) AddGoStmt(version string) error AddGodebug sets the first godebug line for key to value, preserving any existing comments for that line and removing all other godebug lines for key. If no line currently exists for key, AddGodebug adds a new line at the end of the last godebug block. AddIgnore adds a new ignore directive with the given path. It does nothing if the ignore line already exists. (*File) AddModuleStmt(path string) error AddNewRequire adds a new require line for path at version vers at the end of the last require block, regardless of any existing require lines for path. (*File) AddReplace(oldPath, oldVers, newPath, newVers string) error AddRequire sets the first require line for path to version vers, preserving any existing comments for that line and removing all other lines for path. If no line currently exists for path, AddRequire adds a new line at the end of the last require block. AddRetract adds a retract statement to the mod file. Errors if the provided version interval does not consist of canonical version strings AddTool adds a new tool directive with the given path. It does nothing if the tool line already exists. (*File) AddToolchainStmt(name string) error Cleanup cleans up the file f after any edit operations. To avoid quadratic behavior, modifications like [File.DropRequire] clear the entry but do not remove it from the slice. Cleanup cleans out all the cleared entries. (*File) DropExclude(path, vers string) error DropGoStmt deletes the go statement from the file. (*File) DropGodebug(key string) error DropIgnore removes an ignore directive with the given path. It does nothing if no such ignore directive exists. (*File) DropReplace(oldPath, oldVers string) error (*File) DropRequire(path string) error (*File) DropRetract(vi VersionInterval) error DropTool removes a tool directive with the given path. It does nothing if no such tool directive exists. DropToolchainStmt deletes the toolchain statement from the file. (*File) Format() ([]byte, error) SetRequire updates the requirements of f to contain exactly req, preserving the existing block structure and line comment contents (except for 'indirect' markings) for the first requirement on each named module path. The Syntax field is ignored for the requirements in req. Any requirements not already present in the file are added to the block containing the last require line. The requirements in req must specify at most one distinct version for each module path. If any existing requirements may be removed, the caller should call [File.Cleanup] after all edits are complete. SetRequireAtMostTwo is like SetRequireSeparateIndirect but it aggressively consolidates all requirements into at most two blocks (one direct, one indirect). It ignores existing blocks and comments when deciding where to place requirements. SetRequireSeparateIndirect updates the requirements of f to contain the given requirements. Comment contents (except for 'indirect' markings) are retained from the first existing requirement for each module path. Like SetRequire, SetRequireSeparateIndirect adds requirements for new paths in req, updates the version and "// indirect" comment on existing requirements, and deletes requirements on paths not in req. Existing duplicate requirements are deleted. As its name suggests, SetRequireSeparateIndirect puts direct and indirect requirements into two separate blocks, one containing only direct requirements, and the other containing only indirect requirements. SetRequireSeparateIndirect may move requirements between these two blocks when their indirect markings change. However, SetRequireSeparateIndirect won't move requirements from other blocks, especially blocks with comments. If the file initially has one uncommented block of requirements, SetRequireSeparateIndirect will split it into a direct-only and indirect-only block. This aids in the transition to separate blocks. (*File) SortBlocks() func Parse(file string, data []byte, fix VersionFixer) (*File, error) func ParseLax(file string, data []byte, fix VersionFixer) (*File, error)
A FileSyntax represents an entire go.mod file. Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression // file path Stmt []Expr Cleanup cleans up the file syntax x after any edit operations. To avoid quadratic behavior, (*Line).markRemoved marks the line as dead by setting line.Token = nil but does not remove it from the slice in which it appears. After edits have all been indicated, calling Cleanup cleans out the dead lines. Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*FileSyntax) Span() (start, end Position) *FileSyntax : Expr func Format(f *FileSyntax) []byte
A Go is the go statement. Syntax *Line // "1.23"
A Godebug is a single godebug key=value statement. Key string Syntax *Line Value string
An Ignore is a single ignore statement. Path string Syntax *Line
A Line is a single line of tokens. Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression End Position InBlock bool Start Position Token []string Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*Line) Span() (start, end Position) *Line : Expr
A LineBlock is a factored block of lines, like require ( "x" "y" ) Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression LParen LParen Line []*Line RParen RParen Start Position Token []string Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*LineBlock) Span() (start, end Position) *LineBlock : Expr
An LParen represents the beginning of a parenthesized line block. It is a place to store suffix comments. Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression Pos Position Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*LParen) Span() (start, end Position) *LParen : Expr
A Module is the module statement. Deprecated string Mod module.Version Syntax *Line
A Position describes an arbitrary source position in a file, including the file, line, column, and byte offset. // byte in input (starting at 0) // line in input (starting at 1) // rune in line (starting at 1) func (*CommentBlock).Span() (start, end Position) func Expr.Span() (start, end Position) func (*FileSyntax).Span() (start, end Position) func (*Line).Span() (start, end Position) func (*LineBlock).Span() (start, end Position) func (*LParen).Span() (start, end Position) func (*RParen).Span() (start, end Position)
A Replace is a single replace statement. New module.Version Old module.Version Syntax *Line
A Require is a single require statement. // has "// indirect" comment Mod module.Version Syntax *Line func (*File).SetRequire(req []*Require) func (*File).SetRequireAtMostTwo(req []*Require) func (*File).SetRequireSeparateIndirect(req []*Require)
A Retract is a single retract statement. Rationale string Syntax *Line VersionInterval VersionInterval VersionInterval.High string VersionInterval.Low string
An RParen represents the end of a parenthesized line block. It is a place to store whole-line (before) comments. Comments Comments For top-level expressions only, After lists whole-line comments following the expression. // whole-line comments before this expression // end-of-line comments after this expression Pos Position Comment returns the receiver. This isn't useful by itself, but a [Comments] struct is embedded into all the expression implementation types, and this gives each of those a Comment method to satisfy the Expr interface. (*RParen) Span() (start, end Position) *RParen : Expr
A Tool is a single tool statement. Path string Syntax *Line
A Toolchain is the toolchain statement. // "go1.21rc1" Syntax *Line
A Use is a single directory statement. // Module path in the comment. // Use path of module. Syntax *Line func (*WorkFile).SetUse(dirs []*Use)
func Parse(file string, data []byte, fix VersionFixer) (*File, error) func ParseLax(file string, data []byte, fix VersionFixer) (*File, error) func ParseWork(file string, data []byte, fix VersionFixer) (*WorkFile, error)
A VersionInterval represents a range of versions with upper and lower bounds. Intervals are closed: both bounds are included. When Low is equal to High, the interval may refer to a single version ('v1.2.3') or an interval ('[v1.2.3, v1.2.3]'); both have the same representation. High string Low string func (*File).AddRetract(vi VersionInterval, rationale string) error func (*File).DropRetract(vi VersionInterval) error
A WorkFile is the parsed, interpreted form of a go.work file. Go *Go Godebug []*Godebug Replace []*Replace Syntax *FileSyntax Toolchain *Toolchain Use []*Use (*WorkFile) AddGoStmt(version string) error AddGodebug sets the first godebug line for key to value, preserving any existing comments for that line and removing all other godebug lines for key. If no line currently exists for key, AddGodebug adds a new line at the end of the last godebug block. (*WorkFile) AddNewUse(diskPath, modulePath string) (*WorkFile) AddReplace(oldPath, oldVers, newPath, newVers string) error (*WorkFile) AddToolchainStmt(name string) error (*WorkFile) AddUse(diskPath, modulePath string) error Cleanup cleans up the file f after any edit operations. To avoid quadratic behavior, modifications like [WorkFile.DropUse] clear the entry but do not remove it from the slice. Cleanup cleans out all the cleared entries. DropGoStmt deletes the go statement from the file. (*WorkFile) DropGodebug(key string) error (*WorkFile) DropReplace(oldPath, oldVers string) error DropToolchainStmt deletes the toolchain statement from the file. (*WorkFile) DropUse(path string) error (*WorkFile) SetUse(dirs []*Use) (*WorkFile) SortBlocks() func ParseWork(file string, data []byte, fix VersionFixer) (*WorkFile, error)
Package-Level Functions (total 29, in which 8 are exported)
AutoQuote returns s or, if quoting is required for s to appear in a go.mod, the quotation of s.
Format returns a go.mod file as a byte slice, formatted in standard style.
IsDirectoryPath reports whether the given path should be interpreted as a directory path. Just like on the go command line, relative paths starting with a '.' or '..' path component and rooted paths are directory paths; the rest are module paths.
ModulePath returns the module path from the go.mod file text. If it cannot find a module path, it returns an empty string. It is tolerant of unrelated problems in the go.mod file.
MustQuote reports whether s must be quoted in order to appear as a single token in a go.mod line.
Parse parses and returns a go.mod file. file is the name of the file, used in positions and errors. data is the content of the file. fix is an optional function that canonicalizes module versions. If fix is nil, all module versions must be canonical ([module.CanonicalVersion] must return the same string).
ParseLax is like Parse but ignores unknown statements. It is used when parsing go.mod files other than the main module, under the theory that most statement types we add in the future will only apply in the main module, like exclude and replace, and so we get better gradual deployments if old go commands simply ignore those statements when found in go.mod files in dependencies.
ParseWork parses and returns a go.work file. file is the name of the file, used in positions and errors. data is the content of the file. fix is an optional function that canonicalizes module versions. If fix is nil, all module versions must be canonical ([module.CanonicalVersion] must return the same string).
Package-Level Variables (total 7, in which 2 are exported)
Toolchains must be named beginning with `go1`, like "go1.20.3" or "go1.20.3-gccgo". As a special case, "default" is also permitted. Note that this regexp is a much looser condition than go/version.IsValid, for forward compatibility. (This code has to be work to identify new toolchains even if we tweak the syntax in the future.)
Package-Level Constants (total 5, none are exported)