1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00

fix(node): handle resolving ".//<something>" in npm packages (#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.
This commit is contained in:
David Sherret 2024-11-19 09:57:12 -05:00 committed by GitHub
parent 661aa22c03
commit 186b52731c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 30 additions and 16 deletions

View file

@ -474,21 +474,6 @@ impl<TGraphContainer: ModuleGraphContainer>
raw_specifier: &str, raw_specifier: &str,
referrer: &ModuleSpecifier, referrer: &ModuleSpecifier,
) -> Result<ModuleSpecifier, AnyError> { ) -> 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 graph = self.graph_container.graph();
let resolution = match graph.get(referrer) { let resolution = match graph.get(referrer) {
Some(Module::Js(module)) => module Some(Module::Js(module)) => module

View file

@ -202,7 +202,7 @@ impl<TEnv: NodeResolverEnv> NodeResolver<TEnv> {
mode: NodeResolutionMode, mode: NodeResolutionMode,
) -> Result<Url, NodeResolveError> { ) -> Result<Url, NodeResolveError> {
if should_be_treated_as_relative_or_absolute_path(specifier) { 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 { NodeResolveRelativeJoinError {
path: specifier.to_string(), path: specifier.to_string(),
base: referrer.clone(), base: referrer.clone(),
@ -1763,6 +1763,17 @@ fn get_module_name_from_builtin_node_module_specifier(
Some(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)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json; use serde_json::json;

View file

@ -0,0 +1,2 @@
// node.js will resolve this as ./other.js
export * from ".//other.js";

View file

@ -0,0 +1,3 @@
export function add(a, b) {
return a + b;
}

View file

@ -0,0 +1,6 @@
{
"name": "@denotest/specifier-two-slashes",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}

View file

@ -0,0 +1,4 @@
{
"args": "run --quiet main.ts",
"output": "3\n"
}

View file

@ -0,0 +1,3 @@
import { add } from "npm:@denotest/specifier-two-slashes";
console.log(add(1, 2));