Skip to content

Commit

Permalink
fix(info): resolve bare specifier pointing to workspace member (#27020)
Browse files Browse the repository at this point in the history
Fixes #26721

Previously, we were applying only the import map, which would result in
`@scope/foo` expanding to (e.g.) `jsr:@scope/[email protected]`. Since that
didn't exist it would error and fail to resolve.
  • Loading branch information
nathanwhit authored and bartlomieju committed Nov 28, 2024
1 parent 6a5dadf commit 27757e8
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 13 deletions.
62 changes: 55 additions & 7 deletions cli/tools/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions tests/specs/info/package_json_basic_auto_install/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies: 0 unique
size: 471B

npm:/@denotest/[email protected] (471B)
12 changes: 12 additions & 0 deletions tests/specs/info/workspace_member/__test__.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
2 changes: 1 addition & 1 deletion tests/specs/info/workspace_member/deno.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"workspace": ["./member"]
"workspace": ["./member", "./member2", "./member3"]
}
6 changes: 6 additions & 0 deletions tests/specs/info/workspace_member/info_bare_specifier.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local: [WILDCARD]mod.ts
type: TypeScript
dependencies: 0 unique
size: [WILDCARD]

file://[WILDCARD]member/mod.ts ([WILDCARD])
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local: [WILDCARD]mod.ts
type: TypeScript
dependencies: 0 unique
size: [WILDCARD]

file://[WILDCARD]member3/mod.ts ([WILDCARD])
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local: [WILDCARD]bar.ts
type: TypeScript
dependencies: 0 unique
size: [WILDCARD]

file://[WILDCARD]member3/bar.ts ([WILDCARD])
2 changes: 2 additions & 0 deletions tests/specs/info/workspace_member/member/deno.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "@denotest/workspace-member",
"exports": "./mod.ts",
"imports": {
"foo": "./sub/file.ts"
}
Expand Down
3 changes: 3 additions & 0 deletions tests/specs/info/workspace_member/member/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function hi() {
console.log("hi");
}
1 change: 1 addition & 0 deletions tests/specs/info/workspace_member/member2/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/specs/info/workspace_member/member3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@denotest/workspace-member-package-json",
"exports": {
".": "./mod.ts",
"./foo": "./bar.ts"
}
}

0 comments on commit 27757e8

Please sign in to comment.