Skip to content

Commit

Permalink
fix(es/loader): Fix the absolute path check when resolving modules (#…
Browse files Browse the repository at this point in the history
…10080)

fixes: #9854

`if !module_specifier.starts_with('.')` is true when `module_specifier`
is neither a relative path nor an absolute path like `classnames`, then
the file in current working directory will be found.

I gave up adding a test because it needs to change the current working
directory (by calling `std::env::set_current_dir`), which causes
failures of other tests.

Another changed line of code is to align with
https://www.typescriptlang.org/docs/handbook/modules/reference.html#baseurl,
which says:
> The baseUrl compiler option can be combined with any moduleResolution
mode and specifies a directory that bare specifiers (module specifiers
that don’t begin with **./, ../, or /**) are resolved from
  • Loading branch information
CPunisher authored Feb 23, 2025
1 parent a883cdc commit a3894ae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/silent-mirrors-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_loader: patch
---

fix(es/loader):
7 changes: 2 additions & 5 deletions crates/swc_ecma_loader/src/resolvers/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,8 @@ impl NodeModulesResolver {
module_specifier, base, self.target_env
);

if !module_specifier.starts_with('.') {
// Handle absolute path

let path = Path::new(module_specifier);

let path = Path::new(module_specifier);
if path.is_absolute() {
if let Ok(file) = self
.resolve_as_file(path)
.or_else(|_| self.resolve_as_directory(path, false))
Expand Down
3 changes: 2 additions & 1 deletion crates/swc_ecma_loader/src/resolvers/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ where
}
}

if !module_specifier.starts_with('.') {
let path = Path::new(module_specifier);
if matches!(path.components().next(), Some(Component::Normal(_))) {
let path = self.base_url.join(module_specifier);

// https://www.typescriptlang.org/docs/handbook/modules/reference.html#baseurl
Expand Down

0 comments on commit a3894ae

Please sign in to comment.