Skip to content

fix(parser): resolve grammar wasm when dependencies are hoisted (npm / npx) - #165

Closed
Johnson1662 wants to merge 2 commits into
Done-0:mainfrom
Johnson1662:fix/hoisted-wasm-path
Closed

Johnson1662 wants to merge 2 commits into
Done-0:mainfrom
Johnson1662:fix/hoisted-wasm-path

Conversation

@Johnson1662

Copy link
Copy Markdown

Problem

resolveWasmPath() hard-codes <pkg>/node_modules/tree-sitter-wasms/out/*.wasm. That layout only exists with pnpm-style nested installs. Under npm and npx the dependency is hoisted next to the package, so every grammar failed to load and the tool silently fell back to the regex parser:

[WARN] Tree-sitter init failed for javascript, falling back to regex parser: Error: ENOENT:
no such file or directory, open '.../node_modules/eff-u-code/node_modules/tree-sitter-wasms/out/tree-sitter-javascript.wasm'

This is what npx eff-u-code / a global npm install hits — i.e. most users.

Why it matters

The regex fallback loses scope structure, so a function nested inside another function gets its end pinned to the end of the enclosing scope. Everything after it is attributed to it. On one 403-line file:

reported actual
record() L128–377, 250 lines, complexity 40 L128–147, 20 lines, complexity 2
readLines() L380–398, 6 lines correct

Overall score for that repository: 83.9 → 92.0 once real ASTs were used, and most of the top-10 'worst' list disappeared.

Fix

Ask the module resolver first, keep the nested path as a fallback for pnpm:

const require = createRequire(import.meta.url);

function resolveWasmPath(wasmFile: string): string {
  try {
    return require.resolve(`tree-sitter-wasms/out/${wasmFile}`);
  } catch {
    return pathResolve(__dirname, '..', '..', 'node_modules', 'tree-sitter-wasms', 'out', wasmFile);
  }
}

Verification

  • npm run build clean
  • npm test: 205 passed (14 files)
  • npm run lint and npm run format:check clean
  • Manually: no more [WARN] ... falling back to regex parser, and the nested function above now reports 20 lines

Optional follow-up

If the wasm still cannot be loaded, the regex fallback is silent apart from a [WARN]. Failing loudly (or surfacing it in the report) would have made this bug obvious sooner — happy to do that in a separate PR if you want it.

tree-sitter-wasms was resolved through a hard-coded path relative to this
package (`node_modules/tree-sitter-wasms/out/*.wasm`), which only exists
with pnpm-style nested installs. Under npm and npx the dependency is
hoisted next to the package, so every language failed with ENOENT and
silently fell back to the regex parser:

  [WARN] Tree-sitter init failed for javascript, falling back to regex parser:
  ENOENT ... eff-u-code/node_modules/tree-sitter-wasms/out/tree-sitter-javascript.wasm

The regex parser loses nesting, so a function nested inside another scope
had its end pinned to the end of that scope. In one repository a 20-line
function was reported as 250 lines with complexity 40, and the overall
score moved 83.9 -> 92.0 once real ASTs were used.

Resolve through the module resolver first and keep the nested path as a
fallback for pnpm.
Copilot AI balanced review requested due to automatic review settings November 15, 2026 08:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Johnson1662 added a commit to Johnson1662/dsh-inbox that referenced this pull request Nov 15, 2026
根因不是它的函数边界算法,而是 wasm 路径写死成包内 node_modules(pnpm 布局);
npm/npx 会提升依赖,所有语法解析 ENOENT,静默退化成正则,嵌套函数把外层代码吞进自己长度。

- 上游修复已提 PR:Done-0/fuck-u-code#165
- npm run rot 暂指向自己的 fork 分支
- AGENTS/CONTRIBUTING 换成准确描述,并给出判断依据(-v 看有没有 falling back 警告)
- 同一仓库分数:正则 83.9 → 真 AST 92.0
The silent fallback hid a broken wasm path for months: every score looked
plausible while the parser could not see scopes at all. Track the languages
that fell back and print a one-line summary at the end of a run, plus an
actionable warning at the point of failure.
@Johnson1662

Copy link
Copy Markdown
Author

Closing this — the project has had no commits since 2026-09-07, so I've been carrying both fixes in a fork instead (Johnson1662/fuck-u-code, tag v2.2.2-fork.1): the wasm resolution fix in this PR, plus a follow-up so a grammar that fails to load no longer degrades to the regex parser silently.

If development picks up again, both commits apply cleanly — feel free to reopen or cherry-pick. Thanks for the tool; it found real problems in my code once the parser was actually loading.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants