Skip to content

pickPrimaryBaseFile(): "Flag 2" (exitedIndex) is tracked but never read to return early #12

Done

Description

@Shinrai

pickPrimaryBaseFile() in src/lib/resolve-from-caller.mjs implements a documented two-flag state machine for finding the real caller outside the package's own infrastructure:

// Two-flag state machine for detecting when we've exited package infrastructure:
// 1) Flag 1: Set when we exit src/ or dist/ directories
// 2) Flag 2: Set when we exit index.* entry point files
// 3) Once both flags are set, we've found the actual caller outside the package

The function's own docstring example spells out the intended behavior explicitly:

 * @example
 * // Stack trace flow (wisp example):
 * // 0: src/lib/resolve-from-caller.mjs [in src/]
 * // 1: src/wisp.mjs [still in src/]
 * // 2: index.mjs [exited src/ - Flag 1 set]
 * // 3: user-code.mjs [exited index.* - Flag 2 set, return this!]

In the actual implementation, only Flag 1 (exitedSrcOrDist) triggers a return:

// Flag 1: Check if we've exited src/dist (was in, now not in)
if (previousWasInSrcOrDist && !currentIsInSrcOrDist) {
    exitedSrcOrDist = true;
}

// Flag 2: Check if we've exited index.* (was index, now not index)
if (previousWasIndex && !currentIsIndex) {
    exitedIndex = true;
}

// If we've exited src/dist, we've found the caller (index.mjs might be optimized away)
if (exitedSrcOrDist) {
    return f;
}

exitedIndex is set but never read anywhere — there's no if (exitedIndex) return f; (or combined check) matching the docstring's "Flag 2 set, return this!" comment. Currently the function only ever returns via the Flag‑1 path (or null if the loop exhausts the stack).

Found while wiring up eslint for the first time on this repo (no-unused-vars flagged exitedIndex) — see commit 5229277 on next. I deliberately did not try to "complete" the feature myself; suppressing the lint finding with an explanatory comment instead, since I don't have enough context to know whether:

  • this is a real bug (Flag 2 should independently trigger a return, e.g. when V8 inlines/optimizes away index.mjs so Flag 1 never fires, per the "index.mjs might be optimized away" comment a few lines below), or
  • Flag 2 was deliberately left inert (e.g. superseded by the Flag‑1 path handling the same case in practice) and the docstring/comments are just stale, or
  • something else entirely was intended.

Whoever has context on the original design should decide whether to wire up the missing if (exitedIndex) return f; path, rewrite the docstring to drop the Flag‑2‑triggers‑return claim, or something else. Current code: src/lib/resolve-from-caller.mjs lines ~163–232 (function pickPrimaryBaseFile), docstring at ~157–181.

Activity

  1. added
    stale
    on Nov 13, 2026
  2. cldmv-bot commented on Nov 13, 2026

    @cldmv-bot
    Contributor

    This issue has been inactive for 60 days. It will be auto-closed in 14 days if there is no further activity. Comment to keep it open, add the pinned label to exempt it permanently, or close it if it is no longer relevant.

  3. removed
    stale
    on Nov 14, 2026
  4. added
    status: implemented
    on Nov 28, 2026
  5. cldmv-bot commented on Nov 28, 2026

    @cldmv-bot
    Contributor

    Marked status: implemented — resolved by #89, merged into next. This issue closes automatically when that work ships to the default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: implemented

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions