mod

package module
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 15 Imported by: 5 modules, 3 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.

README

mod - Module support for Go/XGo

Build Status GitHub release Coverage Status GoDoc XGo

This repository holds packages for writing tools that work directly with Go/XGo module mechanics. That is, it is for direct manipulation of Go/XGo modules themselves.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDirNotInWorkspace = errors.New("directory is not in the workspace")

ErrDirNotInWorkspace is returned (wrapped, with the directory) by Workspace.Classes when dir does not belong to any main module of the workspace. Detect it with errors.Is(err, ErrDirNotInWorkspace).

View Source
var ErrInvalidPkgPath = errors.New("invalid package path")

ErrInvalidPkgPath is returned by LookupFrom for a package path that cannot be resolved to a location: an empty path, or a relative path that leaves every local module (or whose fromDir is empty).

View Source
var ErrNoModule = errors.New("mod: no go.work or go.mod found")

ErrNoModule is returned by LoadDir when neither a go.work nor a go.mod is found at or above the directory.

Functions

func Discover added in v0.23.0

func Discover(dirFrom string) (gowork, gomod string, err error)

Discover is like Find but also honors the GOWORK environment variable. It is shorthand for Finder{}.Discover.

Types

type Classes added in v0.23.0

type Classes = goxmod.Classes

Classes represents a collection of classfiles. It is a type alias for goxmod.Classes, so callers get the full goxmod.Classes API without importing goxmod just to name the type.

type Finder added in v0.23.0

type Finder struct {
	// Getenv reads an environment variable. If nil, os.Getenv is used.
	Getenv func(key string) string

	// Stat reports file information. If nil, os.Stat is used. Like the go
	// command it should follow symbolic links.
	Stat func(name string) (fs.FileInfo, error)
}

Finder locates go.work and go.mod files. The zero value uses the real file system and the process environment; the fields exist so that tests (and tools that run against a virtual file system) can substitute them.

func (Finder) Discover added in v0.23.0

func (f Finder) Discover(dirFrom string) (gowork, gomod string, err error)

Discover behaves like Find but honors GOWORK the way the go command does:

  • GOWORK=off disables workspace mode: only go.mod is searched for.
  • GOWORK=<file> uses that file as the workspace file, without searching.
  • Otherwise (unset or empty) it is exactly Find.

func (Finder) Find added in v0.23.0

func (f Finder) Find(dirFrom string) (gowork, gomod string, err error)

Find starts at dirFrom (the current directory if empty) and walks up towards the filesystem root, looking for go.work and go.mod in every directory on the way. The two searches are independent, and the go.work search does not stop when a go.mod has been seen.

The first go.work found ends the search. In that case gowork is set and gomod is always empty: with a workspace, the main modules are the ones listed in its "use" directives, and the nearest go.mod is not what defines them. Use Workspace.CheckDir to verify that the module of the current directory is one of them.

If there is no go.work, gomod is the nearest go.mod and gowork is empty. ErrNoModule is returned if there is neither.

A directory named go.work or go.mod is not a match. Find does not consult the GOWORK environment variable; see Discover.

func (Finder) FindGoMod added in v0.23.0

func (f Finder) FindGoMod(dirFrom string) (gomod string, err error)

FindGoMod returns the nearest go.mod at or above dirFrom (the current directory if empty), ignoring any go.work. It returns ErrNoModule if there is none.

type MissingError added in v0.23.0

type MissingError struct {
	Path string
}

MissingError reports a package path that is not standard and is in no module of the workspace.

func (*MissingError) Error added in v0.23.0

func (e *MissingError) Error() string

type Module added in v0.23.0

type Module struct {
	Path string
	Real module.Version
}

Module is a module of the workspace: a main module or a dependency. Path is the module path as it is required (and as import paths are rooted at it); Real is the module version that really supplies the code after replace directives. If the code lives in a local directory, which is the case for a main module and for a module replaced by a local directory, Real.Path is that directory as an absolute path and Real.Version is empty.

func (Module) IsLocal added in v0.23.0

func (m Module) IsLocal() bool

IsLocal reports whether the module's code lives in a local directory (a main module or a module replaced by a local directory), as opposed to the module cache.

type Package added in v0.23.0

type Package struct {
	Type    PkgType
	Dir     string
	ModDir  string
	ModPath string
	Real    module.Version // only when Type == PkgtExtern
}

Package is where a package lives (see LookupFrom).

type PkgType added in v0.23.0

type PkgType int

PkgType specifies a package type.

const (
	PkgtInvalid  PkgType = iota // an invalid package (the zero value; LookupFrom never returns it)
	PkgtStandard                // a standard Go package
	PkgtLocal                   // a package in a local directory: a workspace module or a local replace
	PkgtExtern                  // an external package
)

func (PkgType) String added in v0.23.0

func (t PkgType) String() string

String returns a short name of the package type: "invalid", "standard", "local" or "extern".

type ReplaceConflictError added in v0.23.0

type ReplaceConflictError struct {
	Old  module.Version // the replaced module (version may be empty)
	A, B module.Version // the two different replacements
}

ReplaceConflictError reports two main modules whose go.mod files replace the same module differently, with no replace in go.work to settle it.

func (*ReplaceConflictError) Error added in v0.23.0

func (e *ReplaceConflictError) Error() string

type Workspace added in v0.23.0

type Workspace struct {
	Dir  string            // directory containing go.work, or the module's directory
	File *modfile.WorkFile // parsed go.work, nil for a go.mod-only workspace
	// contains filtered or unexported fields
}

Workspace is a snapshot of a go.work (or a single go.mod) together with the main modules it lists. It is read-only: nothing in this package modifies the files. A Workspace serves both a real workspace and a plain single-module project; a caller never has to branch on "is there a go.work?".

If go.work, a go.mod, or the module cache changes, load the workspace again.

func LoadFromDir added in v0.23.0

func LoadFromDir(dir string) (*Workspace, error)

LoadFromDir finds the workspace that contains dir, the way the go command does, and loads it: the go.work file found by searching dir and its parents, otherwise the go.mod file found the same way. dir may be relative; it is made absolute first.

The search honors GOWORK: GOWORK=off skips go.work, and GOWORK=<path> uses that go.work and does no search (see Discover). If neither a go.work nor a go.mod is found, LoadFromDir returns the default workspace for dir (see NewDefault), which resolves the standard library and recognizes the built-in classfiles; it does not return ErrNoModule. Every other failure is still an error: a broken go.work or go.mod, or a GOWORK that names a missing file, is reported rather than degraded into a default workspace.

func LoadGoMod added in v0.23.0

func LoadGoMod(gomod string) (*Workspace, error)

LoadGoMod reads and parses the go.mod file at gomod and calls NewGoMod with its directory. The file is parsed strictly (modfile.Parse), as the go command parses a main module's go.mod.

func LoadGoWork added in v0.23.0

func LoadGoWork(gowork string) (*Workspace, error)

LoadGoWork reads and parses the go.work file at gowork and calls NewGoWork with its directory. Relative "use" paths are resolved against the directory of go.work and may point outside of it.

func NewDefault added in v0.23.0

func NewDefault(dir string) (*Workspace, error)

NewDefault creates the default workspace for dir: one main module with an empty module path and an empty go.mod, and no go.work. dir is made absolute first; it is also the main module's directory.

A default workspace resolves imports of the standard library only, and recognizes the built-in classfiles (".gsh" and "_test.gox") and nothing else. It is an ordinary go.mod-only workspace, so every Workspace method answers correctly on it with no special case beyond the empty module path. It is what LoadFromDir returns when neither a go.work nor a go.mod is found, and tools that already know there is no module can create one directly.

NewDefault reads nothing from disk. The only possible error is the one from making dir absolute.

func NewGoMod added in v0.23.0

func NewGoMod(dir string, f *modfile.File) (*Workspace, error)

NewGoMod creates a workspace with one main module, from a parsed go.mod file, and no go.work. dir is the directory that holds the file, which is the module's directory; it is made absolute first. The module directive is required.

NewGoMod does not read any file, and does not modify f; the workspace keeps f, so the caller must not modify it afterwards. Workspace.Path and Workspace.File are empty ("" and nil); Workspace.Dir is the module's directory.

func NewGoWork added in v0.23.0

func NewGoWork(dir string, f *modfile.WorkFile) (*Workspace, error)

NewGoWork creates a workspace from a parsed go.work file. dir is the directory that holds the file; the "use" and "replace" directories in the file are relative to it. dir is made absolute first.

NewGoWork reads the go.mod of every "use"d directory from disk, because the lookup needs the main modules' requirements, and returns an error if one cannot be read or parsed. It does not read the go.work file again and does not modify f; the workspace keeps f, so the caller must not modify it afterwards.

By convention Workspace.Path is set to dir/go.work; LoadGoWork sets it to the file that was actually read, which matters when GOWORK names a file with another name.

func (*Workspace) Classes added in v0.23.0

func (w *Workspace) Classes(dir string) (_ Classes, err error)

Classes returns the collection of classfiles for the main module that contains dir, the directory of the package being compiled.

Whether a file such as main.gsh, foo_test.gox or Sprite1.spx is a classfile is not decided by the file itself. It depends on the built-in projects (.gsh and _test.gox), the projects declared in the owning module's gox.mod (falling back to gop.mod), and the projects declared by dependency modules that the module marks with //xgo:class (or the legacy //gop:class) on a require line in go.mod. Classes resolves all of this into a single, read-only lookup table. See Classes (the type) for the questions it answers.

Which main module owns dir follows how the go command assigns directories to modules. dir is made absolute (against the process working directory) and cleaned first, and need not exist on disk: only its path is used, so a tool can ask about a directory it is about to create. Among the main modules whose root contains dir (containment is checked on path components, so /work/app2 is not inside /work/app), the one with the longest root wins. If a go.mod that is not part of the workspace sits strictly below that root on the way down to dir, dir belongs to that foreign module instead, not to the workspace. When no main module owns dir, Classes returns an error wrapping ErrDirNotInWorkspace that includes dir.

The result is cached per main module, not per directory: all directories of one module share the same *Classes pointer and the same error. The work for a module happens on the first call that reaches it, guarded by that module's own sync.Once, so different modules build independently and in parallel and Classes is safe for concurrent use. A build failure is cached too, and is returned for every later call into that module; it does not affect the other modules. The ErrDirNotInWorkspace result is not cached, because it depends only on the path. A long-lived process that wants to retry a build after fixing the environment should create a new Workspace.

A class module that is not yet in the module cache is fetched during the first Classes call that reaches its module. Only the build result is cached; the ownership resolution runs on every call and touches the filesystem (it probes for a foreign go.mod on the way down to dir), so callers on latency-sensitive paths (such as an editor keystroke handler) should resolve dir to a *Classes once at startup and reuse it rather than call per request.

In a workspace with several main modules (a go.work with several use entries), Classes answers for the module that owns dir and for that module only. A class decides how the user's own code is interpreted, and that code belongs to the module the package being compiled lives in; the classfiles of sibling main modules are implementation details of those modules and are not merged in.

The returned *Classes is a lookup table and must be treated as read-only.

func (*Workspace) GoVersion added in v0.23.0

func (w *Workspace) GoVersion() string

GoVersion returns the version of the "go" directive of go.work, or "" if there is none. For a go.mod-only workspace it returns the version of the main module's "go" directive.

func (*Workspace) IsDefault added in v0.23.0

func (w *Workspace) IsDefault() bool

IsDefault reports whether w is a default workspace, that is, one created by NewDefault, or by LoadFromDir when it found neither go.work nor go.mod. A real workspace can never satisfy it: a go.work workspace has file != nil, and NewGoMod and loadMainMod require a "module" directive, so an empty module path occurs only here.

func (*Workspace) Lookup added in v0.23.0

func (w *Workspace) Lookup(pkgPath string) (pkg *Package, err error)

Lookup returns where a package lives. pkgPath is an import path. It is safe for concurrent use.

On failure it returns nil and an error: ErrInvalidPkgPath for an empty path or a relative path that cannot be resolved, a *MissingError for a path that is not standard and in no module, or the error from Modules when the module list cannot be built.

func (*Workspace) LookupModule added in v0.23.0

func (w *Workspace) LookupModule(modPath string) (modVer module.Version, ok bool)

LookupModule looks up a module by its exact module path. ok is false if there is no such module, or if the modules cannot be determined (see Modules).

func (*Workspace) Modules added in v0.23.0

func (w *Workspace) Modules() ([]Module, error)

Modules returns all modules of the workspace, sorted by module path in descending order (longest prefix first). The main modules are included: from the workspace's point of view they are modules in local directories, like any dependency replaced by a local directory. If replace directives conflict, it returns the *ReplaceConflictError, as Replacements does. The returned slice is shared and must not be modified.

func (*Workspace) PkgType added in v0.23.0

func (w *Workspace) PkgType(pkgPath string) PkgType

PkgType returns the type of a package from its path. For a path that needs the module list while the list cannot be determined, it returns PkgtInvalid.

func (*Workspace) Validate added in v0.23.0

func (w *Workspace) Validate() error

Validate checks the workspace for the problems the go command rejects:

  • there are no main modules at all;
  • a directory, or a module path, is listed more than once;
  • a module requires a newer Go version than go.work declares;
  • the replace directives conflict (see Replacements).

For a go.mod-only workspace the checks that are about go.work (duplicate "use" directories and the go.work "go" version) do not apply; the module is still checked, and Replacements reports a conflict only if that one go.mod conflicts with itself.

All problems found are reported together (see errors.Join).

Directories

Path Synopsis
Package gomod reads and edits the XGo-specific parts of a go.mod file.
Package gomod reads and edits the XGo-specific parts of a go.mod file.
Package goxmod answers two questions about an XGo module:
Package goxmod answers two questions about an XGo module:

Jump to

Keyboard shortcuts

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