diff --git a/.changeset/18203-translation-target-contributed-nav.md b/.changeset/18203-translation-target-contributed-nav.md new file mode 100644 index 00000000000..a9ffca4939a --- /dev/null +++ b/.changeset/18203-translation-target-contributed-nav.md @@ -0,0 +1,27 @@ +--- +"@objectstack/lint": patch +--- + +`translation-target-unknown` no longer calls a locale key for a CONTRIBUTED navigation item an orphan — the remedy it printed deleted a translation the runtime honours (#18203) + +`validateTranslationReferences` built the `apps..navigation.*` universe from the app's authored `navigation` array alone. An item injected by another package through `manifest.navigationContributions` (ADR-0029 D7, ADR-0130) is never in that array, so every locale key for it was reported as naming an item *"which app X does not declare"*, at `error` since 17.4.0, with the remedy *"Match the key to the navigation item's `id`, or drop it."* + +⚠️ **That remedy is wrong in the worst direction a false positive can point: following it deletes a working translation.** Measured on `objectstack-ai/hotcrm` `be11c07` (pin 17.4.0), where a service module contributes five items into `crm_enterprise`: + +| | measured | +| :-- | :-- | +| `os build` | **15** findings — 5 contributed items × 3 non-default locales | +| `GET /api/v1/meta/app?id=crm_enterprise` | returns all 5 items, `zh-CN` labels **resolved** from the app's own pack | + +The universe now folds in every contribution aimed at the app, walked by the same `walkNav` a declared subtree gets, so what the rule judges is the population the runtime serves rather than the array the author typed. + +**Both carriers are read**, because a stack in hand has two shapes and `os build` runs the rule table over both: + +- `packages[].manifest.navigationContributions` — the ADR-0130 D4 artifact entry. This is the shape the per-package leg needs (`compile.ts` step 3b-ii): the app's owning package declares no contribution of its own, and the union run above it de-duplicates, so a fix reading only the union would have left that leg reporting the finding alone. +- `manifest.navigationContributions` — the stack's own `StackSchema.manifest`, where a single-`defineStack` project's contributions live. `os validate` judges only the union stack, so reading the artifact form alone would have left the fast inner-loop command still reporting what the build no longer does. + +**The runtime's fold is deliberately not imported, and the union is faithful anyway.** `@objectstack/lint` depends on `@objectstack/spec` and never on a runtime; `applyNavContributions` is a `SchemaRegistry` method in `@objectstack/objectql`. A second implementation would normally be exactly the drift this class of defect is made of — except that the fold pushes the contributed items in *every* branch: into a `group` that resolves, at the app top level when the `group` id names nothing (a `nav_contribution_group_missing` diagnostic, never a refusal), and at the top level when `group` is omitted. It chooses **where** an item lands and never **whether**, so the set of addressable ids is invariant under it. All three placements are pinned side by side so that invariant cannot quietly stop holding. + +**The control, which is the point of the change.** Widening a universe trades a false positive for a blind spot unless the genuine orphan still reports. A key that nothing contributes is still an `error` carrying `translation-target-unknown`, its path and its message; a contribution aimed at app B does not make its ids addressable under app A; and the contributed ids join the population the hint enumerates, so the remedy an author is handed lists what they may actually key to. + +**What this still cannot see, stated rather than implied.** Contributions registered imperatively by plugin code (`engine.registerAppNavContribution` from a plugin's `init`) are not metadata, and no static rule can read them — that is the population `pnpm check:app-nav-i18n` has to boot a composition to judge. A locale key for one of those is still reported here. diff --git a/packages/lint/src/validate-translation-references.test.ts b/packages/lint/src/validate-translation-references.test.ts index e4972419c20..a5bc87f43f9 100644 --- a/packages/lint/src/validate-translation-references.test.ts +++ b/packages/lint/src/validate-translation-references.test.ts @@ -599,6 +599,222 @@ describe('validateTranslationReferences — apps, dashboards, global actions', ( }); }); +/** + * Navigation CONTRIBUTED into an app by another package (#18203). + * + * `manifest.navigationContributions` (ADR-0029 D7) injects nav items into an + * app the contributing package does not own. Those items are NOT in the target + * app's `navigation` array, so a universe built from that array alone reports + * every locale key for them as naming an item "which app X does not declare" — + * with the advice *"Match the key to the navigation item's `id`, or drop it"*. + * + * ⚠️ That advice DELETES a translation the runtime honours. Measured downstream + * on `objectstack-ai/hotcrm` `be11c07`: five contributed items, 15 findings + * (5 × 3 non-default locales), while `GET /api/v1/meta/app?id=crm_enterprise` + * returns all five WITH their `zh-CN` labels resolved from the app's own pack. + * + * The three `group` cases below are the load-bearing ones, and they are why the + * universe can be a union rather than a re-implementation of the runtime fold: + * `SchemaRegistry.applyNavContributions` PUSHES the items in every branch — into + * the resolved group, or at the app top level when the group id names nothing + * (which is a diagnostic, never a refusal; see `NavigationContributionSchema`'s + * header). The fold chooses WHERE an item lands, never WHETHER. So a rule that + * only asks "is this id addressable?" cannot diverge from the fold, and pinning + * all three placements here is what keeps that true. + */ +describe('validateTranslationReferences — contributed navigation (#18203)', () => { + /** Package `crm_core` owns the app; package `crm_service` contributes into it. */ + const contributedArtifact = (contributions: unknown[]) => ({ + packages: [ + { + manifest: { + id: 'crm_core', + apps: [ + { + name: 'crm_enterprise', + navigation: [ + { id: 'group_sales', type: 'group', children: [{ id: 'nav_leads', type: 'object', objectName: 'crm_lead' }] }, + { id: 'group_service', type: 'group', children: [] }, + ], + }, + ], + }, + }, + { manifest: { id: 'crm_service', navigationContributions: contributions } }, + ], + objects: [{ name: 'crm_lead', fields: { name: { type: 'text' } } }], + apps: [ + { + name: 'crm_enterprise', + navigation: [ + { id: 'group_sales', type: 'group', children: [{ id: 'nav_leads', type: 'object', objectName: 'crm_lead' }] }, + { id: 'group_service', type: 'group', children: [] }, + ], + }, + ], + }); + + const localeKeys = (...navIds: string[]) => [ + { + 'zh-CN': { + apps: { + crm_enterprise: { + label: '企业版 CRM', + navigation: Object.fromEntries(navIds.map((id) => [id, { label: id }])), + }, + }, + }, + }, + ]; + + it('accepts a locale key for an item contributed into a group the app DOES declare', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'crm_enterprise', group: 'group_service', priority: 100, items: [{ id: 'nav_case', type: 'object', objectName: 'crm_case' }] }, + ]), + translations: localeKeys('nav_case'), + }); + expect(findings).toEqual([]); + }); + + it('accepts one contributed into a group the app does NOT declare — the fold relocates, it does not drop', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'crm_enterprise', group: 'group_nonexistent', items: [{ id: 'nav_knowledge', type: 'object', objectName: 'crm_kb' }] }, + ]), + translations: localeKeys('nav_knowledge'), + }); + expect(findings).toEqual([]); + }); + + it('accepts one contributed with no `group` at all — appended at the app top level', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'crm_enterprise', items: [{ id: 'nav_service_dashboard', type: 'dashboard' }] }, + ]), + translations: localeKeys('nav_service_dashboard'), + }); + expect(findings).toEqual([]); + }); + + it('accepts a CHILD of a contributed group — contributed items carry subtrees', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { + app: 'crm_enterprise', + group: 'group_service', + items: [{ id: 'nav_reports', type: 'group', children: [{ id: 'nav_report_sla', type: 'report' }] }], + }, + ]), + translations: localeKeys('nav_reports', 'nav_report_sla'), + }); + expect(findings).toEqual([]); + }); + + it('accepts the whole HotCRM set — the five items #18203 measured as 15 findings', () => { + const items = ['nav_case', 'nav_knowledge', 'nav_service_dashboard', 'nav_my_cases', 'nav_report_sla']; + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'crm_enterprise', group: 'group_service', priority: 100, items: items.map((id) => ({ id, type: 'object' })) }, + ]), + translations: localeKeys(...items), + }); + expect(findings).toEqual([]); + }); + + /** + * ⭐ The control. Widening a universe trades a false positive for a blind spot + * unless the genuine orphan still reports — so the same stack, one key that + * NOTHING contributes, has to stay an error with its id and severity intact. + */ + it('still reports a genuinely unknown navigation id, at `error`, with the rule id', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'crm_enterprise', group: 'group_service', items: [{ id: 'nav_case', type: 'object' }] }, + ]), + translations: localeKeys('nav_case', 'nav_deleted_surface'), + }); + expect(findings).toHaveLength(1); + expect(findings[0]).toMatchObject({ + rule: TRANSLATION_TARGET_UNKNOWN, + severity: 'error', + path: 'translations[0]["zh-CN"].apps.crm_enterprise.navigation.nav_deleted_surface', + }); + expect(findings[0].message).toContain('which app "crm_enterprise" does not declare'); + // The contributed id joins the population the hint enumerates, so the + // remedy an author is handed lists what they may actually key to. + expect(findings[0].hint).toContain('nav_case'); + }); + + /** + * A contribution is aimed at ONE app (`NavigationContributionSchema.app`), so + * it may not make its ids addressable under a different one — otherwise the + * widening would silence every app in an artifact at once. + */ + it('does NOT accept a key under app A for an item contributed into app B', () => { + const findings = validateTranslationReferences({ + ...contributedArtifact([ + { app: 'other_app', group: 'group_service', items: [{ id: 'nav_case', type: 'object' }] }, + ]), + translations: localeKeys('nav_case'), + }); + expect(findings).toHaveLength(1); + expect(findings[0].path).toBe('translations[0]["zh-CN"].apps.crm_enterprise.navigation.nav_case'); + }); + + /** + * The single-package shape. A project that is one `defineStack` carries no + * `packages[]`; its contributions sit on the stack's own top-level + * `manifest` (`StackSchema.manifest`). `os validate` judges exactly this + * shape, so reading only the artifact form would leave the fast inner-loop + * command reporting the false positive the build no longer does. + */ + it('reads contributions off the stack\'s own top-level `manifest` when there is no `packages[]`', () => { + const findings = validateTranslationReferences({ + manifest: { + id: 'crm_service', + navigationContributions: [ + { app: 'crm_enterprise', group: 'group_service', items: [{ id: 'nav_case', type: 'object' }] }, + ], + }, + apps: [{ name: 'crm_enterprise', navigation: [{ id: 'group_service', type: 'group', children: [] }] }], + translations: localeKeys('nav_case'), + }); + expect(findings).toEqual([]); + }); + + /** + * The per-PACKAGE leg of `os build` (`compile.ts` step 3b-ii) hands each + * package its OWN body as the stack and the artifact's `packages[]` as + * resolution context (`packageBodyAsStack`, #16611). The app-owning package + * carries the translations and none of the contributions, so this is the + * shape in which the false positive actually reached the HotCRM author — + * the union run above it de-duplicates, so a fix that only worked on the + * union would leave this leg reporting it alone. + */ + it('accepts the key in the per-package leg, where the app owner declares no contribution itself', () => { + const artifactPackages = [ + { manifest: { id: 'crm_core', apps: [{ name: 'crm_enterprise', navigation: [{ id: 'group_service', type: 'group', children: [] }] }] } }, + { + manifest: { + id: 'crm_service', + navigationContributions: [ + { app: 'crm_enterprise', group: 'group_service', items: [{ id: 'nav_case', type: 'object' }] }, + ], + }, + }, + ]; + const ownerBody = { + id: 'crm_core', + apps: [{ name: 'crm_enterprise', navigation: [{ id: 'group_service', type: 'group', children: [] }] }], + translations: localeKeys('nav_case'), + }; + // `packageBodyAsStack(body, entries)` — the body IS its own manifest. + const findings = validateTranslationReferences({ ...ownerBody, manifest: ownerBody, packages: artifactPackages }); + expect(findings).toEqual([]); + }); +}); + describe('validateTranslationReferences — flows (#7646 / #11287)', () => { /** * One stack, shared by every case below — the clean run and the three diff --git a/packages/lint/src/validate-translation-references.ts b/packages/lint/src/validate-translation-references.ts index 872ee9d3290..ca631c6215e 100644 --- a/packages/lint/src/validate-translation-references.ts +++ b/packages/lint/src/validate-translation-references.ts @@ -601,6 +601,100 @@ function readOptions(field: AnyRec): { values: Set; byLabel: Map 0 ? { values, byLabel } : undefined; } +/** + * Target app name → every `navigationContributions[].items` array aimed at it + * (ADR-0029 D7, #18203). + * + * ## Why the universe has to include these at all + * + * A package injects nav items into an app it does not own by declaring + * `manifest.navigationContributions`. Those items never enter the target app's + * authored `navigation` array — the runtime merges them on every read + * (`SchemaRegistry.applyNavContributions`, which `GET /api/v1/meta/app` reaches + * through the protocol). So a universe built from the authored array alone + * calls every locale key for a contributed item an orphan, and this rule's + * remedy for an orphan is `or drop it` — which deletes a translation the + * runtime honours. Measured on `objectstack-ai/hotcrm` `be11c07`: five + * contributed items, 15 findings across three non-default locales, while the + * app endpoint returned all five with those very labels resolved. + * + * ## Why this reads the declaration and does not re-run the runtime's fold + * + * ⛔ `applyNavContributions` is NOT imported, and not because it is awkward to + * reach: `@objectstack/lint` depends on `@objectstack/spec` and never on a + * runtime (this package's own `description`), and that fold is a method on + * `@objectstack/objectql`'s `SchemaRegistry`. `os build` reaches the engine + * lazily for the *group* question (`cli/src/utils/nav-contribution-groups.ts`), + * whose own header records why that check could not be a lint rule. + * + * The reason a union is nevertheless FAITHFUL here, rather than a second + * implementation free to drift, is a property of the fold: it pushes the items + * in EVERY branch. A `group` that resolves receives them; a `group` that names + * nothing gets a `nav_contribution_group_missing` diagnostic and the items are + * appended at the app top level; an omitted `group` appends at the top level + * too (`NavigationContributionSchema`'s header states the same). The fold + * chooses WHERE an item lands and never WHETHER — so the set of addressable + * ids is invariant under it, and the only question this rule asks is whether an + * id is in that set. Reproducing the placement would pull a runtime in to + * compute a set the placement cannot change. The three placements are pinned + * side by side in this rule's test file so the invariant cannot quietly stop + * being true. + * + * ## Which two carriers are read, and why both + * + * Contributions reach a stack in hand in exactly two shapes, and `os build` + * runs the rule table over both of them (`compile.ts` step 3b / 3b-ii): + * + * 1. `packages[].manifest.…` — the ADR-0130 D4 artifact entry, whose body + * half is the assembled package body. Read with the same reach + * `validateObjectReferences`' `artifactProvidedObjectNames` uses (#16611), + * because it answers the same question: what does THIS ARTIFACT provide, + * beyond the collections the stack in hand carries at its top level. It is + * the shape the per-package leg needs — the app's owner declares no + * contribution of its own, so without this its stack sees none. + * 2. `manifest.…` — the stack's own `StackSchema.manifest`, which is where a + * single-`defineStack` project's contributions live. `os validate` judges + * only the union stack, so reading the artifact form alone would leave the + * fast inner-loop command reporting the false positive the build no longer + * does. + * + * ⚠️ Both are read unconditionally, which is a deliberate difference from + * `collectNavGroupInputs`, whose top-level manifest is read ONLY when there is + * no `packages[]`. That either/or exists to stop a DIAGNOSTIC being emitted + * twice for one mis-aim, since composition also picks a singular top-level + * `manifest` that is a copy of one package's. The output here is set + * membership, where a duplicate is free and a miss is the defect being fixed. + * + * ⛔ What this cannot see, stated rather than implied: contributions registered + * IMPERATIVELY by plugin code (`engine.registerAppNavContribution` from a + * plugin's `init`) are not metadata and no static rule can read them. That is + * the population `packages/cli/scripts/check-app-nav-i18n.mjs` has to BOOT a + * composition to judge — see its header. A key for one of those is still + * reported here. + */ +function contributedNavItemsByApp(stack: AnyRec): Map { + const byApp = new Map(); + const add = (contributions: unknown) => { + for (const contribution of recordsOf(contributions)) { + const targetApp = strName(contribution.app); + // A contribution names ONE target app. Items aimed elsewhere must not + // become addressable here, or one widening would silence every app in + // the artifact at once. + if (!targetApp) continue; + const items = byApp.get(targetApp) ?? []; + items.push(contribution.items); + byApp.set(targetApp, items); + } + }; + add(isRec(stack.manifest) ? stack.manifest.navigationContributions : undefined); + for (const entry of recordsOf(stack.packages)) { + const body = entry.manifest; + if (!isRec(body)) continue; + add(body.navigationContributions); + } + return byApp; +} + /** * Collect every name a translation bundle may resolve against. Built once per * run: the same universe answers all bundles and all locales. @@ -707,6 +801,7 @@ function buildUniverse(stack: AnyRec): Universe { } // ── Apps: navigation item ids (`apps..navigation..label`) ── + const contributedNav = contributedNavItemsByApp(stack); const apps = new Map>(); for (const app of recordsOf(stack.apps)) { const appName = strName(app.name); @@ -725,6 +820,10 @@ function buildUniverse(stack: AnyRec): Universe { if (areaId) navIds.add(areaId); walkNav(area.navigation); } + // [#18203] …and everything CONTRIBUTED into this app, walked by the same + // `walkNav` so a contributed subtree resolves exactly as a declared one + // does. This is the population the runtime serves, not the authored array. + for (const items of contributedNav.get(appName) ?? []) walkNav(items); apps.set(appName, navIds); }