Skip to content

fix(javascript): honor package exports condition order - #591

Merged
morluto merged 3 commits into
morluto:mainfrom
rudycelekli:campaign/rea-package-condition-order-20261005
Oct 5, 2026
Merged

morluto merged 3 commits into
morluto:mainfrom
rudycelekli:campaign/rea-package-condition-order-20261005

Conversation

@rudycelekli

@rudycelekli rudycelekli commented Oct 5, 2026 •

Copy link
Copy Markdown

Summary

Select supported default/import/require package export conditions in declared insertion order. Real source-owned installed-package Node import and require subprocesses prove default-before-specific selects default; no expansion of unsupported nested/array conditions.

No tracking issue: this is a focused correctness regression found through production-seam reproduction.

Problem and expected behavior

Module relationship selection now respects supported condition insertion order; nested and array-condition coverage remains as before.

Change and scope

The change stays in the owning adapter/application stage and corrects existing behavior without adding tools or broadening provider coverage.

Contract and boundary impact

  • Semantic owner and earliest changed stage: Application package export-condition selection, before selecting a package entry artifact.
  • CLI and MCP/tool-catalog contract: existing schemas and tool inventory remain intact; corrected results flow through the owning operation.
  • Provider, bridge, target-format, or platform compatibility: Actual Node import/require executions ran for the changed condition-order cases. No claim is made about additional Node export-map forms.
  • Evidence, artifact, provenance, or reconstruction contract: Module relationship selection now respects supported condition insertion order; nested and array-condition coverage remains as before.
  • Process execution, authorization, cleanup, or containment impact: Existing execution authority, cleanup and containment remain preserved.
  • Generated metadata, package, or installation impact: no metadata or dependency changes.

Evidence and regression coverage

Two base-revision mismatches; 30 focused tests pass. Real installed-package Node import and require subprocesses select the earlier default condition.

Tests exercise production parsing, filesystem, HTTP, or browser seams, without module mocks. Evidence above is executed; unverified platform/provider coverage is stated explicitly.

Validation performed

All required local gates passed on the signed commit:

  • npm run check:pr
  • npm run check:ci
  • npm run verify:package
  • npm pack --dry-run

Development runtime: Node.js 24.18.0 and npm 11.16.0. Upstream CI is separate from these local results.

Compatibility, safety, and release

No migration or package dependency changes. Real Hopper/Ghidra verification was not run; this patch does not change those providers. Remaining proof limits are stated above. The final diff contains source and tests only.

Prepared with AI assistance.

Rudy Celekli and others added 2 commits October 5, 2026 12:05
The declared-order fix selected targets against {default, import|require},
but Node's active set for the resolver that owns installed packages also
includes "node" and "node-addons". Ordering only means something against the
full active set, so a package declaring {"node", "import"} resolved to the
wrong target, and a package declaring only {"import"} reported no target
where Node resolves ./node.cjs.

Nested condition objects and fallback arrays are also valid Node targets.
They are now flattened in Node's own order: a condition object stops at its
first active key, so a null or unmatched nested target ends the search, while
an array skips the entries it cannot resolve and concatenates the rest. Node
does not fall through a target whose file is absent, so the first flattened
target stands and its absence is reported rather than skipped.

A valid exports map that matches no active condition is reported as external
with the declared condition names, and one that selects an unsupported nested
or array target no longer borrows the "not valid package JSON" reason.

The differential test installs a real package and resolves the same bare
specifier through Node and through REA, mirroring file presence between the
filesystem and the inventory. 18 of the 25 cases fail without this change.
@morluto

morluto commented Oct 5, 2026

Copy link
Copy Markdown
Owner

Pushed 23eb8820 onto this branch. The declared-order fix is correct — I verified default-first and specific-first against a real Node subprocess in both directions, and the original test's "default precedes" claim holds.

Three things were left, all reachable through the function this PR changes:

1. Ordering was applied to a partial condition set. The active set is {default, import|require}, but Node's active set for the resolver that owns installed packages also includes node and node-addons. Declared order is only meaningful against the full set, so:

exports Node before
{node, import} node.cjs specific.cjs
{node, import} + require node.cjs no target
{node-addons, import} node.cjs specific.cjs

2. Nested condition objects and array fallbacks were reported as malformed. Both are valid Node inputs. Flattening them needed care about where Node stops:

  • a condition object takes its first active key and stops — a matched null target does not fall through to a later key, so {import:{node:null, default:"./d.js"}} is ERR_PACKAGE_PATH_NOT_EXPORTED
  • an array skips what it cannot resolve and concatenates the rest, so {import:[null, "./array.cjs"]} resolves
  • Node does not fall through a target whose file is absent — {import:["./absent.cjs","./array.cjs"]} is ERR_MODULE_NOT_FOUND, not array.cjs. So the first flattened target stands and its absence is reported.

3. "not valid package JSON" was returned for valid metadata. A package whose exports match no active condition, or whose target is nested/array, was reported as malformed JSON. Those are now external naming the declared conditions, and unavailable naming the unexpanded target, so a caller can tell a package that does not export this entry from one REA could not read.

The test now mirrors file presence between the real filesystem and the inventory and asserts REA's leaf equals Node's stdout, pinning both sides. 18 of its 25 cases fail against the pre-change file.

One thing I did not change, since your PR body already scopes it out: subpath maps are still only resolved via the "." key, so a bare specifier against {"./feature": "./feature.js"} stays unresolved. That agrees with Node refusing the bare specifier, so it is consistent — flagging it only because the nested-condition and array-fallback gaps are now closed and this is the remaining known one.

@morluto
morluto merged commit 8289595 into morluto:main Oct 5, 2026
18 checks passed
@rudycelekli

Copy link
Copy Markdown
Author

Thanks for extending the active condition set and adding the real Node comparisons. I reviewed and preserved those additions after this merged.

The additional controls found two remaining distinctions in that expanded path: a nested condition object with no match should allow the next outer condition, whereas an explicit null target blocks it; array fallback should skip invalid targets, stop at the first usable target, and retain the final invalid-versus-null outcome if none succeeds. I prepared the focused follow-up in #606, preserving your admission checks and existing diagnostics.

Five of the 34 actual Node controls failed on merged main before that follow-up; all 34 plus the 28 application path-resolution tests pass afterward. npm run check and npm run check:fast also pass on its signed final head under the current guidelines. The target validation remains scoped to the supported relative-prefix contract; this does not claim complete Node export-target grammar or expanded subpath-pattern support.

@morluto

morluto commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Thanks, @rudycelekli, for your work on “fix(javascript): honor package exports condition order.” I appreciate your contribution to REA.

darsrc pushed a commit to darsrc/rea that referenced this pull request Oct 10, 2026
* fix(javascript): honor package exports condition order

Signed-off-by: Rudy Celekli <[email protected]>

* fix(javascript): resolve exports against Node's full condition set

The declared-order fix selected targets against {default, import|require},
but Node's active set for the resolver that owns installed packages also
includes "node" and "node-addons". Ordering only means something against the
full active set, so a package declaring {"node", "import"} resolved to the
wrong target, and a package declaring only {"import"} reported no target
where Node resolves ./node.cjs.

Nested condition objects and fallback arrays are also valid Node targets.
They are now flattened in Node's own order: a condition object stops at its
first active key, so a null or unmatched nested target ends the search, while
an array skips the entries it cannot resolve and concatenates the rest. Node
does not fall through a target whose file is absent, so the first flattened
target stands and its absence is reported rather than skipped.

A valid exports map that matches no active condition is reported as external
with the declared condition names, and one that selects an unsupported nested
or array target no longer borrows the "not valid package JSON" reason.

The differential test installs a real package and resolves the same bare
specifier through Node and through REA, mirroring file presence between the
filesystem and the inventory. 18 of the 25 cases fail without this change.

* style: apply Prettier to the conditional exports differential test

---------

Signed-off-by: Rudy Celekli <[email protected]>
Co-authored-by: morluto <[email protected]>
@morluto morluto added bug Something isn't working area: build Build, CI, packaging, and publishing labels Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: build Build, CI, packaging, and publishing bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants