Skip to content

fix(plugin): use traceInclude - #27

Merged
pi0 merged 1 commit into
unjs:mainfrom
peterhirn:main
Mar 19, 2026
Merged

pi0 merged 1 commit into
unjs:mainfrom
peterhirn:main

Conversation

@peterhirn

@peterhirn peterhirn commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor
import { defineConfig } from "vite";
import { externals } from "nf3/plugin";

export default defineConfig({
  build: {
    rollupOptions: {
      input: ["src/server.js"],
      plugins: [
        externals({
          traceInclude: ["node_modules/import-in-the-middle/hook.mjs"],
        }),
      ],
    },
  },
});

Note: The example config in the readme contains: traceInclude: ["some-lib"]. Maybe this should be changed to traceInclude: ["some-lib/index.js"]?

Summary by CodeRabbit

  • Bug Fixes
    • Bundling now respects additional configured trace-includes, ensuring extra paths are considered during tracing.
    • This improves dependency resolution and reduces missed assets during builds.

@coderabbitai

coderabbitai Bot commented Mar 9, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR updates writeBundle in src/plugin.ts to resolve and add each entry from opts.traceInclude into the tracedPaths set (using absolute check, tryResolve, or resolving against rootDir) before running the existing tracing logic.

Changes

Cohort / File(s) Summary
Path Tracing Enhancement
src/plugin.ts
In writeBundle, iterate over opts.traceInclude, resolve entries to absolute paths when needed, and add them to the tracedPaths set so traceNodeModules considers these additional paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I nibble through opts and tracedPaths too,
Resolving routes where little paths grew,
Each included trace a bright new track,
I hop and add, then never look back,
A rabbit’s map — small and true!

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(plugin): use traceInclude' directly and clearly describes the main change - enabling the use of the traceInclude option in the plugin, which is confirmed by the code changes that implement traceInclude iteration in writeBundle.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@coderabbitai coderabbitai Bot 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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/plugin.ts (1)

127-131: ⚠️ Potential issue | 🟡 Minor

Early return bypasses traceInclude when tracedPaths is empty.

The guard condition at line 127 returns early if tracedPaths.size === 0, but this now incorrectly skips tracing when only traceInclude has values. If a user specifies traceInclude paths but no modules are auto-traced during the build, those explicit paths will never be traced.

🐛 Proposed fix to include `traceInclude` in the guard check
-        if (opts.trace === false || tracedPaths.size === 0) {
+        const traceIncludePaths = opts.traceInclude ?? [];
+        if (opts.trace === false || (tracedPaths.size === 0 && traceIncludePaths.length === 0)) {
           return;
         }
         const { traceNodeModules } = await import("./trace.ts");
-        await traceNodeModules([...tracedPaths, ...(opts.traceInclude ?? [])], {
+        await traceNodeModules([...tracedPaths, ...traceIncludePaths], {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/plugin.ts` around lines 127 - 131, The early-return currently checks
opts.trace === false || tracedPaths.size === 0 which skips tracing when
tracedPaths is empty but opts.traceInclude contains explicit paths; change the
guard to only return when tracing is disabled OR both tracedPaths.size === 0 AND
(opts.traceInclude is missing or empty). Locate the block using opts.trace,
tracedPaths, traceNodeModules and traceInclude in plugin.ts and update the
conditional accordingly so traceNodeModules is invoked with the combined list
[...tracedPaths, ...(opts.traceInclude ?? [])] when any paths exist.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/plugin.ts`:
- Around line 127-131: The early-return currently checks opts.trace === false ||
tracedPaths.size === 0 which skips tracing when tracedPaths is empty but
opts.traceInclude contains explicit paths; change the guard to only return when
tracing is disabled OR both tracedPaths.size === 0 AND (opts.traceInclude is
missing or empty). Locate the block using opts.trace, tracedPaths,
traceNodeModules and traceInclude in plugin.ts and update the conditional
accordingly so traceNodeModules is invoked with the combined list
[...tracedPaths, ...(opts.traceInclude ?? [])] when any paths exist.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4731aa11-e070-45c8-b757-abfcd80312da

📥 Commits

Reviewing files that changed from the base of the PR and between e0a683a and 772c81f.

📒 Files selected for processing (1)
  • src/plugin.ts

@peterhirn

Copy link
Copy Markdown
Contributor Author

Not sure if paths in traceInclude should be resolved to absolute paths before passing to traceNodeModules?

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/plugin.ts`:
- Line 127: Normalize each entry in opts.traceInclude before adding to
tracedPaths by applying the same normalization/resolve logic used for other
paths (the code around lines where tracedPaths is populated, e.g., the
normalization used on previous additions) — e.g., resolve relative entries
against rootDir (or call the same normalizePath/resolve helper used at lines
85-88) so that traceNodeModules() forwards absolute/normalized paths to
nodeFileTrace() (which is called with base: "/") and bare/relative specifiers
are correctly resolved.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f5356fc-7665-4901-afde-cfb4a4514198

📥 Commits

Reviewing files that changed from the base of the PR and between 772c81f and 6c1257c.

📒 Files selected for processing (1)
  • src/plugin.ts

Comment thread src/plugin.ts Resolved
@peterhirn

peterhirn commented Mar 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Oh cool, I just realized this now works even better than anticipated, eg.

import { defineConfig } from "vite";
import { externals } from "nf3/plugin";

export default defineConfig({
  build: {
    rollupOptions: {
      input: ["src/server.js"],
      plugins: [
        externals({
          traceInclude: [
            // traces default export from package.json, eg. node_modules/import-in-the-middle/index.js
            "import-in-the-middle",
            // prefix 'node_modules/' no longer necessary for specific files
            "import-in-the-middle/hook.mjs"
          ]
        })
      ]
    }
  }
});

@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!

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