|
| 1 | +// Copyright 2023 CUE Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package modfile |
| 16 | + |
| 17 | +import ( |
| 18 | + _ "embed" |
| 19 | + "fmt" |
| 20 | + "sync" |
| 21 | + |
| 22 | + "golang.org/x/mod/semver" |
| 23 | + |
| 24 | + "cuelang.org/go/cue" |
| 25 | + "cuelang.org/go/cue/cuecontext" |
| 26 | + "cuelang.org/go/cue/errors" |
| 27 | + "cuelang.org/go/cue/parser" |
| 28 | + "cuelang.org/go/cue/token" |
| 29 | + "cuelang.org/go/internal/mod/module" |
| 30 | +) |
| 31 | + |
| 32 | +//go:embed schema.cue |
| 33 | +var moduleSchemaData []byte |
| 34 | + |
| 35 | +type File struct { |
| 36 | + Module string `json:"module"` |
| 37 | + Language Language `json:"language"` |
| 38 | + Deps map[string]*Dep `json:"deps,omitempty"` |
| 39 | + versions []module.Version |
| 40 | +} |
| 41 | + |
| 42 | +type Language struct { |
| 43 | + Version string `json:"version"` |
| 44 | +} |
| 45 | + |
| 46 | +type Dep struct { |
| 47 | + Version string `json:"v"` |
| 48 | + Default bool `json:"default,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +type noDepsFile struct { |
| 52 | + Module string `json:"module"` |
| 53 | +} |
| 54 | + |
| 55 | +var ( |
| 56 | + moduleSchemaOnce sync.Once |
| 57 | + _moduleSchema cue.Value |
| 58 | +) |
| 59 | + |
| 60 | +func moduleSchema() cue.Value { |
| 61 | + moduleSchemaOnce.Do(func() { |
| 62 | + ctx := cuecontext.New() |
| 63 | + schemav := ctx.CompileBytes(moduleSchemaData, cue.Filename("cuelang.org/go/internal/mod/modfile/schema.cue")) |
| 64 | + schemav = lookup(schemav, cue.Def("#File")) |
| 65 | + //schemav = schemav.Unify(lookup(schemav, cue.Hid("#Strict", "_"))) |
| 66 | + if err := schemav.Validate(); err != nil { |
| 67 | + panic(fmt.Errorf("internal error: invalid CUE module.cue schema: %v", errors.Details(err, nil))) |
| 68 | + } |
| 69 | + _moduleSchema = schemav |
| 70 | + }) |
| 71 | + return _moduleSchema |
| 72 | +} |
| 73 | + |
| 74 | +func lookup(v cue.Value, sels ...cue.Selector) cue.Value { |
| 75 | + return v.LookupPath(cue.MakePath(sels...)) |
| 76 | +} |
| 77 | + |
| 78 | +// Parse verifies that the module file has correct syntax. |
| 79 | +// The file name is used for error messages. |
| 80 | +// All dependencies must be specified correctly: with major |
| 81 | +// versions in the module paths and canonical dependency |
| 82 | +// versions. |
| 83 | +func Parse(modfile []byte, filename string) (*File, error) { |
| 84 | + return parse(modfile, filename, true) |
| 85 | +} |
| 86 | + |
| 87 | +// ParseLegacy parses the legacy version of the module file |
| 88 | +// that only supports the single field "module" and ignores all other |
| 89 | +// fields. |
| 90 | +func ParseLegacy(modfile []byte, filename string) (*File, error) { |
| 91 | + v := moduleSchema().Context().CompileBytes(modfile, cue.Filename(filename)) |
| 92 | + if err := v.Err(); err != nil { |
| 93 | + return nil, errors.Wrapf(err, token.NoPos, "invalid module.cue file") |
| 94 | + } |
| 95 | + var f noDepsFile |
| 96 | + if err := v.Decode(&f); err != nil { |
| 97 | + return nil, newCUEError(err, filename) |
| 98 | + } |
| 99 | + return &File{ |
| 100 | + Module: f.Module, |
| 101 | + }, nil |
| 102 | +} |
| 103 | + |
| 104 | +// ParseNonStrict is like Parse but allows some laxity in the parsing: |
| 105 | +// - if a module path lacks a version, it's taken from the version. |
| 106 | +// - if a non-canonical version is used, it will be canonicalized. |
| 107 | +// |
| 108 | +// The file name is used for error messages. |
| 109 | +func ParseNonStrict(modfile []byte, filename string) (*File, error) { |
| 110 | + return parse(modfile, filename, false) |
| 111 | +} |
| 112 | + |
| 113 | +func parse(modfile []byte, filename string, strict bool) (*File, error) { |
| 114 | + file, err := parser.ParseFile(filename, modfile) |
| 115 | + if err != nil { |
| 116 | + return nil, errors.Wrapf(err, token.NoPos, "invalid module.cue file syntax") |
| 117 | + } |
| 118 | + // TODO disallow non-data-mode CUE. |
| 119 | + |
| 120 | + v := moduleSchema().Context().BuildFile(file) |
| 121 | + if err := v.Validate(cue.Concrete(true)); err != nil { |
| 122 | + return nil, errors.Wrapf(err, token.NoPos, "invalid module.cue file value") |
| 123 | + } |
| 124 | + v = v.Unify(moduleSchema()) |
| 125 | + if err := v.Validate(); err != nil { |
| 126 | + return nil, newCUEError(err, filename) |
| 127 | + } |
| 128 | + var mf File |
| 129 | + if err := v.Decode(&mf); err != nil { |
| 130 | + return nil, errors.Wrapf(err, token.NoPos, "internal error: cannot decode into modFile struct") |
| 131 | + } |
| 132 | + if strict { |
| 133 | + _, v, ok := module.SplitPathVersion(mf.Module) |
| 134 | + if !ok { |
| 135 | + return nil, fmt.Errorf("module path %q in %s does not contain major version", mf.Module, filename) |
| 136 | + } |
| 137 | + if semver.Major(v) != v { |
| 138 | + return nil, fmt.Errorf("module path %s in %q should contain the major version only", mf.Module, filename) |
| 139 | + } |
| 140 | + } |
| 141 | + if v := mf.Language.Version; v != "" && !semver.IsValid(v) { |
| 142 | + return nil, fmt.Errorf("language version %q in %s is not well formed", v, filename) |
| 143 | + } |
| 144 | + var versions []module.Version |
| 145 | + // Check that major versions match dependency versions. |
| 146 | + for m, dep := range mf.Deps { |
| 147 | + v, err := module.NewVersion(m, dep.Version) |
| 148 | + if err != nil { |
| 149 | + return nil, fmt.Errorf("invalid module.cue file %s: cannot make version from module %q, version %q: %v", filename, m, dep.Version, err) |
| 150 | + } |
| 151 | + versions = append(versions, v) |
| 152 | + if strict && v.Path() != m { |
| 153 | + return nil, fmt.Errorf("invalid module.cue file %s: no major version in %q", filename, m) |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + mf.versions = versions[:len(versions):len(versions)] |
| 158 | + module.Sort(mf.versions) |
| 159 | + return &mf, nil |
| 160 | +} |
| 161 | + |
| 162 | +func newCUEError(err error, filename string) error { |
| 163 | + // TODO we have some potential to improve error messages here. |
| 164 | + return err |
| 165 | +} |
| 166 | + |
| 167 | +// DepVersions returns the versions of all the modules depended on by the |
| 168 | +// file. The caller should not modify the returned slice. |
| 169 | +func (f *File) DepVersions() []module.Version { |
| 170 | + return f.versions |
| 171 | +} |
0 commit comments