Skip to content

Commit

Permalink
fix(node): handle resolving ".//<something>" in npm packages (denolan…
Browse files Browse the repository at this point in the history
…d#26920)

The issue was this package had an import like: `".//index.js"` and we
resolved that as specified, but node normalizes it to `"./index.js"` so
we have to copy node.
  • Loading branch information
dsherret authored Nov 19, 2024
1 parent 661aa22 commit 186b527
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
15 changes: 0 additions & 15 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,21 +474,6 @@ impl<TGraphContainer: ModuleGraphContainer>
raw_specifier: &str,
referrer: &ModuleSpecifier,
) -> Result<ModuleSpecifier, AnyError> {
if self.shared.in_npm_pkg_checker.in_npm_package(referrer) {
return Ok(
self
.shared
.node_resolver
.resolve(
raw_specifier,
referrer,
self.shared.cjs_tracker.get_referrer_kind(referrer),
NodeResolutionMode::Execution,
)?
.into_url(),
);
}

let graph = self.graph_container.graph();
let resolution = match graph.get(referrer) {
Some(Module::Js(module)) => module
Expand Down
13 changes: 12 additions & 1 deletion resolvers/node/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> {
mode: NodeResolutionMode,
) -> Result<Url, NodeResolveError> {
if should_be_treated_as_relative_or_absolute_path(specifier) {
Ok(referrer.join(specifier).map_err(|err| {
Ok(node_join_url(referrer, specifier).map_err(|err| {
NodeResolveRelativeJoinError {
path: specifier.to_string(),
base: referrer.clone(),
Expand Down Expand Up @@ -1763,6 +1763,17 @@ fn get_module_name_from_builtin_node_module_specifier(
Some(specifier)
}

/// Node is more lenient joining paths than the url crate is,
/// so this function handles that.
fn node_join_url(url: &Url, path: &str) -> Result<Url, url::ParseError> {
if let Some(suffix) = path.strip_prefix(".//") {
// specifier had two leading slashes
url.join(&format!("./{}", suffix))
} else {
url.join(path)
}
}

#[cfg(test)]
mod tests {
use serde_json::json;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// node.js will resolve this as ./other.js
export * from ".//other.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add(a, b) {
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@denotest/specifier-two-slashes",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}
4 changes: 4 additions & 0 deletions tests/specs/npm/specifier_two_slashes/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"args": "run --quiet main.ts",
"output": "3\n"
}
3 changes: 3 additions & 0 deletions tests/specs/npm/specifier_two_slashes/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { add } from "npm:@denotest/specifier-two-slashes";

console.log(add(1, 2));

0 comments on commit 186b527

Please sign in to comment.