Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/build/plugins/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export function externals(opts: ExternalsOptions): Plugin {
await traceNodeModules([...tracedPaths], {
...traceOpts,
fullTraceInclude: resolved?.fullTraceInclude,
traceInclude: resolved?.traceInclude,
conditions: opts.conditions,
rootDir: opts.rootDir,
writePackageJson: true, // deno compat
Expand Down Expand Up @@ -227,11 +228,18 @@ export function resolveTraceDeps(
const fullTraceInclude = [...new Set([...builtinFullTrace, ...userFullTrace])].filter(
(d) => !negated.has(d)
);
// Named (non-RegExp) deps to force-trace by name. nft cannot statically detect
// packages that are loaded dynamically (e.g. native bindings), so they are
// resolved and traced explicitly by `traceNodeModules`. This also makes them
// work under pnpm, where a nested dependency is not hoisted and only resolves
// from the dependent package's real `.pnpm` location.
const traceInclude = resolved.filter((d): d is string => typeof d === "string");
return {
includePattern: tracePattern
? new RegExp(`(?:^|[/\\\\]node_modules[/\\\\])(?:${tracePattern})(?:[/\\\\]|$)`)
: undefined,
fullTraceInclude: fullTraceInclude.length > 0 ? fullTraceInclude : undefined,
traceInclude: traceInclude.length > 0 ? traceInclude : undefined,
};
}

Expand Down
20 changes: 20 additions & 0 deletions test/unit/trace-deps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ describe("resolveTraceDeps", () => {
expect(result.fullTraceInclude).toContain("prisma");
});

it("returns named deps as traceInclude (builtins + user, RegExp excluded)", () => {
const result = resolveTraceDeps(["my-pkg", /my-.*-pkg/], defaults);
expect(result.traceInclude).toContain("sharp");
expect(result.traceInclude).toContain("canvas");
expect(result.traceInclude).toContain("my-pkg");
// RegExp entries cannot be resolved by name and must be excluded
expect(result.traceInclude!.every((d) => typeof d === "string")).toBe(true);
});
Comment on lines +43 to +50

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the exact traceInclude membership.

This test doesn't really prove the RegExp selector is excluded. An implementation that accidentally stringifies the regex would still pass typeof d === "string". Please assert the final members/count instead.

Suggested test tightening
   it("returns named deps as traceInclude (builtins + user, RegExp excluded)", () => {
     const result = resolveTraceDeps(["my-pkg", /my-.*-pkg/], defaults);
-    expect(result.traceInclude).toContain("sharp");
-    expect(result.traceInclude).toContain("canvas");
-    expect(result.traceInclude).toContain("my-pkg");
-    // RegExp entries cannot be resolved by name and must be excluded
-    expect(result.traceInclude!.every((d) => typeof d === "string")).toBe(true);
+    expect(result.traceInclude).toEqual(
+      expect.arrayContaining(["sharp", "canvas", "my-pkg"])
+    );
+    expect(result.traceInclude).toHaveLength(3);
   });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("returns named deps as traceInclude (builtins + user, RegExp excluded)", () => {
const result = resolveTraceDeps(["my-pkg", /my-.*-pkg/], defaults);
expect(result.traceInclude).toContain("sharp");
expect(result.traceInclude).toContain("canvas");
expect(result.traceInclude).toContain("my-pkg");
// RegExp entries cannot be resolved by name and must be excluded
expect(result.traceInclude!.every((d) => typeof d === "string")).toBe(true);
});
it("returns named deps as traceInclude (builtins + user, RegExp excluded)", () => {
const result = resolveTraceDeps(["my-pkg", /my-.*-pkg/], defaults);
expect(result.traceInclude).toEqual(
expect.arrayContaining(["sharp", "canvas", "my-pkg"])
);
expect(result.traceInclude).toHaveLength(3);
});
🤖 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/trace-deps.test.ts` around lines 43 - 50, The trace deps test is
too loose because it only checks that traceInclude entries are strings, which
would still pass if the RegExp selector were incorrectly stringified. Tighten
the assertion in trace-deps.test.ts around resolveTraceDeps to verify the exact
traceInclude membership and count for the ["my-pkg", /my-.*-pkg/] case, using
the existing defaults fixture and the resolveTraceDeps result to ensure only the
expected builtins plus "my-pkg" are present.


it("excludes negated packages from traceInclude", () => {
const result = resolveTraceDeps(["!sharp"], defaults);
expect(result.traceInclude).not.toContain("sharp");
expect(result.traceInclude).toContain("canvas");
});

it("returns undefined traceInclude when all deps are negated", () => {
const result = resolveTraceDeps(["!sharp", "!canvas"], defaults);
expect(result.traceInclude).toBeUndefined();
});

it("throws on bare ! selector", () => {
expect(() => resolveTraceDeps(["!"], defaults)).toThrow('Invalid traceDeps selector: "!"');
});
Expand Down
Loading