github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/modload/modfile.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package modload 6 7 import ( 8 "github.com/shogo82148/std/context" 9 "github.com/shogo82148/std/errors" 10 11 "golang.org/x/mod/modfile" 12 "golang.org/x/mod/module" 13 ) 14 15 // ReadModFile reads and parses the mod file at gomod. ReadModFile properly applies the 16 // overlay, locks the file while reading, and applies fix, if applicable. 17 func ReadModFile(gomod string, fix modfile.VersionFixer) (data []byte, f *modfile.File, err error) 18 19 // CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by 20 // the main module's go.mod or retracted by its author. Most version queries use 21 // this to filter out versions that should not be used. 22 func CheckAllowed(ctx context.Context, m module.Version) error 23 24 // ErrDisallowed is returned by version predicates passed to Query and similar 25 // functions to indicate that a version should not be considered. 26 var ErrDisallowed = errors.New("disallowed module version") 27 28 // CheckExclusions returns an error equivalent to ErrDisallowed if module m is 29 // excluded by the main module's go.mod file. 30 func CheckExclusions(ctx context.Context, m module.Version) error 31 32 // CheckRetractions returns an error if module m has been retracted by 33 // its author. 34 func CheckRetractions(ctx context.Context, m module.Version) (err error) 35 36 type ModuleRetractedError struct { 37 Rationale []string 38 } 39 40 func (e *ModuleRetractedError) Error() string 41 42 func (e *ModuleRetractedError) Is(err error) bool 43 44 // ShortMessage returns a string from go.mod (for example, a retraction 45 // rationale or deprecation message) that is safe to print in a terminal. 46 // 47 // If the given string is empty, ShortMessage returns the given default. If the 48 // given string is too long or contains non-printable characters, ShortMessage 49 // returns a hard-coded string. 50 func ShortMessage(message, emptyDefault string) string 51 52 // CheckDeprecation returns a deprecation message from the go.mod file of the 53 // latest version of the given module. Deprecation messages are comments 54 // before or on the same line as the module directives that start with 55 // "Deprecated:" and run until the end of the paragraph. 56 // 57 // CheckDeprecation returns an error if the message can't be loaded. 58 // CheckDeprecation returns "", nil if there is no deprecation message. 59 func CheckDeprecation(ctx context.Context, m module.Version) (deprecation string, err error) 60 61 // Replacement returns the replacement for mod, if any. If the path in the 62 // module.Version is relative it's relative to the single main module outside 63 // workspace mode, or the workspace's directory in workspace mode. 64 func Replacement(mod module.Version) module.Version 65 66 // ToDirectoryPath adds a prefix if necessary so that path in unambiguously 67 // an absolute path or a relative path starting with a '.' or '..' 68 // path component. 69 func ToDirectoryPath(path string) string