Skip to content

Commit

Permalink
fix: support bun specifiers in JSR publish (#24588)
Browse files Browse the repository at this point in the history
Fixes #26989

---------

Co-authored-by: Nathan Whitaker <nathan@deno.com>
  • Loading branch information
2 people authored and bartlomieju committed Nov 28, 2024
1 parent 52bcd35 commit 4a0bbd0
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 18 deletions.
23 changes: 22 additions & 1 deletion cli/graph_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::args::CliLockfile;
use crate::args::CliOptions;
use crate::args::DENO_DISABLE_PEDANTIC_NODE_WARNINGS;
use crate::cache;
use crate::cache::FetchCacher;
use crate::cache::GlobalHttpCache;
use crate::cache::ModuleInfoCache;
use crate::cache::ParsedSourceCache;
Expand Down Expand Up @@ -254,6 +255,23 @@ impl ModuleGraphCreator {
package_configs: &[JsrPackageConfig],
build_fast_check_graph: bool,
) -> Result<ModuleGraph, AnyError> {
struct PublishLoader(FetchCacher);
impl Loader for PublishLoader {
fn load(
&self,
specifier: &deno_ast::ModuleSpecifier,
options: deno_graph::source::LoadOptions,
) -> deno_graph::source::LoadFuture {
if specifier.scheme() == "bun" {
return Box::pin(std::future::ready(Ok(Some(
deno_graph::source::LoadResponse::External {
specifier: specifier.clone(),
},
))));
}
self.0.load(specifier, options)
}
}
fn graph_has_external_remote(graph: &ModuleGraph) -> bool {
// Earlier on, we marked external non-JSR modules as external.
// If the graph contains any of those, it would cause type checking
Expand All @@ -271,12 +289,15 @@ impl ModuleGraphCreator {
for package_config in package_configs {
roots.extend(package_config.config_file.resolve_export_value_urls()?);
}

let loader = self.module_graph_builder.create_graph_loader();
let mut publish_loader = PublishLoader(loader);
let mut graph = self
.create_graph_with_options(CreateGraphOptions {
is_dynamic: false,
graph_kind: deno_graph::GraphKind::All,
roots,
loader: None,
loader: Some(&mut publish_loader),
})
.await?;
self.graph_valid(&graph)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/registry/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl Diagnostic for PublishDiagnostic {
InvalidExternalImport { imported, .. } => Cow::Owned(vec![
Cow::Owned(format!("the import was resolved to '{}'", imported)),
Cow::Borrowed("this specifier is not allowed to be imported on jsr"),
Cow::Borrowed("jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers"),
Cow::Borrowed("jsr only supports importing `jsr:`, `npm:`, `data:`, `bun:`, and `node:` specifiers"),
]),
UnsupportedJsxTsx { .. } => Cow::Owned(vec![
Cow::Borrowed("follow https://github.com/jsr-io/jsr/issues/24 for updates"),
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/registry/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl GraphDiagnosticsCollector {
resolution: &ResolutionResolved| {
if visited.insert(resolution.specifier.clone()) {
match resolution.specifier.scheme() {
"file" | "data" | "node" => {}
"file" | "data" | "node" | "bun" => {}
"jsr" => {
skip_specifiers.insert(resolution.specifier.clone());

Expand Down
26 changes: 15 additions & 11 deletions cli/tsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,17 +656,21 @@ fn op_load_inner(
}
Module::Npm(_) | Module::Node(_) => None,
Module::External(module) => {
// means it's Deno code importing an npm module
let specifier = node::resolve_specifier_into_node_modules(
&module.specifier,
&deno_fs::RealFs,
);
Some(Cow::Owned(load_from_node_modules(
&specifier,
state.maybe_npm.as_ref(),
&mut media_type,
&mut is_cjs,
)?))
if module.specifier.scheme() != "file" {
None
} else {
// means it's Deno code importing an npm module
let specifier = node::resolve_specifier_into_node_modules(
&module.specifier,
&deno_fs::RealFs,
);
Some(Cow::Owned(load_from_node_modules(
&specifier,
state.maybe_npm.as_ref(),
&mut media_type,
&mut is_cjs,
)?))
}
}
}
} else if let Some(npm) = state
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/publish/bun_specifier/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "publish --token 'sadfasdf'",
"output": "bun_specifier.out"
}
6 changes: 6 additions & 0 deletions tests/specs/publish/bun_specifier/bun_specifier.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Check file:///[WILDCARD]/mod.ts
Checking for slow types in the public API...
Check file:///[WILDCARD]/mod.ts
Publishing @foo/bar@1.0.0 ...
Successfully published @foo/bar@1.0.0
Visit http://127.0.0.1:4250/@foo/bar@1.0.0 for details
8 changes: 8 additions & 0 deletions tests/specs/publish/bun_specifier/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@foo/bar",
"version": "1.0.0",
"exports": {
".": "./mod.ts"
},
"license": "MIT"
}
1 change: 1 addition & 0 deletions tests/specs/publish/bun_specifier/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "bun:sqlite";
4 changes: 2 additions & 2 deletions tests/specs/publish/invalid_import/invalid_import.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error[invalid-external-import]: invalid import to a non-JSR 'http' specifier

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

error[invalid-external-import]: invalid import to a non-JSR 'http' specifier
Expand All @@ -25,7 +25,7 @@ error[invalid-external-import]: invalid import to a non-JSR 'http' specifier

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

error: Found 2 problems
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[invalid-external-import]: invalid import to a non-JSR 'http' specifier

info: the import was resolved to 'http://esm.sh/react-dom@18.2.0/server'
info: this specifier is not allowed to be imported on jsr
info: jsr only supports importing `jsr:`, `npm:`, and `data:` specifiers
info: jsr only supports importing `jsr:`, `npm:`, `data:`, `bun:`, and `node:` specifiers
docs: https://jsr.io/go/invalid-external-import

error: Found 1 problem
2 changes: 1 addition & 1 deletion tests/specs/publish/prefer_fast_check_graph/main.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ error[invalid-external-import]: invalid import to a non-JSR 'https' specifier

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

error: Found 1 problem

0 comments on commit 4a0bbd0

Please sign in to comment.