diff --git a/.changeset/31832-picklist-sibling-dependency.md b/.changeset/31832-picklist-sibling-dependency.md new file mode 100644 index 00000000000..81274e4db70 --- /dev/null +++ b/.changeset/31832-picklist-sibling-dependency.md @@ -0,0 +1,19 @@ +--- +'@objectstack/cli': patch +--- + +Clause-②: no (narrowing) +Metadata-protocol: none + +`os validate` and `os build` now resolve a field's `picklist` the way the runtime does (ADR-0150 D3): from the package that declares the field, then its declared dependency closure (plus `sys`). A field naming a list that only a sibling package of the same artifact declares, with no declared dependency on that package, is refused as `picklist-reference-unknown` in the runtime's own sentence. Before, the door accepted it, while the boot served the field no options and the install refused it. + +The fix is one `dependencies` entry on the package that declares the field, for example `dependencies: { 'com.example.core': '^1.0.0' }`. The refusal's hint names the entry. When that package also depends on a package the stack does not carry, the reference is reported as `picklist-reference-unverified` and not refused, as before. + +Two more references are now refused, as the boot already refuses them (`INVALID_METADATA`), whatever the outside dependencies: + +- a field whose package declares dependencies on two packages of the stack that both declare the list; +- a `picklistExtensions` entry whose `extend` names a list two packages of the stack declare. An extension still needs no dependency: its target resolves against any single holder in the stack. + +The fix for both is to keep the list in one package. + + diff --git a/packages/cli/src/utils/picklist-references.boot-parity.test.ts b/packages/cli/src/utils/picklist-references.boot-parity.test.ts index 9c96720257c..d840e2a1375 100644 --- a/packages/cli/src/utils/picklist-references.boot-parity.test.ts +++ b/packages/cli/src/utils/picklist-references.boot-parity.test.ts @@ -50,7 +50,13 @@ const composed = (top: AnyRec, coreExtra: AnyRec = {}): AnyRec => ({ /** One refused stack and one control. */ const STACKS: Record = { 'a list nothing names': composed({ picklists: [unused] }), - 'CONTROL: a body-declared list a body field names': composed({}, { objects: [caseObj(statusField)] }), + // [ADR-0150 D3] Forced edit: the core body declares its dependency on the + // app body, so the door accepts the whole stack — a sibling it does not + // depend on is refused (the third describe below). + 'CONTROL: a body-declared list a body field names': composed({}, { + dependencies: { 'com.example.parity.app': '^1.0.0' }, + objects: [caseObj(statusField)], + }), }; const kernels: ObjectKernel[] = []; @@ -99,6 +105,7 @@ describe('picklist-residual-unreferenced — the author-time door and the boot a const boot = await bootRefusal(stack); if (label.startsWith('CONTROL')) { expect(door).toEqual([]); + expect(judgePicklistReferences(stack)).toEqual({ refusals: [], notices: [] }); expect(boot?.message ?? '').not.toContain('is declared on the top level of stack'); return; } @@ -121,8 +128,9 @@ describe('picklist-residual-unreferenced — the author-time door and the boot a * options) because a runtime write is judged against the packages INSTALLED * here. So the agreement is pinned rather than inherited — and pinned where * the two can disagree: a list a SIBLING package of the same artifact owns, - * which this door resolves under ADR-0130 D4 and the save door must resolve - * because the boot registered it. + * which this door resolves through the declaring package's declared + * dependency on it (ADR-0150 D3) and the save door must resolve because the + * boot registered it. * * Asked of the save door's judge directly, over the real registry the boot * filled. `readMetaItem` is stubbed to "no stored row", so the case is a FIRST @@ -196,3 +204,76 @@ describe('[#29898] picklist-reference-unknown — the author-time door and the m expect(door[0].path?.endsWith(saveDoor!.issues![0].path)).toBe(true); }); }); + +/** + * [ADR-0150 D3; maintainer ruling N1, record 6729820123] The third parity: a + * field naming a list a SIBLING package of the same artifact declares, with no + * declared dependency on it. Since E6 the boot serves that field no options + * and warns with R's refusal (`findOutsideClosurePicklistReferences`), and the + * install judge refuses it; this door refuses it first, in the same sentence — + * both read `resolvePackageScopedHolders` over `packageResolutionClosure`. + */ +describe('[ADR-0150 D3] picklist-reference-unknown — a sibling\'s list with no declared dependency: the door and the boot say one sentence', () => { + const artifact = (dependencies?: Record): AnyRec => ({ + manifest: manifest(STACK_ID), + packages: [ + { manifest: { ...manifest('com.example.parity.core'), ...(dependencies ? { dependencies } : {}), objects: [caseObj(statusField)] } }, + { manifest: { ...manifest('com.example.parity.app'), picklists: [status] } }, + ], + }); + const outsideClosure = (engine: { registry: unknown } | undefined) => + (engine?.registry as { findOutsideClosurePicklistReferences(): Array<{ field: string; refusal?: string }> }) + .findOutsideClosurePicklistReferences(); + + it('the door refuses it, with the sentence the boot warns with', async () => { + const stack = artifact(); + const door = judgePicklistReferences(stack).refusals.filter((r) => r.rule === PICKLIST_REFERENCE_UNKNOWN); + const { refusal, engine } = await bootStack(stack); + + expect(refusal, String(refusal?.message)).toBeUndefined(); + expect(door).toHaveLength(1); + expect(door[0].path).toBe('packages[0].manifest.objects[0].fields.status.picklist'); + expect(outsideClosure(engine).map((o) => [o.field, o.refusal])).toEqual([['status', door[0].message]]); + }); + + it('CONTROL: with the dependency declared, the door accepts it and the boot warns nothing', async () => { + const stack = artifact({ 'com.example.parity.app': '^1.0.0' }); + const door = judgePicklistReferences(stack); + const { refusal, engine } = await bootStack(stack); + + expect(refusal, String(refusal?.message)).toBeUndefined(); + expect(door.refusals.filter((r) => r.rule === PICKLIST_REFERENCE_UNKNOWN)).toEqual([]); + expect(door.notices).toEqual([]); + expect(outsideClosure(engine)).toEqual([]); + }); + + + // [ADR-0150 D3] Two holders one reference reaches: the boot refuses them + // (`INVALID_METADATA`), quoting R's sentence, and so does the door. + const twoHolders = (core: AnyRec): AnyRec => ({ + manifest: manifest(STACK_ID), + packages: [ + { manifest: { ...manifest('com.example.parity.core'), ...core } }, + { manifest: { ...manifest('com.example.parity.app'), picklists: [status] } }, + { manifest: { ...manifest('com.example.parity.crm'), picklists: [status] } }, + ], + }); + + it.each([ + ['a field whose package depends on both holders', twoHolders({ + dependencies: { 'com.example.parity.app': '^1.0.0', 'com.example.parity.crm': '^1.0.0' }, + objects: [caseObj(statusField)], + })], + ['an extension whose target two packages hold', twoHolders({ + picklistExtensions: [{ extend: 'parity_status', options: [{ label: 'Closed', value: 'closed' }] }], + })], + ])('%s: the door refuses it, and the boot refuses it quoting the same sentence', async (_label, stack) => { + const door = judgePicklistReferences(stack).refusals.filter((r) => r.rule === PICKLIST_REFERENCE_UNKNOWN); + const { refusal } = await bootStack(stack); + + expect(door).toHaveLength(1); + expect(door[0].message).toContain('Nothing picks one of them'); + expect(refusal?.code).toBe('INVALID_METADATA'); + expect(refusal?.message).toContain(`(${door[0].message})`); + }); +}); diff --git a/packages/cli/src/utils/picklist-references.test.ts b/packages/cli/src/utils/picklist-references.test.ts index 5a9ca0ff075..e9fd40702cb 100644 --- a/packages/cli/src/utils/picklist-references.test.ts +++ b/packages/cli/src/utils/picklist-references.test.ts @@ -7,15 +7,20 @@ * * Pinned here: which references are judged (the load path's reading — the top * level when there is no `packages[]`, each body's own otherwise), what they - * resolve against (every picklist the stack declares), and which of the two - * verdicts an unresolved one gets (refused when the declaring package depends - * on nothing outside the stack; an `info` notice when it does) — the same two - * verdicts for a field and for an extension. Asserted by rule id, severity, - * the field or extension named in `where`, the list named in `message` and the - * `path` — never by the sentence around them. + * resolve against (a field's `picklist` from the package that declares it: + * its own list, then its declared dependency closure plus `sys`, ADR-0150 D3; + * an extension's `extend` with no caller), and which of the two verdicts an + * unresolved one gets (refused when the declaring package depends on nothing + * outside the stack; an `info` notice when it does) — the same two verdicts + * for a field and for an extension. Asserted by rule id, severity, the field + * or extension named in `where`, the list named in `message` and the `path` — + * never by the sentence around them, except where the sentence IS the + * contract: a refusal for a holder outside the closure carries the runtime's + * own (`packageScopedRefusalMessage`). */ import { describe, expect, it } from 'vitest'; +import { packageScopedRefusalMessage } from '@objectstack/core'; import { unclaimedTopLevel } from '@objectstack/metadata'; import { unreferencedResidualPicklists } from '@objectstack/metadata/picklist-references'; import { @@ -183,6 +188,205 @@ describe('judgePicklistReferences — a `packages[]` stack', () => { }); }); +/** + * [ADR-0150 D3; maintainer ruling N1, record 6729820123] A field's `picklist` + * resolves from the package that declares the field: its own list, then the + * unique holder in its declared dependency closure plus `sys`, else refused + * naming every holder. A sibling of the same artifact is in that closure only + * when the declaring package's manifest names it — the runtime refuses the + * rest since E6 (the boot warns, the install judge refuses), and this door + * now says so first, in the runtime's sentence. + */ +describe('judgePicklistReferences — a field naming a sibling package\'s list (ADR-0150 D3)', () => { + const core = (extra: Record = {}) => + ({ manifest: { ...manifest('com.example.core'), picklists: [industry], ...extra } }); + const orders = (extra: Record = {}) => + ({ manifest: { ...manifest('com.example.orders', extra), objects: [account('industry')] } }); + + it('refuses a sibling\'s list the declaring package does not depend on, in the runtime\'s sentence', () => { + const { refusals, notices } = judgePicklistReferences({ packages: [core(), orders()] }); + expect(notices).toEqual([]); + expect(refusals).toHaveLength(1); + expect(refusals[0]).toMatchObject({ + severity: 'error', + rule: PICKLIST_REFERENCE_UNKNOWN, + where: 'field "pk_account.industry"', + path: 'packages[1].manifest.objects[0].fields.industry.picklist', + }); + expect(refusals[0].message).toBe(packageScopedRefusalMessage({ + status: 'refused', + reason: 'outside-closure', + type: 'picklist', + name: 'industry', + callerPackageId: 'com.example.orders', + candidates: [{ kind: 'package', packageId: 'com.example.core' }], + })); + // The one-line fix: the dependency entry, on the package that declares the field. + expect(refusals[0].hint).toContain('`\'com.example.core\': \'^1.0.0\'`'); + expect(refusals[0].hint).toContain('`dependencies` of package \'com.example.orders\''); + }); + + it('names every holder when two siblings hold the list and the declaring package depends on neither', () => { + const { refusals } = judgePicklistReferences({ + packages: [core(), { manifest: { ...manifest('com.example.crm'), picklists: [industry] } }, orders()], + }); + expect(refusals).toHaveLength(1); + expect(refusals[0].message).toContain('package "com.example.core"; package "com.example.crm"'); + }); + + it('REPORTS, never refuses, when the declaring package also depends on a package this stack does not carry', () => { + const { refusals, notices } = judgePicklistReferences({ + packages: [core(), orders({ dependencies: { 'com.acme.crm': '^1.0.0' } })], + }); + expect(refusals).toEqual([]); + expect(notices).toHaveLength(1); + expect(notices[0]).toMatchObject({ + severity: 'info', + rule: PICKLIST_REFERENCE_UNVERIFIED, + path: 'packages[1].manifest.objects[0].fields.industry.picklist', + }); + expect(notices[0].message).toContain('\'industry\' is only in \'com.example.core\', undeclared'); + expect(notices[0].message).toContain('\'com.acme.crm\''); + expect(notices[0].hint).toContain('declare a dependency on \'com.example.core\' in package \'com.example.orders\''); + }); + + it('keeps that notice one verdict line of at most 200 characters, at realistic name lengths', () => { + const list = { ...industry, name: 'opportunity_industry_segment' }; + const { notices } = judgePicklistReferences({ + packages: [ + { manifest: { ...manifest('com.acmecorp.crm.sales.core'), picklists: [list] } }, + { + manifest: { + ...manifest('com.acmecorp.crm.sales.pipeline', { dependencies: { 'com.acmecorp.erp.integration': '^2.0.0' } }), + objects: [{ + name: 'crm_opportunity_line_item', + label: 'Line item', + fields: { industry_segment: { type: 'select', label: 'Segment', picklist: 'opportunity_industry_segment' } }, + }], + }, + }, + ], + }); + expect(notices).toHaveLength(1); + // `os explain picklist-reference-unverified` carries the why; the notice is the verdict. + expect(notices[0].message.length, notices[0].message).toBeLessThanOrEqual(200); + }); + + // An outside package can add a holder, never remove one: two holders the + // closure reaches are refused whatever the outside dependencies. + it('refuses two sibling holders the declaring package depends on both of, naming each — even with an outside dependency', () => { + const crm = { manifest: { ...manifest('com.example.crm'), picklists: [industry] } }; + const both = { 'com.example.core': '^1.0.0', 'com.example.crm': '^1.0.0' }; + for (const dependencies of [both, { ...both, 'com.acme.erp': '^1.0.0' }]) { + const { refusals, notices } = judgePicklistReferences({ packages: [core(), crm, orders({ dependencies })] }); + expect(notices).toEqual([]); + expect(refusals).toHaveLength(1); + expect(refusals[0]).toMatchObject({ + severity: 'error', + rule: PICKLIST_REFERENCE_UNKNOWN, + path: 'packages[2].manifest.objects[0].fields.industry.picklist', + }); + expect(refusals[0].message).toBe(packageScopedRefusalMessage({ + status: 'refused', + reason: 'ambiguous-in-closure', + type: 'picklist', + name: 'industry', + callerPackageId: 'com.example.orders', + candidates: [{ kind: 'package', packageId: 'com.example.core' }, { kind: 'package', packageId: 'com.example.crm' }], + })); + } + }); + + it('judges a field an `objectExtensions` entry adds from the EXTENDING package: a sibling\'s list it does not depend on is refused', () => { + const { refusals } = judgePicklistReferences({ + packages: [ + core({ objects: [{ name: 'pk_account', label: 'Account', fields: { name: { type: 'text', label: 'Name' } } }] }), + { + manifest: { + ...manifest('com.example.lists'), + picklists: [{ ...industry, name: 'segment' }], + }, + }, + { + manifest: { + ...manifest('com.example.orders', { dependencies: { 'com.example.core': '^1.0.0' } }), + objectExtensions: [{ extend: 'pk_account', fields: { segment: { type: 'select', label: 'Segment', picklist: 'segment' } } }], + }, + }, + ], + }); + expect(refusals).toHaveLength(1); + expect(refusals[0]).toMatchObject({ + rule: PICKLIST_REFERENCE_UNKNOWN, + where: 'field "pk_account.segment"', + path: 'packages[2].manifest.objectExtensions[0].fields.segment.picklist', + }); + expect(refusals[0].message).toContain('package "com.example.orders"'); + expect(refusals[0].message).toContain('package "com.example.lists"'); + }); + + it('CONTROL: the sibling declared as a dependency resolves', () => { + expect(judgePicklistReferences({ + packages: [core(), orders({ dependencies: { 'com.example.core': '^1.0.0' } })], + })).toEqual({ refusals: [], notices: [] }); + }); + + it('CONTROL: a sibling reached through a declared dependency\'s own dependency resolves — the closure is transitive', () => { + expect(judgePicklistReferences({ + packages: [ + core(), + { manifest: { ...manifest('com.example.base', { dependencies: { 'com.example.core': '^1.0.0' } }) } }, + orders({ dependencies: { 'com.example.base': '^1.0.0' } }), + ], + })).toEqual({ refusals: [], notices: [] }); + }); + + it('CONTROL: the declaring package\'s own list resolves, whatever a sibling holds', () => { + expect(judgePicklistReferences({ + packages: [core(), { manifest: { ...manifest('com.example.orders'), picklists: [industry], objects: [account('industry')] } }], + })).toEqual({ refusals: [], notices: [] }); + }); + + it('CONTROL: a `sys` package\'s list resolves with no declared dependency — `sys` is in every closure', () => { + expect(judgePicklistReferences({ + packages: [core({ namespace: 'sys' }), orders()], + })).toEqual({ refusals: [], notices: [] }); + }); + + it('CONTROL: an extension of a sibling\'s list needs no dependency — its `extend` resolves with no caller', () => { + expect(judgePicklistReferences({ + packages: [core(), { manifest: { ...manifest('com.example.orders'), picklistExtensions: [extension('industry')] } }], + })).toEqual({ refusals: [], notices: [] }); + }); + + // Ruling X: the target resolves with no caller, so it needs ONE holder in + // the stack; two are refused, naming each — even with an outside dependency. + it('refuses an extension whose target two packages of the stack hold, naming each', () => { + const crm = { manifest: { ...manifest('com.example.crm'), picklists: [industry] } }; + for (const extra of [{}, { dependencies: { 'com.acme.erp': '^1.0.0' } }]) { + const { refusals, notices } = judgePicklistReferences({ + packages: [core(), crm, { manifest: { ...manifest('com.example.orders', extra), picklistExtensions: [extension('industry')] } }], + }); + expect(notices).toEqual([]); + expect(refusals).toHaveLength(1); + expect(refusals[0]).toMatchObject({ + severity: 'error', + rule: PICKLIST_REFERENCE_UNKNOWN, + where: 'picklist extension "industry"', + path: 'packages[2].manifest.picklistExtensions[0].extend', + }); + expect(refusals[0].message).toBe(packageScopedRefusalMessage({ + status: 'refused', + reason: 'ambiguous-in-environment', + type: 'picklist', + name: 'industry', + callerPackageId: null, + candidates: [{ kind: 'package', packageId: 'com.example.core' }, { kind: 'package', packageId: 'com.example.crm' }], + })); + } + }); +}); + describe('judgePicklistReferences — a `picklistExtensions[].extend` in a one-package stack', () => { it('CONTROL: an extension naming a picklist the stack declares is neither refused nor reported', () => { expect(judgePicklistReferences({ @@ -447,9 +651,11 @@ describe('judgePicklistReferences — a composed stack\'s residual picklist that describe('picklistRefusalHeading — the text face\'s heading names what each rule refused', () => { const finding = (rule: string) => ({ severity: 'error' as const, rule, where: 'w', path: 'p', message: 'm', hint: 'h' }); - it('CONTROL: reference refusals alone keep the heading they always had', () => { + // Forced edit (#31832): the heading names the closure refusal (ADR-0150 D3) beside the unknown name. + it('CONTROL: reference refusals alone are headed as references', () => { expect(picklistRefusalHeading([finding(PICKLIST_REFERENCE_UNKNOWN), finding(PICKLIST_REFERENCE_UNKNOWN)])) - .toBe('A picklist reference names a picklist this stack does not declare (2 references)'); + .toBe('A picklist reference names a picklist this stack does not declare, or one only a package outside its ' + + 'package\'s declared dependencies holds (2 references)'); }); it('a residual refusal is headed as the boot\'s refusal, not as a reference', () => { diff --git a/packages/cli/src/utils/picklist-references.ts b/packages/cli/src/utils/picklist-references.ts index ce51b5249ff..0e54d2bc707 100644 --- a/packages/cli/src/utils/picklist-references.ts +++ b/packages/cli/src/utils/picklist-references.ts @@ -36,20 +36,27 @@ * `picklistExtensions`, bounded by that body's own `dependencies`, and * ⛔ NOT the top level, which the load path does not register from. * - * A reference RESOLVES against every picklist the stack declares — every - * `packages[]` body's `picklists`, or the top-level `picklists` when there is - * no `packages[]`. The release artifact is the co-ownership boundary - * (ADR-0130), so a list a SIBLING package in the same artifact owns resolves - * here exactly as one the declaring package owns. + * ## A reference resolves the way the runtime resolves it (ADR-0150 D3) * - * Note (2026-11-09): this sibling allowance narrows to declared dependencies. - * ADR-0150 D3 resolves a bare reference from the referring package, then the - * unique holder in its declared dependency closure (ADR-0141, plus `sys`), - * else refuses it naming every candidate; a sibling of the same artifact is in - * that closure only when the referring package declares it (maintainer ruling - * N1, record 6729820123; ADR-0130's dated note under D1). The code below still - * resolves against every body's picklists until the CLI reader is wired to - * that rule. + * The holders of a list are the bodies that declare it in `picklists` — every + * `packages[]` body, or the top level when there is no `packages[]`. A field's + * `picklist` resolves from the package that declares the field: that package's + * own list, then the unique holder in its declared dependency closure + * (ADR-0141, plus `sys`), else it is refused naming every candidate. A sibling + * package of the same artifact is in that closure only when the referring + * package's manifest declares it, and the author's fix is that one + * `dependencies` entry: the release artifact is ADR-0130's co-ownership + * boundary, not a resolution scope (maintainer ruling N1, record 6729820123; + * ADR-0130's dated note under D1). A `picklistExtensions` entry's `extend` is a + * contribution to another package's list, not a reference its package reads, + * so it resolves with no caller: the unique holder in the stack (epic-seat + * ruling X on the E6 card, ADR-0150 D3). + * + * The rule is the runtime's own, not a copy: `resolvePackageScopedHolders` + * (`@objectstack/core`) over the artifact's holders, with the closure from + * `packageResolutionClosure` (`@objectstack/metadata-core`) — the calls the + * registry's `picklistReferenceVerdict` makes, so the refusal here is the + * sentence the boot warns with and the install judge refuses with. * * ## A composed stack's top-level picklist that nothing names: refused * @@ -65,10 +72,12 @@ * A package may use a picklist another package owns — `picklistExtensions` * exists because one package adds values to a list it does not own. When that * owner is a declared dependency INSIDE this artifact, its picklists are in - * the resolution set above. When it is a dependency OUTSIDE the artifact, this + * the closure above. When it is a dependency OUTSIDE the artifact, this * command cannot read it: `manifest.dependencies` maps package IDS to version * ranges, which the installer resolves, and nothing maps such an id to a file - * the CLI could open. So a reference that resolves nowhere here is: + * the CLI could open. So a reference that resolves to no list here — nothing + * in the stack holds it, or only packages outside the declaring package's + * closure do — is: * * - REFUSED (`severity: 'error'`) when the declaring package names no * dependency outside this stack — nothing else could provide the list; @@ -77,9 +86,14 @@ * reject a stack that is correct. The notice names the dependencies, so the * skipped judgement is said out loud rather than read as a pass. * + * A reference that reaches TWO holders — two packages of the declaring + * package's closure for a field, two packages of the stack for an extension — + * is refused whatever the outside dependencies: an outside package can add a + * holder, never remove one, and the boot refuses the same ambiguity. + * * A field and an extension take the SAME verdict and the same two rule ids: - * what is judged is one fact — a name that resolves to no picklist the stack - * declares — and the finding's `where`, `path` and message say which key + * what is judged is one fact — a name that resolves to no list from where it + * is written — and the finding's `where`, `path` and message say which key * carried it. The extension is where the outside-dependency arm is most at * home, since an extension is how a package adds to a list it does not own. * @@ -102,7 +116,14 @@ */ import chalk from 'chalk'; +import { + packageScopedHolderSource, + resolvePackageScopedHolders, + type PackageScopedRefusal, + type PackageScopedResolution, +} from '@objectstack/core'; import type { AuthoringFinding } from '@objectstack/lint'; +import { packageResolutionClosure, type PackageClosureRegistry } from '@objectstack/metadata-core'; import { unclaimedTopLevel } from '@objectstack/metadata'; import { unreferencedResidualPicklists } from '@objectstack/objectql'; @@ -154,10 +175,20 @@ interface JudgedBody { /** Path prefix of this body in the parsed stack: `''` or `packages[1].manifest.`. */ prefix: string; body: AnyRec; + /** The package this body is: the one that holds its `picklists` and writes its fields' references. */ + packageId: string; + /** What the closure walk reads of it: `dependencies`, `namespace`, `id`. */ + manifest: AnyRec; /** Declared dependency ids this artifact does not carry. */ outside: string[]; } +/** + * The package a stack with no `packages[]` is, when its manifest names no id. + * It is the only holder there, so it is never named in a refusal. + */ +const UNNAMED_STACK_PACKAGE = 'this stack'; + function declaredDependencyIds(manifestLike: AnyRec | undefined): string[] { const declared = asRec(manifestLike?.dependencies); return declared ? Object.keys(declared) : []; @@ -173,18 +204,67 @@ function bodiesOf(parsed: AnyRec): JudgedBody[] { return packages.map((pkg) => ({ prefix: `packages[${pkg.index}].manifest.`, body: pkg.body, + packageId: pkg.id, + manifest: pkg.body, outside: declaredDependencyIds(pkg.body).filter((id) => !inside.has(id)), })); } const manifest = asRec(parsed.manifest); - const self = typeof manifest?.id === 'string' ? manifest.id : undefined; + const self = typeof manifest?.id === 'string' && manifest.id !== '' ? manifest.id : undefined; return [{ prefix: '', body: parsed, + packageId: self ?? UNNAMED_STACK_PACKAGE, + manifest: manifest ?? {}, outside: declaredDependencyIds(manifest).filter((id) => id !== self), }]; } +/** + * The artifact as the one closure walk (`packageResolutionClosure`) reads an + * installed registry: each body answers `getPackage` for its own id with its + * manifest — the first body of an id, as a holder holds a name once — and + * the bodies are every installed package. A declared dependency the artifact + * does not carry is still in the closure (the walk keeps a declared id it + * cannot produce); it holds nothing here, which is why it is reported, not + * judged (module header). + */ +function artifactRegistry(bodies: readonly JudgedBody[]): PackageClosureRegistry { + const byId = new Map(); + for (const judged of bodies) { + if (!byId.has(judged.packageId)) byId.set(judged.packageId, { manifest: judged.manifest }); + } + return { + getPackage: (id) => byId.get(id), + getAllPackages: () => [...byId.values()], + }; +} + +/** + * R's verdict on one reference (ADR-0150 D3), from the package that wrote it + * — `null` for an extension's `extend`, which resolves with no caller (module + * header) — over the holders the artifact declares. + */ +type ReferenceVerdict = PackageScopedResolution; + +function referenceResolver(bodies: readonly JudgedBody[]): (callerPackageId: string | null, picklist: string) => ReferenceVerdict { + const holdersOf = new Map(); + for (const judged of bodies) { + for (const name of picklistNamesOf(judged.body)) { + const holders = holdersOf.get(name) ?? []; + if (!holders.includes(judged.packageId)) holders.push(judged.packageId); + holdersOf.set(name, holders); + } + } + const registry = artifactRegistry(bodies); + const closureOf = (callerPackageId: string) => packageResolutionClosure(registry, callerPackageId); + return (callerPackageId, picklist) => resolvePackageScopedHolders( + { type: 'picklist', name: picklist, callerPackageId }, + packageScopedHolderSource((holdersOf.get(picklist) ?? []).map((id) => [id, true] as const)), + closureOf, + ); +} + function picklistNamesOf(body: AnyRec): string[] { const declared = body.picklists; if (!Array.isArray(declared)) return []; @@ -294,26 +374,98 @@ function quoteNames(names: readonly string[]): string { return `declared: ${quoted}${rest > 0 ? ` and ${rest} more` : ''}`; } +/** A version range a `dependencies` entry on `packageId` could carry: its declared version, caret-ranged. */ +function rangeOf(packageId: string, bodies: readonly JudgedBody[]): string { + const version = bodies.find((b) => b.packageId === packageId)?.manifest.version; + return typeof version === 'string' && version !== '' ? `^${version}` : '*'; +} + +/** + * The fix for a reference R refused (ADR-0150 D3): for a holder outside the + * declaring package's closure, the one `dependencies` entry that brings it in + * — the author's one-line fix (ruling N1); for two holders one reference + * reaches, keeping the list in one of them. + */ +function refusedReferenceHint(refusal: PackageScopedRefusal, bodies: readonly JudgedBody[]): string { + const holders = refusal.candidates.flatMap((c) => (c.kind === 'package' ? [c.packageId] : [])); + const caller = refusal.callerPackageId; + if (refusal.reason === 'outside-closure' && caller !== null) { + const declare = holders.length === 1 + ? `add \`'${holders[0]}': '${rangeOf(holders[0], bodies)}'\` to the \`dependencies\` of package '${caller}'` + : `add the one package whose list the field means (${holders.map((id) => `'${id}'`).join(' or ')}) ` + + `to the \`dependencies\` of package '${caller}'`; + return `Declare the dependency: ${declare}. Or declare the list in '${caller}' itself. ` + + 'Until then the installed field serves no options and its values are refused.'; + } + return `Keep '${refusal.name}' in one of ${holders.map((id) => `'${id}'`).join(', ')}: rename or remove the others' list, ` + + 'so the reference reaches one list.'; +} + +/** + * The notice's verdict and fix for a reference R refused as held only OUTSIDE + * the declaring package's closure, when that package also depends on a + * package this stack does not carry: short, because a notice is one line + * ({@link printPicklistReferenceNotices}) and `os explain` carries the why. + */ +function outsideClosureNotice( + ref: PicklistReference, + refusal: PackageScopedRefusal, + outside: readonly string[], +): { notice: string; noticeHint: string } { + const holders = dependencyList(refusal.candidates.flatMap((c) => (c.kind === 'package' ? [`'${c.packageId}'`] : []))); + return { + notice: `${ref.where}: '${ref.picklist}' is only in ${holders}, undeclared — not judged: ` + + `${dependencyList(outside.map((id) => `'${id}'`))} may hold it`, + noticeHint: `If one of those packages declares '${ref.picklist}', nothing is wrong. Otherwise declare a dependency ` + + `on ${holders} in package '${refusal.callerPackageId}'.`, + }; +} + /** * Every picklist reference in this stack — a field's `picklist`, an - * extension's `extend` — that resolves to no picklist the stack declares: - * refused, or reported when the declaring package names dependencies outside - * the stack. See the module header for the walk and the two verdicts. + * extension's `extend` — that resolves to no list from where it is written + * (ADR-0150 D3): refused, or reported when the declaring package names + * dependencies outside the stack and an outside package could still supply + * the list. See the module header for the walk, the rule and the two + * verdicts. * * Returns two empty lists for a stack whose every reference resolves. */ export function judgePicklistReferences(parsed: AnyRec): PicklistReferenceJudgement { const bodies = bodiesOf(parsed); const declared = [...new Set(bodies.flatMap((b) => picklistNamesOf(b.body)))]; - const known = new Set(declared); + const verdictOf = referenceResolver(bodies); const refusals: AuthoringFinding[] = []; const notices: AuthoringFinding[] = []; for (const judged of bodies) { for (const ref of referencesOf(judged)) { - if (known.has(ref.picklist)) continue; - const wording = wordingOf(ref, declared); - if (judged.outside.length === 0) { + const verdict = verdictOf(ref.site === 'extension' ? null : judged.packageId, ref.picklist); + if (verdict.status === 'resolved') continue; + // Nothing in the stack holds the name (`not-found`), or R refused it, + // naming every holder: the runtime's sentence, verbatim. + const unknown = wordingOf(ref, declared); + const wording = verdict.status === 'refused' + ? { + refusal: verdict.message, + refusalHint: refusedReferenceHint(verdict, bodies), + ...(verdict.reason === 'outside-closure' ? outsideClosureNotice(ref, verdict, judged.outside) : { notice: '', noticeHint: '' }), + } + : { + refusal: unknown.consequence, + refusalHint: unknown.refusalHint, + notice: + `${ref.where}: ${unknown.names} — not judged: it may come from ` + + `${dependencyList(judged.outside.map((id) => `'${id}'`))}, ` + + `${judged.outside.length === 1 ? 'a dependency' : 'dependencies'} this stack does not carry`, + noticeHint: unknown.noticeHint, + }; + // An outside package can only SUPPLY a list: it softens a name nothing + // here holds, or holds only outside the closure, to a notice. Two + // holders one reference reaches stay two whatever else is installed, + // so an ambiguity is refused even then — as the boot refuses it. + const outsideCouldSupply = verdict.status === 'not-found' || verdict.reason === 'outside-closure'; + if (judged.outside.length === 0 || !outsideCouldSupply) { // `where` names the field (or the extension) and every printer leads // the line with it, so the message does not repeat it. refusals.push({ @@ -321,7 +473,7 @@ export function judgePicklistReferences(parsed: AnyRec): PicklistReferenceJudgem rule: PICKLIST_REFERENCE_UNKNOWN, where: ref.where, path: ref.path, - message: wording.consequence, + message: wording.refusal, hint: wording.refusalHint, }); } else { @@ -334,10 +486,7 @@ export function judgePicklistReferences(parsed: AnyRec): PicklistReferenceJudgem // [#22161] One verdict sentence; why a dependency outside the stack // cannot be read is this rule's `RULE_EXPLANATIONS` entry // (`os explain picklist-reference-unverified`). - message: - `${ref.where}: ${wording.names} — not judged: it may come from ` + - `${dependencyList(judged.outside.map((id) => `'${id}'`))}, ` + - `${judged.outside.length === 1 ? 'a dependency' : 'dependencies'} this stack does not carry`, + message: wording.notice, hint: wording.noticeHint, }); } @@ -402,7 +551,10 @@ export function picklistRefusalHeading(refusals: readonly AuthoringFinding[]): s const references = refusals.length - residual; const parts: string[] = []; if (references > 0) { - parts.push(`a picklist reference names a picklist this stack does not declare (${references} reference${references > 1 ? 's' : ''})`); + parts.push( + 'a picklist reference names a picklist this stack does not declare, or one only a package outside its ' + + `package's declared dependencies holds (${references} reference${references > 1 ? 's' : ''})`, + ); } if (residual > 0) { parts.push(`a top-level picklist of this composed stack is one the boot refuses: no package body declares it and no object field the engine registers names it (${residual} picklist${residual > 1 ? 's' : ''})`); diff --git a/packages/lint/src/rule-explanations.ts b/packages/lint/src/rule-explanations.ts index 77604069b39..a661662a90e 100644 --- a/packages/lint/src/rule-explanations.ts +++ b/packages/lint/src/rule-explanations.ts @@ -5658,16 +5658,21 @@ const PICKLIST_REFERENCE_UNVERIFIED_EXPLANATION: RuleExplanation = { covers: 'why a picklist reference is not judged', paragraphs: [ 'A field\'s `picklist` and a picklist extension\'s `extend` name a shared option list. `os ' + - 'validate` and `os build` resolve the name against every picklist the stack declares (every ' + - '`packages[]` body\'s `picklists`, or the top-level `picklists`) and refuse one that resolves ' + - 'nowhere, as `picklist-reference-unknown`.', + 'validate` and `os build` resolve them the way the runtime does (ADR-0150 D3). A field\'s ' + + '`picklist` resolves from the package that declares the field: its own `picklists`, then the ' + + 'packages its manifest declares as dependencies, transitively, plus every `sys` package. A sibling ' + + 'package of the same artifact counts only when it is declared. An extension\'s `extend` resolves ' + + 'against the one package of the stack that holds the list. A name that resolves to no list, only ' + + 'to lists held outside the declaring package\'s declared dependencies, or to two holders, is ' + + 'refused as `picklist-reference-unknown`.', 'When the package that declares the reference depends on a package this stack does not carry, the ' + 'list could live there, and this command cannot read it: `manifest.dependencies` maps package ids ' + 'to version ranges the installer resolves, and nothing maps such an id to a file the CLI could ' + 'open. So the reference gets this notice instead of a refusal: it is not judged, and the notice ' + 'never gates a run, not even under `--strict`.', 'If one of the named dependencies declares the list, nothing is wrong. Otherwise declare the list ' + - 'in this stack, or correct the name. A reference saved at runtime through Studio or the REST ' + + 'in this stack, declare a dependency on the package of this stack that holds it, or correct the ' + + 'name. A reference saved at runtime through Studio or the REST ' + 'metadata API is not judged here.', ], };