Skip to content

perf(build): only cross-resolve internal aliases#4371

Merged
pi0 merged 3 commits into
nitrojs:mainfrom
TheAlexLichter:perf/skip-irrelevant-alias-resolution
Jun 22, 2026
Merged

perf(build): only cross-resolve internal aliases#4371
pi0 merged 3 commits into
nitrojs:mainfrom
TheAlexLichter:perf/skip-irrelevant-alias-resolution

Conversation

@TheAlexLichter

@TheAlexLichter TheAlexLichter commented Jun 21, 2026

Copy link
Copy Markdown
Member

This PR skips package aliases that cannot participate in Nitro’s internal alias resolution, reducing unnecessary nested comparisons. It preserves ~, @, and # alias behavior while leaving package aliases unchanged, with regression coverage. Benchmarks show improvements from roughly 3× on small alias sets to 48× on large sets.

@TheAlexLichter TheAlexLichter requested a review from pi0 as a code owner June 21, 2026 19:02
@vercel

vercel Bot commented Jun 21, 2026

Copy link
Copy Markdown

@TheAlexLichter is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a88e4f84-572d-4390-8036-9644215aff1e

📥 Commits

Reviewing files that changed from the base of the PR and between fd49215 and ab3134c.

📒 Files selected for processing (2)
  • src/build/config.ts
  • test/unit/build-config.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/build/config.ts

📝 Walkthrough

Walkthrough

src/build/config.ts introduces ROOT_ALIAS constant and refactors resolveAliases to precompute eligible alias keys, skip non-resolvable values early, enforce a root-alias (@/ prefix) guard, and extract isResolvableAliasKey and isResolvableAliasValue helper predicates. A new Vitest file test/unit/build-config.test.ts validates the refactored resolution behavior end-to-end.

Changes

resolveAliases Refactor and Test

Layer / File(s) Summary
Constants, refactored resolveAliases, and helper predicates
src/build/config.ts
Adds ROOT_ALIAS constant for the root (@) alias; refactors the resolveAliases loop to precompute eligible keys via isResolvableAliasKey, short-circuit non-resolvable values via isResolvableAliasValue, and enforce a @/ prefix guard for the root alias; extracts isResolvableAliasKey and isResolvableAliasValue as standalone predicates.
Unit tests for resolveAliases
test/unit/build-config.test.ts
Introduces a Vitest suite with two tests: the first validates internal (#-prefixed) aliases chained through each other alongside unchanged package-scoped aliases; the second covers "double root" aliases (~~ and @@) that expand to root-prefixed paths, both asserting exact resolved mappings.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format (type: description) and accurately summarizes the main optimization change to the alias resolution logic.
Description check ✅ Passed The description is directly related to the changeset, explaining the performance optimization, preserved behavior, and benchmark improvements from the alias resolution refactoring.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Jun 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4371

commit: ab3134c

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/unit/build-config.test.ts (1)

5-23: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Add a regression case where an internal alias points to a scoped package path.

This test doesn’t currently lock the “scoped package aliases should not participate in internal resolution” behavior. Adding one input like "#ext": "@scope/package/subpath" and asserting it stays unchanged would prevent this class of regression.

Suggested test extension
     expect(
       resolveAliases({
         "~": "/root",
         "@": "/root",
         "`#app`": "`@/app`",
         "`#build`": "`#app/build`",
+        "`#ext`": "`@scope/package/subpath`",
         package: "/node_modules/package",
         "`@scope/package`": "/node_modules/@scope/package",
       })
     ).toEqual({
       "`@scope/package`": "/node_modules/@scope/package",
       "`#build`": "/root/app/build",
+      "`#ext`": "`@scope/package/subpath`",
       package: "/node_modules/package",
       "`#app`": "/root/app",
       "~": "/root",
       "@": "/root",
     });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/unit/build-config.test.ts` around lines 5 - 23, The test for the
resolveAliases function is missing a regression case to verify that internal
aliases pointing to scoped package paths are not processed during resolution.
Add a new input to the test object passed to resolveAliases with a key like
"`#ext`" that maps to a scoped package path value such as
"`@scope/package/subpath`", and then add the corresponding assertion in the
expected output object to verify that this alias remains unchanged in the
result. This ensures the function does not accidentally attempt to resolve
scoped package paths within internal alias definitions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/build/config.ts`:
- Around line 106-113: The issue is that scoped package aliases (those starting
with "@") are being classified as resolvable by the isResolvableAlias function,
causing them to be included in the resolvableAliases array when they should be
skipped entirely to match the stated intent. Update the isResolvableAlias
function to explicitly exclude scoped packages by checking if the alias starts
with "@" and returning false for those cases, which will prevent unnecessary
comparisons in the inner loop at lines 106 and 125 and properly filter out all
package aliases regardless of scope.

---

Nitpick comments:
In `@test/unit/build-config.test.ts`:
- Around line 5-23: The test for the resolveAliases function is missing a
regression case to verify that internal aliases pointing to scoped package paths
are not processed during resolution. Add a new input to the test object passed
to resolveAliases with a key like "`#ext`" that maps to a scoped package path
value such as "`@scope/package/subpath`", and then add the corresponding assertion
in the expected output object to verify that this alias remains unchanged in the
result. This ensures the function does not accidentally attempt to resolve
scoped package paths within internal alias definitions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 52316a9b-fad7-4ef4-a936-5fcb31e01c03

📥 Commits

Reviewing files that changed from the base of the PR and between f9f328e and 45c734d.

📒 Files selected for processing (2)
  • src/build/config.ts
  • test/unit/build-config.test.ts

Comment thread src/build/config.ts Outdated
@pi0x pi0x changed the title perf(build): skip irrelevant alias resolution perf(build): only cross-resolve internal aliases Jun 22, 2026
Generalize internal-alias detection so all-`@` prefixes (`@`, `@@`) are
cross-resolved like `~`/`~~`/`#`, while still excluding scoped packages
(`@scope/pkg`). Fixes `~~` vs `@@` asymmetry and adds a regression test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@pi0 pi0 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thnx! Only fixed to keep supporting @@

@pi0 pi0 merged commit 6de81ee into nitrojs:main Jun 22, 2026
9 of 10 checks passed
@TheAlexLichter TheAlexLichter deleted the perf/skip-irrelevant-alias-resolution branch June 22, 2026 08:38
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