Skip to content

Self-referencing imports (own package name) produce UNRESOLVED_IMPORT warnings #96

Description

@pi0x

Describe the bug

If a bundled entry imports the package by its own name (Node's package self-referencing through exports), obuild prints UNRESOLVED_IMPORT warnings.

Here is why. obuild marks only dependencies and peerDependencies as external (src/builders/bundle.ts#L93-L101), and the package's own name isn't one of them. So Rolldown tries to resolve pkg/foo through exports, which points into dist/. obuild has just cleaned dist/, so the lookup fails and Rolldown warns before treating the import as external.

The output itself is correct, since the specifier is kept and resolves at runtime. The problem is noise: one warning block per self-import. In env-runner that's 8 blocks on every build, from src/loader.ts lazily importing env-runner/runners/<name>.

Reproduction

package.json

{
  "name": "pkg",
  "type": "module",
  "exports": { ".": "./dist/index.mjs", "./foo": "./dist/foo.mjs" },
  "devDependencies": { "obuild": "^0.4.40" }
}

build.config.mjs

import { defineBuildConfig } from "obuild/config";

export default defineBuildConfig({
  entries: [{ type: "bundle", dts: false, input: ["src/index.ts", "src/foo.ts"] }],
});

src/foo.ts

export const foo = "foo";

src/index.ts

export const load = () => import("pkg/foo").then((m) => m.foo);
$ pnpm obuild
🧻 Cleaning up ./dist
src/index.ts (1:33) [UNRESOLVED_IMPORT] Could not resolve 'pkg/foo' in src/index.ts
  ...
  ╰── Module not found, treating it as an external dependency
✅ obuild finished in 71ms

$ cat dist/index.mjs
const load = () => import("pkg/foo").then((m) => m.foo);
export { load };

$ node -e 'import("pkg").then((m) => m.load()).then(console.log)'
foo

Expected

No warning. obuild could add the package's own name to the default externals, along with dependencies and peerDependencies:

...[ctx.pkg.name, ...Object.keys(ctx.pkg.dependencies || {}), ...Object.keys(ctx.pkg.peerDependencies || {})]
  .filter(Boolean)
  .flatMap((p) => [p, new RegExp(`^${p}/`)]),

Workaround for now: rolldown: { external: [/^pkg(\/|$)/] }.

Versions

  • obuild 0.4.40 (latest)
  • rolldown 1.2.8
  • Node.js 24.20.0

🤖 Generated with AI assistant

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions