Skip to content

solidity loader: resolve @-scoped imports via Node-style walk-up#38

Merged
exo-nikita merged 5 commits into
masterfrom
claude/dreamy-hypatia-NZPSQ
May 28, 2026
Merged

solidity loader: resolve @-scoped imports via Node-style walk-up#38
exo-nikita merged 5 commits into
masterfrom
claude/dreamy-hypatia-NZPSQ

Conversation

@exo-nikita
Copy link
Copy Markdown
Collaborator

Summary

  • Adds a Node-style walk-up fallback for @scope/pkg/... imports when no remapping covers them.
  • resolveNodeModulesSpec(baseDir, specifier) walks up from baseDir looking for <dir>/node_modules/<pkg>/package.json (mirroring Node's CJS resolution). Returns the subpath relative to baseDir, or null if the package can't be found, the subpath contains .., or the resolution would escape baseDir.
  • resolveSolImport stays pure (no I/O); the Node fallback is layered on at both call sites (collectSolidityFilesFromDisk and buildSolidityTree).
  • New fixture tests/fixtures/solidity-bundle/nm-fallback/ covers a project that resolves @oz/contracts/... purely via node_modules — no remappings.txt needed.

Test plan

  • node --run test — 301 tests pass
  • node --run lint — clean
  • CLI smoke: cd nm-fallback && stasis bundle src/A.sol -o out.br resolves @oz/contracts/utils/Math.sol to node_modules/@oz/contracts/utils/Math.sol
  • Path-traversal guard: @oz/contracts/../../etc/passwd returns null
  • Out-of-baseDir resolution (parent monorepo's node_modules) returns null

https://claude.ai/code/session_01YFi2BPkgGhtb2vXKZKL2it


Generated by Claude Code

The hardcoded `node_modules/<spec>` fallback couldn't see monorepos,
didn't verify the package existed, and accepted `..` segments verbatim
(letting a malicious solidity source read sibling files).

Replace it with `resolveNodeModulesSpec(baseDir, specifier)`:
- splits `@scope/pkg/sub/path` correctly
- walks up from baseDir looking for `<dir>/node_modules/<pkg>/package.json`
- rejects `..` in the subpath
- rejects resolutions outside baseDir (the Bundle layer can't store them)

`resolveSolImport` is pure again. The Node fallback is applied at both
call sites (`collectSolidityFilesFromDisk` already has baseDir;
`buildSolidityTree` now accepts it as an option, threaded through from
`buildSolidityBundle`).
@exo-nikita exo-nikita force-pushed the claude/dreamy-hypatia-NZPSQ branch from c80a562 to f7d38ca Compare May 27, 2026 19:38
claude added 4 commits May 27, 2026 19:41
Drop the manual `while (true)` node_modules walk-up. Node already
implements the algorithm via createRequire(anchor).resolve(specifier);
anchoring with a sentinel path inside baseDir makes it walk up from
there. The path-traversal guard (no `..` segments) and the "must stay
inside baseDir" check stay — Node would happily resolve to a sibling
file or a parent monorepo's node_modules, neither of which fit in a
Bundle's workspace bucket.
resolveNodeModulesSpec(baseDir, fromFile, specifier) now anchors
createRequire at <baseDir>/<fromFile> instead of <baseDir>/noop.js.
That mirrors Node's actual algorithm: a file under node_modules/foo/
looks in foo/node_modules/ first, only then walking up to the top-
level node_modules.

Adds a nm-nested fixture that exercises this: src/A.sol imports
@dep/x/Y.sol; Y.sol in turn imports @inner/z/Z.sol, which lives in
@dep/x's own node_modules. The bundle ends up with three buckets —
workspace, @dep/x, and the nested @inner/z under @dep/x.
Drop the separate resolveNodeModulesSpec + resolveNodeModulesRel pair
and inline Node-style resolution as a third strategy inside
resolveSolImport itself. The function now takes an options bag
({ remappings, baseDir }) and tries:

  1. remappings (longest prefix wins)
  2. relative paths (..-escape returns null)
  3. createRequire(<baseDir>/<fromFile>).resolve(<spec>) for
     @scope/pkg/... specifiers, when baseDir is provided

Both callers shrink to a single resolveSolImport call. Path-traversal
guards (..) and the bundle-relative conversion stay; they're just
collocated with the strategy that needs them now.
…olve()

path.resolve takes multiple segments and normalizes them — there's no
need to resolve baseDir first and then join.
@exo-nikita exo-nikita merged commit eee5297 into master May 28, 2026
2 checks passed
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