Skip to content

Bug: Rule 5 / Condition C03 is reachable dead code (multi-default "flatten without default" never executes) #97

Description

@Shinrai

Bug: Rule 5 / Condition C03 is reachable dead code (multi-default "flatten without default" never executes)

Summary

The C03 branch of getFlatteningDecision() — "multi-default context without default export" — produces a flattenToRoot: true decision that no downstream consumer reads. The decision is computed, the matching processModuleForAPI() path prepares the correct flat moduleContent payload, and then modes-processor.mjs silently drops the flag and assigns the payload as a nested namespace just like any non-flattening file. Net result: the documented C03 behavior never happens at runtime, in any location (root, nested, or api.add() mount).

This is not just an untested branch — empirical tracing confirms C03 is reached on real fixtures and produces output that nothing consumes.

Affected locations

  • src/lib/processors/flatten.mjs:74-86 — #checkMultiDefault() returns {flattenToRoot: true, ...} for C03 inputs
  • src/lib/processors/flatten.mjs:272-289 — processModuleForAPI() honors flattenToRoot and prepares moduleContent = {<named exports>} ready for parent-merge
  • src/lib/builders/modes-processor.mjs — has no consumer of decision.flattenToRoot. Only decision.flattenToCategory is consumed (line 741, addapi-only path). grep flattenToRoot src/lib/builders/modes-processor.mjs returns zero hits.

Empirical reproduction

Fixture:

multi/
├── a.mjs       export default function aFn(x) { return "a:" + x; }
├── b.mjs       export default function bFn(x) { return "b:" + x; }
└── utils.mjs   export function helper(x) { return "helper:" + x; }
                export const VERSION = "1.0";

Trace (instrumented Flatten.prototype.getFlatteningDecision and Flatten.prototype.processModuleForAPI):

[DEC] moduleName="a"     hasMultipleDefaults=true hasDefault=true  → {preserveAsNamespace:true}
[DEC] moduleName="b"     hasMultipleDefaults=true hasDefault=true  → {preserveAsNamespace:true}
[DEC] moduleName="utils" hasMultipleDefaults=true hasDefault=false → {flattenToRoot:true}
[PROC] moduleName="utils" → moduleContentType:"object" moduleContentKeys:["VERSION","helper"] decisionInput:{flattenToRoot:true}

Result:
api.multi keys:       [ 'a', 'b', 'utils' ]
api.multi.utils keys: [ 'VERSION', 'helper' ]
api.multi.helper:     undefined        ← NOT hoisted

Same outcome (decision reached, hoist never happens) in all three locations:

Scenario C03 reached? Hoisted to parent? utils still nested?
Files at the api ROOT yes (flattenToRoot:true) no (api.helper undefined) yes (api.utils.{helper,VERSION})
Files at multi/sub/ (nested) yes no (api.multi.sub.helper undefined) yes
Files mounted via api.slothlet.api.add("mounted", folder) yes no (api.mounted.helper undefined) yes

Doc vs. code inversion (separate but related bug)

The docs at docs/API-RULES/API-RULES-CONDITIONS.md:47-97 describe C02 and C03 inverted relative to the actual code:

  • Docs say C02 (multi-default + has-default) → shouldFlatten: true, flattenType: "root". Code returns preserveAsNamespace: true.
  • Docs say C03 (multi-default + no-default) → shouldFlatten: false, flattenType: "namespace". Code returns flattenToRoot: true.

Both doc blocks also cite source path src/lib/helpers/api_builder/decisions.mjs ~L113-L125 — that file and directory do not exist. The real location is src/lib/processors/flatten.mjs.

The runtime behavior happens to match the docs ("preserve as namespace") for both C02 and C03, but only because: (a) C02's preserveAsNamespace is honored explicitly, and (b) C03's flattenToRoot is silently ignored so the consumer falls through to the standard namespace-assignment path.

Test coverage of C03

  • No unit test calls getFlatteningDecision({hasMultipleDefaults: true, hasDefault: false}) and asserts the returned shape.
  • No fixture exists for the C03 pattern (folder with 2+ default-exporting files plus a no-default sibling).
  • No integration test asserts the user-visible behavior for the C03 layout.
  • The "Verified" badge on docs/API-RULES.md:249 (Rule 5) cites api_tests/api_test as evidence, but that fixture's root doesn't contain the multi-default + no-default sibling pattern. The doc's validators/email.mjs + validators/phone.mjs "Verified Example" code references a fixture that doesn't exist in the repo.

The branch hits 100% coverage only because the line gets executed during normal flow — nothing pins the meaning of what it returns or what happens downstream.

Expected behavior (if C03 were wired up)

Layout:

api/
└── notifications/
    ├── email.mjs       export default function send(to, msg) {...}
    ├── sms.mjs         export default function send(to, msg) {...}
    └── helpers.mjs     export function formatPhone(p) {...}
                        export function formatEmail(e) {...}
                        export const RETRY_LIMIT = 3

Today's behavior:

api.notifications.email("a@x", "hi")          // callable (default export preserved)
api.notifications.sms("+1...", "hi")          // callable (default export preserved)
api.notifications.helpers.formatPhone("...")  // NESTED — extra ".helpers" hop
api.notifications.helpers.RETRY_LIMIT         // NESTED
api.notifications.formatPhone                 // undefined

If C03 worked as the code's intent suggests:

api.notifications.email("a@x", "hi")    // callable (preserved — has default)
api.notifications.sms("+1...", "hi")    // callable (preserved — has default)
api.notifications.formatPhone("...")    // HOISTED — promoted to folder namespace
api.notifications.formatEmail("...")    // HOISTED
api.notifications.RETRY_LIMIT           // HOISTED
api.notifications.helpers               // doesn't exist; the .helpers hop dissolves

Semantic intent: "files with a default = entities (email, sms); files without a default = stuff that lives alongside the entities." The .helpers. hop is boilerplate the developer didn't ask for; C03 was meant to dissolve it.

This is the multi-file analog of the within-file pattern where export default fn + export const X merges named exports onto the function. It also matches the addapi.mjs always-hoist pattern (flattenToCategory), just gated on the multi-default heuristic instead of a magic filename.

Collisions (e.g., helpers.mjs exporting email while email.mjs exists in the same folder) would route through the existing collisionMode config (merge / error / warn / skip) — slothlet already has that machinery.

Three fix paths

  1. Wire it up. Add a if (decision.flattenToRoot) { ...merge moduleContent into targetApi... } branch in modes-processor.mjs matching the existing addapi flattenToCategory block. This is a behavior change: a folder with mixed default + named-only files starts merging the named-only file's exports to the folder. Most aligned with the code's obvious original intent. Needs a feature-test fixture and integration tests covering root / nested / api.add() locations and the collision modes.

  2. Remove the dead branch. Change flatten.mjs:82-85 to return preserveAsNamespace: true for C03 as well, rewrite the C03 doc block to say "preserves like C02," and pin the now-explicit behavior with a unit test + integration test. Cleanest cleanup; abandons the feature.

  3. Leave-and-document. Keep the dead branch, add a comment at flatten.mjs:82 explaining "currently inert — consumer doesn't read flattenToRoot; runtime falls through to standard namespace assignment." Add tests that pin both the decision shape and the user-visible namespace-preservation outcome. Most honest about the current state without committing to a direction.

Option (1) is the most useful and matches the documented intent (after fixing the doc inversion); options (2) and (3) are valid cleanups if the feature isn't wanted.

Cross-cutting fixes regardless of path chosen

  • Fix the C02 / C03 doc-vs-code inversion in docs/API-RULES/API-RULES-CONDITIONS.md.
  • Fix the stale source path (src/lib/helpers/api_builder/decisions.mjs → src/lib/processors/flatten.mjs).
  • Remove or correct the validators/email.mjs + validators/phone.mjs "Verified Example" in docs/API-RULES.md:249 (Rule 5) — the cited fixture doesn't exist.
  • Add a unit test pinning C02's return shape (preserveAsNamespace: true) — currently only one such test exists, but it should be paired with the C03 test to document both branches.

Activity

  1. self-assigned this
    on May 26, 2026
  2. added
    type: bug
    priority: low
    status: confirmed
    on May 26, 2026
  3. added theissue type on May 26, 2026
  4. linked a pull request that will close this issuerelease: v3.9.0 - Browser mode #119on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority: lowstatus: confirmedtype: bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions