From 1ec161a0f15886f97d4fb9cbb5d115b29ed5e2a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sun, 12 Feb 2023 01:27:17 +0900 Subject: [PATCH] fix(es/module): Fix `jsc.paths` on Windows (#6930) **Related issue:** - Reverts https://github.com/swc-project/swc/pull/6716. - Closes https://github.com/swc-project/swc/issues/6782. --- crates/swc/src/config/mod.rs | 10 ++++------ .../fixture/issues-6xxx/6782/1/input/.swcrc | 20 +++++++++++++++++++ .../issues-6xxx/6782/1/input/config/index.ts | 1 + .../issues-6xxx/6782/1/input/src/index.ts | 8 ++++++++ .../issues-6xxx/6782/1/output/config/index.ts | 1 + .../issues-6xxx/6782/1/output/src/index.ts | 1 + crates/swc_ecma_transforms_module/src/path.rs | 2 +- 7 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-6xxx/6782/1/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-6xxx/6782/1/input/config/index.ts create mode 100644 crates/swc/tests/fixture/issues-6xxx/6782/1/input/src/index.ts create mode 100644 crates/swc/tests/fixture/issues-6xxx/6782/1/output/config/index.ts create mode 100644 crates/swc/tests/fixture/issues-6xxx/6782/1/output/src/index.ts diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 28610e524b26..7a6bd8b7a3e8 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -1327,12 +1327,10 @@ impl ModuleConfig { available_features: FeatureFlag, ) -> Box { let base = match base { - FileName::Real(path) if !paths.is_empty() && !path.is_absolute() => FileName::Real( - std::env::current_dir() - .map(|v| v.join(path)) - .unwrap_or_else(|_| path.to_path_buf()), - ), - _ => base.to_owned(), + FileName::Real(v) if !paths.is_empty() => { + FileName::Real(v.canonicalize().unwrap_or_else(|_| v.to_path_buf())) + } + _ => base.clone(), }; match config { diff --git a/crates/swc/tests/fixture/issues-6xxx/6782/1/input/.swcrc b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/.swcrc new file mode 100644 index 000000000000..eddac6fe7c09 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/.swcrc @@ -0,0 +1,20 @@ +{ + "minify": true, + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": true, + "dynamicImport": true + }, + "target": "es2020", + "baseUrl": "./", + "paths": { + "@/config": [ + "config" + ], + "@/config/*": [ + "config/*" + ] + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-6xxx/6782/1/input/config/index.ts b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/config/index.ts new file mode 100644 index 000000000000..6c9c49b23635 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/config/index.ts @@ -0,0 +1 @@ +export const config = () => console.log("\n\n--> all good!\n\n"); diff --git a/crates/swc/tests/fixture/issues-6xxx/6782/1/input/src/index.ts b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/src/index.ts new file mode 100644 index 000000000000..db2c426393d1 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6782/1/input/src/index.ts @@ -0,0 +1,8 @@ +import { config } from "@/config"; + +const main = () => config(); + +main(); + +// See https://github.com/swc-project/swc/issues/6782 +// See https://github.com/swc-project/swc/issues/6858 diff --git a/crates/swc/tests/fixture/issues-6xxx/6782/1/output/config/index.ts b/crates/swc/tests/fixture/issues-6xxx/6782/1/output/config/index.ts new file mode 100644 index 000000000000..2c0b15829078 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6782/1/output/config/index.ts @@ -0,0 +1 @@ +export const config=()=>console.log("\n\n--> all good!\n\n"); diff --git a/crates/swc/tests/fixture/issues-6xxx/6782/1/output/src/index.ts b/crates/swc/tests/fixture/issues-6xxx/6782/1/output/src/index.ts new file mode 100644 index 000000000000..a164355829e3 --- /dev/null +++ b/crates/swc/tests/fixture/issues-6xxx/6782/1/output/src/index.ts @@ -0,0 +1 @@ +import{config}from"../config";const main=()=>config();main(); diff --git a/crates/swc_ecma_transforms_module/src/path.rs b/crates/swc_ecma_transforms_module/src/path.rs index 67378f5fe563..3a2c2a07d16a 100644 --- a/crates/swc_ecma_transforms_module/src/path.rs +++ b/crates/swc_ecma_transforms_module/src/path.rs @@ -116,7 +116,7 @@ where let mut p = PathBuf::from(target_path); if cfg!(debug_assertions) { - trace!("to_specifier: orig_ext={:?}", orig_ext); + trace!("to_specifier({target_path}): orig_ext={:?}", orig_ext); } if let Some(orig_ext) = orig_ext {