gocuelang.org/go/mod/modfile Index | Files

package modfile

import "cuelang.org/go/mod/modfile"

Package modfile provides functionality for reading and parsing the CUE module file, cue.mod/module.cue.

WARNING: THIS PACKAGE IS EXPERIMENTAL. ITS API MAY CHANGE AT ANY TIME.

Index

Variables

var ErrNoLanguageVersion = fmt.Errorf("no language version declared in module.cue")

ErrNoLanguageVersion is returned by Parse and ParseNonStrict when a cue.mod/module.cue file lacks the `language.version` field.

Functions

func EarliestClosedSchemaVersion

func EarliestClosedSchemaVersion() string

EarliestClosedSchemaVersion returns the earliest module.cue schema version that excludes unknown fields. Any version declared in a module.cue file should be at least this, because that's when we added the language.version field itself.

Types

type Dep

type Dep struct {
	Version string `json:"v"`
	Default bool   `json:"default,omitempty"`
}

type File

type File struct {
	// Module holds the module path, which may
	// not contain a major version suffix.
	// Use the [File.QualifiedModule] method to obtain a module
	// path that's always qualified. See also the
	// [File.ModulePath] and [File.MajorVersion] methods.
	Module   string                    `json:"module"`
	Language *Language                 `json:"language,omitempty"`
	Source   *Source                   `json:"source,omitempty"`
	Deps     map[string]*Dep           `json:"deps,omitempty"`
	Custom   map[string]map[string]any `json:"custom,omitempty"`
	// contains filtered or unexported fields
}

File represents the contents of a cue.mod/module.cue file.

func FixLegacy

func FixLegacy(modfile []byte, filename string) (*File, error)

FixLegacy converts a legacy module.cue file as parsed by ParseLegacy into a format suitable for parsing with Parse. It adds a language.version field and moves all unrecognized fields into custom.legacy.

If there is no module field or it is empty, it is set to "test.example".

If the file already parses OK with ParseNonStrict, it returns the result of that.

func Parse

func Parse(modfile []byte, filename string) (*File, error)

Parse verifies that the module file has correct syntax and follows the schema following the required language.version field. The file name is used for error messages. All dependencies must be specified correctly: with major versions in the module paths and canonical dependency versions.

func ParseLegacy

func ParseLegacy(modfile []byte, filename string) (*File, error)

ParseLegacy parses the legacy version of the module file that only supports the single field "module" and ignores all other fields.

func ParseNonStrict

func ParseNonStrict(modfile []byte, filename string) (*File, error)

ParseNonStrict is like Parse but allows some laxity in the parsing:

The file name is used for error messages.

func (*File) DefaultMajorVersions

func (f *File) DefaultMajorVersions() map[string]string

DefaultMajorVersions returns a map from module base path to the major version that's specified as the default for that module. The caller should not modify the returned map.

func (*File) DepVersions

func (f *File) DepVersions() []module.Version

DepVersions returns the versions of all the modules depended on by the file. The caller should not modify the returned slice.

This always returns the same value, even if the contents of f are changed. If f was not created with Parse, it returns nil.

func (*File) Format

func (f *File) Format() ([]byte, error)

Format returns a formatted representation of f in CUE syntax.

func (*File) MajorVersion

func (f *File) MajorVersion() string

MajorVersion returns the major version of the module, not including the "@". If there is no module (which can happen when ParseLegacy is used or if Module is explicitly set to an empty string), it returns the empty string.

func (*File) ModuleForImportPath

func (f *File) ModuleForImportPath(importPath string) (module.Version, bool)

ModuleForImportPath returns the module that should contain the given import path and reports whether the module was found. It does not check to see if the import path actually exists within the module.

It works entirely from information in f, meaning that it does not consult a registry to resolve a package whose module is not mentioned in the file, which means it will not work in general unless the module is tidy (as with `cue mod tidy`).

func (*File) ModulePath

func (f *File) ModulePath() string

ModulePath returns the path part of the module without its major version suffix.

func (*File) QualifiedModule

func (f *File) QualifiedModule() string

Module returns the fully qualified module path if is one. It returns the empty string when ParseLegacy is used and the module field is empty.

Note that when the module field does not contain a major version suffix, "@v0" is assumed.

type Language

type Language struct {
	Version string `json:"version,omitempty"`
}

type Source

type Source struct {
	Kind string `json:"kind"`
}

Source represents how to transform from a module's source to its actual contents.

func (*Source) Validate

func (src *Source) Validate() error

Validate checks that src is well formed.

Source Files

modfile.go

Version
v0.13.0-alpha.1
Published
Mar 12, 2025
Platform
windows/amd64
Imports
18 packages
Last checked
47 minutes ago

Tools for package owners.