# Node Resolve Plugin Review Rules

> Auto-generated by pr-review-rules-generator skill on 2026-02-24
> Source: git history mining (DEF commits) + domain expertise interview

## Overview

Rules for the node resolver and filesystem adapter layer (`src/node-resolve/`, `src/resolver/`,
`src/node-fs-adapter/`, `src/web-fs-loader/`). This is high-churn code: `resolveImportSpecifiers.js`
has 11 changes and `package/utils.ts` has 8. Multiple defects have been filed against this area.

## Rules

---

## Rule ID: fs_adapter_api_contract

**Summary:** FS wrapper functions must conform to the resolver's expected API contract

**Rationale:** DEF0744564 found that the `readFile`, `stat`, and `readdir` wrappers in the
filesystem adapters did not match the exact API shape expected by the resolve engine — return
types, error codes, and async patterns differed between the Node adapter and the browser (lightning-
fs) adapter. This caused silent resolution failures that were only reproducible with specific
package structures. The fix required extensive test additions (417 lines of tests added in a single
commit) to pin down the contract.

**Scope:**
- Paths: `src/node-resolve/**`, `src/resolver/**`, `src/node-fs-adapter/**`, `src/web-fs-loader/**`

**Detection Guidance:** When reviewing changes to any filesystem adapter method (`readFile`,
`stat`, `realpath`, `readdir`, `exists`), verify: (1) the return type matches what the resolver
layer expects — check `src/fs-iface.ts` for the interface definition, (2) error handling returns
the same error codes (e.g., `ENOENT`) that the resolver checks for, (3) async methods return
Promises consistently (no mixing of sync/async for the same operation), and (4) new adapter
methods have tests in `test/resolver/`. If you see the interface in `src/fs-iface.ts` changed,
flag this as a potential breaking change that requires all adapter implementations to be updated.

**Severity:** `error`

**Source:**
- Type: `incident`
- Refs: DEF0744564, fd1484b
- Confidence: high

**Override:**
- Allowed: false
- Requires: n/a

**Examples:**

### Violation Example
```typescript
// ❌ Bad: returns undefined instead of throwing ENOENT — resolver can't distinguish missing from error
async readFile(path: string): Promise<string> {
  try {
    return await fs.readFile(path, 'utf-8');
  } catch {
    return undefined; // resolver expects an ENOENT error, not undefined
  }
}
```

### Correct Example
```typescript
// ✅ Good: propagates ENOENT so the resolver can make correct decisions
async readFile(path: string): Promise<string> {
  return fs.readFile(path, 'utf-8'); // let ENOENT propagate
}
```


---

## Rule ID: no_hardcoded_package_names

**Summary:** Resolver logic must not hardcode specific package names (e.g., `path-browserify`)

**Rationale:** DEF0729925 found that `path-browserify` was hardcoded as the browser replacement
for the `path` module in `resolvePackageTarget.ts` and `package/utils.ts`. This caused issues when
the package was not available or when an alternate polyfill was used. Package-to-browser-polyfill
mappings should be read from configuration or the `browser` field in `package.json`, not hard-
coded in resolver logic.

**Scope:**
- Paths: `src/node-resolve/**`, `src/resolver/**`

**Detection Guidance:** Search the diff for string literals that are npm package names (e.g.,
`'path-browserify'`, `'buffer'`, `'stream-browserify'`, `'process'`). If a package name is used
as a hard-coded fallback or replacement target, flag it. The correct approach is to derive the
browser mapping from the package's `browser` field in `package.json` or from an explicitly
configurable polyfill map.

**Severity:** `error`

**Source:**
- Type: `incident`
- Refs: DEF0729925, 54b2c28
- Confidence: high

**Override:**
- Allowed: true
- Requires: justification


---

## Rule ID: browser_fs_adapter_interface_parity

**Summary:** Browser (lightning-fs) and Node (native fs) adapters must maintain interface parity

**Rationale:** The isomorphic design of this package requires that the browser filesystem adapter
(`@servicenow/glider-lightning-fs`) and the Node native fs adapter expose the same interface to
the rollup plugins. Historically, subtle API differences (missing methods, different error shapes,
synchronous vs asynchronous) have caused bugs that only manifest in one environment. Each time
the resolver is changed, both adapters must be considered.

**Scope:**
- Paths: `src/node-fs-adapter/**`, `src/web-fs-loader/**`, `src/fs-iface.ts`

**Detection Guidance:** If `src/fs-iface.ts` changes (interface definition), verify both adapter
implementations are updated. If a new method is added to the Node adapter, check whether the
browser adapter has an equivalent. Look for any places where Node-specific methods are called
without a browser-compatible fallback. If the PR only tests with one environment, flag this as
incomplete coverage.

**Severity:** `warning`

**Source:**
- Type: interview
- Refs: interview, DEF0744564
- Confidence: high

**Override:**
- Allowed: true
- Requires: justification


---

## Attribution

All rules are traceable to specific sources:
- `incident` = Direct from production defect (DEF*)
- `interview` = Captured during domain expertise interview
- `mined` = Derived from git history analysis

## Override Mechanism

To override a rule in a PR:

1. **In PR description:**
   ```markdown
   <!-- pr-review: skip fs_adapter_api_contract reason="Justification here" -->
   ```

2. **In repo config (`.pr-review-config.yaml`):**
   ```yaml
   overrides:
     - rule_id: no_hardcoded_package_names
       paths: ["path/to/exclude/**"]
       reason: "Why this path is exempt"
   ```

Note: Rules marked `override.allowed: false` cannot be skipped without approval.
