Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime plugin onResolve behaves incorrectly for asynchronous imports #9862

Open
TomasHubelbauer opened this issue Apr 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@TomasHubelbauer
Copy link

What version of Bun is running?

1.1.0+5903a6141

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

See the reproduction repository: https://github.com/TomasHubelbauer/bun-runtime-plugin-onResolve-sync-async-import

Make a plugin that uses onResolve to map non-existent paths to existent paths:

import { plugin } from "bun";

await plugin({
  name: "test",
  async setup(build) {
    console.log('Plugin loaded');

    build.onResolve({ filter: /\.demo$/ }, ({ path }) => {
      console.log('onResolve called', path);
      switch (path) {
        case './sync.demo': {
          return { path: './sync.ts' };
        }
        case './async.demo': {
          return { path: './async.ts' };
        }
        default: {
          throw new Error(`Unknown path: ${path}`);
        }
      }
    });
  },
});

Register it in bunfig.toml:

preload = ["./plugin.ts"]

Come up with a script that has sync and async imports matching the plugin's onResolve filter:

import syncDemo from './sync.demo';

console.log('Sync demo', syncDemo);

try {
  const asyncDemo = await import('./async.demo');
  console.log('Async demo', asyncDemo);
}
catch (error) {
  console.error('Error loading async demo', error);
}

Run the script: bun index.ts:

Plugin loaded
onResolve called ./sync.demo
Sync demo Hello, sync!
onResolve called ./async.demo
Error loading async demo error: ENOENT reading "file:./async.ts"

What is the expected behavior?

The async import should resolve to ./async.ts (which it seems to be doing) and then the built-in loader for *.ts should pick it up as it does with the sync import. The output should be this:

Plugin loaded
onResolve called ./sync.demo
Sync demo Hello, sync!
Async demo Module {
  default: "Hello, async!",
}

What do you see instead?

Plugin loaded
onResolve called ./sync.demo
Sync demo Hello, sync!
onResolve called ./async.demo
Error loading async demo error: ENOENT reading "file:./async.ts"

Additional information

This seems to be specifically related to async imports.

@TomasHubelbauer TomasHubelbauer added the bug Something isn't working label Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant