mesh-build-node: say a placed release outside the package file's range (H-A-276)
The latest release is placed whatever the range a package.json names (ADR 0573 §2); one outside that range is now said in the journal, for the SDK and every other mesh package, so a module built against a release its range excludes is visible. A range of a form not read is said as such. Builds: design 47 H H-A-267, H-A-276
This commit is contained in:
@@ -458,6 +458,15 @@ func (m *Module) placeLatest(ctx context.Context, dir, rel, name string) error {
|
||||
}
|
||||
version, integrity = latest, ""
|
||||
}
|
||||
if spec := namedRange(dir, name); spec != "" && m.Say != nil {
|
||||
// Said, never refused: the latest release is placed whatever the range (ADR 0573 §2; design 47 H, H-A-276).
|
||||
switch in, read := inRange(spec, version); {
|
||||
case !read:
|
||||
m.Say("%s: its package.json names %s at %q, a range not read; the latest published %s is placed (ADR 0573 §2)", rel, name, spec, version)
|
||||
case !in:
|
||||
m.Say("%s: the latest published %s@%s lies outside the range %q its package.json names; it is placed (ADR 0573 §2)", rel, name, version, spec)
|
||||
}
|
||||
}
|
||||
want := name + "@" + version
|
||||
got, registered, held, err := m.registryPackageChecked(ctx, name, version)
|
||||
if err != nil {
|
||||
@@ -479,6 +488,109 @@ func (m *Module) placeLatest(ctx context.Context, dir, rel, name string) error {
|
||||
return unpackPackage(got, into)
|
||||
}
|
||||
|
||||
// namedRange is the range the module's package.json names of name: its dependencies first, then its peer, optional and
|
||||
// dev dependencies; "" when it names none.
|
||||
func namedRange(dir, name string) string {
|
||||
raw, err := os.ReadFile(filepath.Join(dir, "package.json"))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var p map[string]any
|
||||
if json.Unmarshal(raw, &p) != nil {
|
||||
return ""
|
||||
}
|
||||
for _, k := range []string{"dependencies", "peerDependencies", "optionalDependencies", "devDependencies"} {
|
||||
deps, _ := p[k].(map[string]any)
|
||||
if spec, ok := deps[name].(string); ok {
|
||||
return strings.TrimSpace(spec)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// inRange says whether the release v (major.minor.patch) lies in the npm range spec; read is false for a range of a
|
||||
// form it does not read (a union, a hyphen range, a comparator, a tag other than latest), never taken as in or out. It
|
||||
// reads what a package file of the mesh names: an exact version, an x-range, a caret and a tilde range, as npm does.
|
||||
func inRange(spec, v string) (in, read bool) {
|
||||
spec = strings.TrimSpace(spec)
|
||||
if spec == "" || spec == "*" || spec == "latest" {
|
||||
return true, true
|
||||
}
|
||||
if strings.ContainsAny(spec, " |<>") {
|
||||
return false, false
|
||||
}
|
||||
op := ""
|
||||
if spec[0] == '^' || spec[0] == '~' {
|
||||
op, spec = spec[:1], spec[1:]
|
||||
}
|
||||
spec = strings.TrimPrefix(strings.TrimPrefix(spec, "="), "v")
|
||||
if strings.Contains(spec, "-") || strings.Contains(spec, "+") {
|
||||
return false, false // a pre-release or build range
|
||||
}
|
||||
var low [3]int
|
||||
given := 0
|
||||
for i, part := range strings.Split(spec, ".") {
|
||||
if i > 2 {
|
||||
return false, false
|
||||
}
|
||||
if part == "x" || part == "X" || part == "*" {
|
||||
break
|
||||
}
|
||||
n, err := strconv.Atoi(part)
|
||||
if err != nil || n < 0 {
|
||||
return false, false
|
||||
}
|
||||
low[i] = n
|
||||
given = i + 1
|
||||
}
|
||||
if given == 0 {
|
||||
return true, true // x, x.x
|
||||
}
|
||||
var at [3]int
|
||||
for i, part := range strings.Split(v, ".") {
|
||||
if i > 2 {
|
||||
return false, false
|
||||
}
|
||||
n, err := strconv.Atoi(part)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
at[i] = n
|
||||
}
|
||||
// bump is the part of low raised by one for the exclusive upper bound, the parts after it zero.
|
||||
bump := given - 1
|
||||
switch op {
|
||||
case "^":
|
||||
switch {
|
||||
case low[0] > 0 || given == 1:
|
||||
bump = 0
|
||||
case low[1] > 0 || given == 2:
|
||||
bump = 1
|
||||
default:
|
||||
bump = 2
|
||||
}
|
||||
case "~":
|
||||
bump = 0
|
||||
if given > 1 {
|
||||
bump = 1
|
||||
}
|
||||
}
|
||||
high := low
|
||||
high[bump]++
|
||||
for i := bump + 1; i < 3; i++ {
|
||||
high[i] = 0
|
||||
}
|
||||
less := func(a, b [3]int) bool {
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return a[i] < b[i]
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
return !less(at, low) && less(at, high), true
|
||||
}
|
||||
|
||||
// placedSDK is the version of the SDK placed in the module's node_modules ("" when none is).
|
||||
func placedSDK(dir string) string {
|
||||
p, err := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(sdkPackage)))
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -189,3 +190,46 @@ func TestTheGroupPackagesArgumentIsRefusedWhenItIsNotOne(t *testing.T) {
|
||||
t.Errorf("a member's root and a member's directory: %+v %v", gs, err)
|
||||
}
|
||||
}
|
||||
|
||||
// design 47 H, H-A-276 (amending H-A-267): the latest release placed outside the range the package file names is said in
|
||||
// the journal, and placed all the same; one inside it is not said; a range of a form not read is said as such.
|
||||
func TestALatestReleaseOutsideItsRangeIsSaid(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
spec, v string
|
||||
in, read bool
|
||||
}{
|
||||
{"^0.1.0", "0.1.9", true, true}, {"^0.1.0", "0.2.0", false, true}, {"^1.2.3", "1.9.0", true, true},
|
||||
{"^1.2.3", "2.0.0", false, true}, {"^0.0.3", "0.0.4", false, true}, {"^0.x", "0.9.0", true, true},
|
||||
{"~0.1.2", "0.1.9", true, true}, {"~0.1.2", "0.2.0", false, true}, {"~1", "1.5.0", true, true},
|
||||
{"0.1.4", "0.1.4", true, true}, {"0.1.4", "0.1.5", false, true}, {"0.1.x", "0.1.7", true, true},
|
||||
{"0.1", "0.2.0", false, true}, {"*", "9.9.9", true, true}, {"^0.1.0", "0.0.9", false, true},
|
||||
{">=0.1.0", "0.2.0", false, false}, {"^0.1.0 || ^0.2.0", "0.2.0", false, false}, {"file:../x", "0.1.0", false, false},
|
||||
} {
|
||||
if in, read := inRange(c.spec, c.v); in != c.in || read != c.read {
|
||||
t.Errorf("inRange(%q, %q) = %v, %v; want %v, %v", c.spec, c.v, in, read, c.in, c.read)
|
||||
}
|
||||
}
|
||||
m, _, _ := newMachine(t)
|
||||
const name = "@novox/mesh-delivery-contracts-git"
|
||||
fakeRegistryOf(t, m, map[string]map[string][]byte{name: {"0.1.9": tgzOf(t, name, "0.1.9"), "0.2.0": tgzOf(t, name, "0.2.0")}})
|
||||
for _, c := range []struct{ spec, said string }{
|
||||
{"^0.1.0", `the latest published ` + name + `@0.2.0 lies outside the range "^0.1.0"`},
|
||||
{"^0.2.0", ""},
|
||||
{">=0.1.0", `at ">=0.1.0", a range not read`},
|
||||
} {
|
||||
var said []string
|
||||
m.Say = func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }
|
||||
dir := t.TempDir()
|
||||
write(t, filepath.Join(dir, "package.json"), `{"dependencies":{"`+name+`":"`+c.spec+`"}}`, 0o644)
|
||||
if err := m.placeLatest(context.Background(), dir, "modules/m", name); err != nil {
|
||||
t.Fatalf("%s: %v", c.spec, err)
|
||||
}
|
||||
if p, _ := readPackageJSON(filepath.Join(dir, "node_modules", filepath.FromSlash(name))); p.Version != "0.2.0" {
|
||||
t.Errorf("%s: placed %q, want the latest release 0.2.0", c.spec, p.Version)
|
||||
}
|
||||
joined := strings.Join(said, "\n")
|
||||
if c.said == "" && joined != "" || c.said != "" && !strings.Contains(joined, c.said) {
|
||||
t.Errorf("%s: said %q, want %q", c.spec, joined, c.said)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user