From 27757e83e09ee9815b200a72d3a2b50454641493 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:54:26 -0800 Subject: [PATCH] fix(info): resolve bare specifier pointing to workspace member (#27020) Fixes https://github.com/denoland/deno/issues/26721 Previously, we were applying only the import map, which would result in `@scope/foo` expanding to (e.g.) `jsr:@scope/foo@1.0.0`. Since that didn't exist it would error and fail to resolve. --- cli/tools/info.rs | 62 ++++++++++++++++--- .../__test__.jsonc | 22 +++++-- .../info_bare.out | 4 ++ .../info/workspace_member/__test__.jsonc | 12 ++++ tests/specs/info/workspace_member/deno.json | 2 +- .../workspace_member/info_bare_specifier.out | 6 ++ .../info_bare_specifier_package_json.out | 6 ++ ...fo_bare_specifier_package_json_subpath.out | 6 ++ .../info/workspace_member/member/deno.json | 2 + .../specs/info/workspace_member/member/mod.ts | 3 + .../info/workspace_member/member2/deno.json | 1 + .../info/workspace_member/member3/bar.ts | 0 .../info/workspace_member/member3/mod.ts | 0 .../workspace_member/member3/package.json | 7 +++ 14 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 tests/specs/info/package_json_basic_auto_install/info_bare.out create mode 100644 tests/specs/info/workspace_member/info_bare_specifier.out create mode 100644 tests/specs/info/workspace_member/info_bare_specifier_package_json.out create mode 100644 tests/specs/info/workspace_member/info_bare_specifier_package_json_subpath.out create mode 100644 tests/specs/info/workspace_member/member/mod.ts create mode 100644 tests/specs/info/workspace_member/member2/deno.json create mode 100644 tests/specs/info/workspace_member/member3/bar.ts create mode 100644 tests/specs/info/workspace_member/member3/mod.ts create mode 100644 tests/specs/info/workspace_member/member3/package.json diff --git a/cli/tools/info.rs b/cli/tools/info.rs index fcc9fdcfa1f6dc..f0cd37772d0e29 100644 --- a/cli/tools/info.rs +++ b/cli/tools/info.rs @@ -49,19 +49,67 @@ pub async fn info( let module_graph_creator = factory.module_graph_creator().await?; let npm_resolver = factory.npm_resolver().await?; let maybe_lockfile = cli_options.maybe_lockfile(); + let resolver = factory.workspace_resolver().await?.clone(); let npmrc = cli_options.npmrc(); - let resolver = factory.workspace_resolver().await?; + let node_resolver = factory.node_resolver().await?; let cwd_url = url::Url::from_directory_path(cli_options.initial_cwd()).unwrap(); - let maybe_import_specifier = if let Some(import_map) = - resolver.maybe_import_map() + let maybe_import_specifier = if let Ok(resolved) = + resolver.resolve(&specifier, &cwd_url) { - if let Ok(imports_specifier) = import_map.resolve(&specifier, &cwd_url) { - Some(imports_specifier) - } else { - None + match resolved { + deno_config::workspace::MappedResolution::Normal { + specifier, .. + } + | deno_config::workspace::MappedResolution::ImportMap { + specifier, + .. + } + | deno_config::workspace::MappedResolution::WorkspaceJsrPackage { + specifier, + .. + } => Some(specifier), + deno_config::workspace::MappedResolution::WorkspaceNpmPackage { + target_pkg_json, + sub_path, + .. + } => Some(node_resolver.resolve_package_subpath_from_deno_module( + target_pkg_json.clone().dir_path(), + sub_path.as_deref(), + Some(&cwd_url), + node_resolver::ResolutionMode::Import, + node_resolver::NodeResolutionKind::Execution, + )?), + deno_config::workspace::MappedResolution::PackageJson { + alias, + sub_path, + dep_result, + .. + } => match dep_result.as_ref().map_err(|e| e.clone())? { + deno_package_json::PackageJsonDepValue::Workspace(version_req) => { + let pkg_folder = resolver + .resolve_workspace_pkg_json_folder_for_pkg_json_dep( + alias, + version_req, + )?; + Some(node_resolver.resolve_package_subpath_from_deno_module( + pkg_folder, + sub_path.as_deref(), + Some(&cwd_url), + node_resolver::ResolutionMode::Import, + node_resolver::NodeResolutionKind::Execution, + )?) + } + deno_package_json::PackageJsonDepValue::Req(req) => { + Some(ModuleSpecifier::parse(&format!( + "npm:{}{}", + req, + sub_path.map(|s| format!("/{}", s)).unwrap_or_default() + ))?) + } + }, } } else { None diff --git a/tests/specs/info/package_json_basic_auto_install/__test__.jsonc b/tests/specs/info/package_json_basic_auto_install/__test__.jsonc index a98f89e91318c7..4b5b9fb1a83bdc 100644 --- a/tests/specs/info/package_json_basic_auto_install/__test__.jsonc +++ b/tests/specs/info/package_json_basic_auto_install/__test__.jsonc @@ -1,9 +1,21 @@ { "tempDir": true, - "steps": [ - { - "args": "info --quiet main.ts", - "output": "info.out" + "tests": { + "resolves_npm_deps": { + "steps": [ + { + "args": "info --quiet main.ts", + "output": "info.out" + } + ] + }, + "bare_specifier": { + "steps": [ + { + "args": "info --quiet @denotest/esm-basic", + "output": "info_bare.out" + } + ] } - ] + } } diff --git a/tests/specs/info/package_json_basic_auto_install/info_bare.out b/tests/specs/info/package_json_basic_auto_install/info_bare.out new file mode 100644 index 00000000000000..4269e054136a68 --- /dev/null +++ b/tests/specs/info/package_json_basic_auto_install/info_bare.out @@ -0,0 +1,4 @@ +dependencies: 0 unique +size: 471B + +npm:/@denotest/esm-basic@1.0.0 (471B) diff --git a/tests/specs/info/workspace_member/__test__.jsonc b/tests/specs/info/workspace_member/__test__.jsonc index d13d3b03e80908..b62b3a3a596ef6 100644 --- a/tests/specs/info/workspace_member/__test__.jsonc +++ b/tests/specs/info/workspace_member/__test__.jsonc @@ -11,6 +11,18 @@ "cwd": "member/sub", "output": "info_workspace_member_sub.out", "exitCode": 0 + }, + "member_bare_specifier": { + "args": "info @denotest/workspace-member", + "output": "info_bare_specifier.out" + }, + "member_package_json_specifier": { + "args": "info @denotest/workspace-member-package-json", + "output": "info_bare_specifier_package_json.out" + }, + "member_package_json_specifier_subpath": { + "args": "info @denotest/workspace-member-package-json/foo", + "output": "info_bare_specifier_package_json_subpath.out" } } } diff --git a/tests/specs/info/workspace_member/deno.json b/tests/specs/info/workspace_member/deno.json index f88028aeab0b71..dc8a3f4ba562cc 100644 --- a/tests/specs/info/workspace_member/deno.json +++ b/tests/specs/info/workspace_member/deno.json @@ -1,3 +1,3 @@ { - "workspace": ["./member"] + "workspace": ["./member", "./member2", "./member3"] } diff --git a/tests/specs/info/workspace_member/info_bare_specifier.out b/tests/specs/info/workspace_member/info_bare_specifier.out new file mode 100644 index 00000000000000..e446407d506ffe --- /dev/null +++ b/tests/specs/info/workspace_member/info_bare_specifier.out @@ -0,0 +1,6 @@ +local: [WILDCARD]mod.ts +type: TypeScript +dependencies: 0 unique +size: [WILDCARD] + +file://[WILDCARD]member/mod.ts ([WILDCARD]) diff --git a/tests/specs/info/workspace_member/info_bare_specifier_package_json.out b/tests/specs/info/workspace_member/info_bare_specifier_package_json.out new file mode 100644 index 00000000000000..846305e4bb1c6b --- /dev/null +++ b/tests/specs/info/workspace_member/info_bare_specifier_package_json.out @@ -0,0 +1,6 @@ +local: [WILDCARD]mod.ts +type: TypeScript +dependencies: 0 unique +size: [WILDCARD] + +file://[WILDCARD]member3/mod.ts ([WILDCARD]) diff --git a/tests/specs/info/workspace_member/info_bare_specifier_package_json_subpath.out b/tests/specs/info/workspace_member/info_bare_specifier_package_json_subpath.out new file mode 100644 index 00000000000000..89c1a13b530e60 --- /dev/null +++ b/tests/specs/info/workspace_member/info_bare_specifier_package_json_subpath.out @@ -0,0 +1,6 @@ +local: [WILDCARD]bar.ts +type: TypeScript +dependencies: 0 unique +size: [WILDCARD] + +file://[WILDCARD]member3/bar.ts ([WILDCARD]) diff --git a/tests/specs/info/workspace_member/member/deno.json b/tests/specs/info/workspace_member/member/deno.json index 66aac290469038..5b8d7202a54870 100644 --- a/tests/specs/info/workspace_member/member/deno.json +++ b/tests/specs/info/workspace_member/member/deno.json @@ -1,4 +1,6 @@ { + "name": "@denotest/workspace-member", + "exports": "./mod.ts", "imports": { "foo": "./sub/file.ts" } diff --git a/tests/specs/info/workspace_member/member/mod.ts b/tests/specs/info/workspace_member/member/mod.ts new file mode 100644 index 00000000000000..e415f2ed64a54d --- /dev/null +++ b/tests/specs/info/workspace_member/member/mod.ts @@ -0,0 +1,3 @@ +export function hi() { + console.log("hi"); +} diff --git a/tests/specs/info/workspace_member/member2/deno.json b/tests/specs/info/workspace_member/member2/deno.json new file mode 100644 index 00000000000000..0967ef424bce67 --- /dev/null +++ b/tests/specs/info/workspace_member/member2/deno.json @@ -0,0 +1 @@ +{} diff --git a/tests/specs/info/workspace_member/member3/bar.ts b/tests/specs/info/workspace_member/member3/bar.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/tests/specs/info/workspace_member/member3/mod.ts b/tests/specs/info/workspace_member/member3/mod.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/tests/specs/info/workspace_member/member3/package.json b/tests/specs/info/workspace_member/member3/package.json new file mode 100644 index 00000000000000..7a4ddf9dd3e43b --- /dev/null +++ b/tests/specs/info/workspace_member/member3/package.json @@ -0,0 +1,7 @@ +{ + "name": "@denotest/workspace-member-package-json", + "exports": { + ".": "./mod.ts", + "./foo": "./bar.ts" + } +}