jsr-io / jsr

The open-source package registry for modern JavaScript and TypeScript
https://jsr.io
MIT License
2.3k stars 102 forks source link

Cannot publish package using `bun:` import specifier: `invalidexternalimport` #599

Open Hexagon opened 3 months ago

Hexagon commented 3 months ago

I'm trying to publish @cross/test which does a conditional import of "bun:test" if running bun, but this error pops up - https://jsr.io/docs/troubleshooting#invalidexternalimport

The code is dynamically imported, so it would never execute in Deno

} else if (CurrentRuntime == Runtime.Bun) {
  const { wrappedTest } = await import("./shims/bun.ts");

... and the actual problem is in ./shims/bun.ts (which again is conditionally imported):

import { test } from "bun:test";
// ...

How to get around this?

bowtiedgnome commented 2 months ago

I ran into the same issue with 'bun:ffi'

It is possible to do a dynamic import (at least until the publish analyzer changes).

Its not pretty, it feels hacky, and the deno publish analyzer gives a warning, but it does work for now.

const bunFFISpecifier = "bun:ffi";
const ffi = await import(bunFFISpecifier);
console.log(ffi);

I had to include the bun ffi types from @types/bun into the module with some modifications to to get everything working as desired when developing using the deno vscode extension and typescript.

gerold-penz commented 2 months ago

I tried to use"bun:sqlite". Same problem. In my case, I want to make a package for the usage with "Bun", not for Deno.

Hexagon commented 1 month ago

@lucacasonato Is this fix live? Still getting an error when trying both deno publish and npx jsr publish. I actually seem to get an older version of the error message, missing both node: and bun::

error[invalid-external-import]: invalid import to a 'bun' specifier
 --> /home/hexagon/git/test/shims/bun.ts:8:33
  | 
8 |   const { test } = await import("bun:test");
  |                                 ^^^^^^^^^^ the specifier
  | 
  = hint: replace this import with one from jsr or npm, or vendor the dependency into your package

  info: the import was resolved to 'bun:test'
  info: this specifier is not allowed to be imported on jsr
  info: jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers
  docs: https://jsr.io/go/invalid-external-import

error: Found 1 problem

Using deno 1.45.2 (release, x86_64-unknown-linux-gnu)

Hexagon commented 1 month ago

Still no go, i think this should be reopened. Is it working for you @bowtiedgnome ?

gerold-penz commented 1 month ago

I think we just have to wait for the change to be integrated into the production version. If they do it conscientiously, then it could take a week or two.

marionauta commented 1 month ago

The command line doesn't allow me to publish a package that depends on bun:sqlite. It direct me to the docs that state:

JSR only allows external imports that are jsr:, npm:, data:, bun:, or node: specifiers.

So either the cli or the online docs should be updated.

bowtiedgnome commented 3 weeks ago

@Hexagon I still get errors the following even with npm:@types/bun and bun-types installed :

// @ts-types="bun-types"
import * as ffi from "bun:ffi";

console.log(ffi);

There isn't a file for it to cache, but it does pick up on the types.

Hexagon commented 6 days ago

Still no go

@Hexagon ➜ /workspaces/test (main) $ deno publish
Checking for slow types in the public API...
error[invalid-external-import]: invalid import to a 'bun' specifier
 --> /workspaces/test/shims/bun.ts:8:33
  | 
8 |   const { test } = await import("bun:test");
  |                                 ^^^^^^^^^^ the specifier
  | 
  = hint: replace this import with one from jsr or npm, or vendor the dependency into your package

  info: the import was resolved to 'bun:test'
  info: this specifier is not allowed to be imported on jsr
  info: jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers
  docs: https://jsr.io/go/invalid-external-import

error: Found 1 problem
@Hexagon ➜ /workspaces/test (main) $ deno --version
deno 1.46.3 (stable, release, x86_64-unknown-linux-gnu)
v8 12.9.202.5-rusty
typescript 5.5.2