Skip to content

Commit c299339

Browse files
authored
fix: resolve parent base url correctly by normalizing as absolute path (#72)
1 parent da3c6dd commit c299339

File tree

8 files changed

+57
-4
lines changed

8 files changed

+57
-4
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ end_of_line=lf
88
charset=utf-8
99
trim_trailing_whitespace=true
1010
insert_final_newline=true
11+
12+
[*.rs]
13+
indent_size = 4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "index";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfigs/tsconfig.dev"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "../src"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./tsconfig.base"
3+
}

src/tests/tsconfig_paths.rs

+24
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,30 @@ fn test_paths_nested_base() {
297297
}
298298
}
299299

300+
#[test]
301+
fn test_parent_base_url() {
302+
let f = super::fixture_root().join("tsconfig");
303+
let f2 = f.join("cases").join("parent-base-url");
304+
305+
#[rustfmt::skip]
306+
let pass = [
307+
(f2.join("test"), "tsconfig.json", ".", Err(ResolveError::NotFound(".".into()))),
308+
(f2.join("test"), "tsconfig.json", "index", Ok(f2.join("src/index.ts"))),
309+
];
310+
311+
for (dir, tsconfig, request, expected) in pass {
312+
let resolver = Resolver::new(ResolveOptions {
313+
tsconfig: Some(TsconfigOptions {
314+
config_file: dir.parent().unwrap().join(tsconfig),
315+
references: TsconfigReferences::Auto,
316+
}),
317+
..ResolveOptions::default().with_extension(String::from(".ts"))
318+
});
319+
let resolved_path = resolver.resolve(&dir, request).map(|f| f.full_path());
320+
assert_eq!(resolved_path, expected, "{request} {tsconfig} {dir:?}");
321+
}
322+
}
323+
300324
#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
301325
mod windows_test {
302326
use std::{

src/tsconfig.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,29 @@ pub trait TsConfig: Sized + Debug {
8181
fn extend_tsconfig(&mut self, tsconfig: &Self) {
8282
let compiler_options = self.compiler_options_mut();
8383

84+
let tsconfig_dir = tsconfig.directory();
85+
8486
if compiler_options.base_url().is_none() {
8587
if let Some(base_url) = tsconfig.compiler_options().base_url() {
86-
compiler_options.set_base_url(base_url.to_path_buf());
88+
compiler_options.set_base_url(if base_url.starts_with(TEMPLATE_VARIABLE) {
89+
base_url.to_path_buf()
90+
} else {
91+
tsconfig_dir.join(base_url).normalize()
92+
});
8793
}
8894
}
8995

9096
if compiler_options.paths().is_none() {
91-
let paths_base = compiler_options
92-
.base_url()
93-
.map_or_else(|| tsconfig.directory().to_path_buf(), Path::to_path_buf);
97+
let paths_base = compiler_options.base_url().map_or_else(
98+
|| tsconfig_dir.to_path_buf(),
99+
|path| {
100+
if path.starts_with(TEMPLATE_VARIABLE) {
101+
path.to_path_buf()
102+
} else {
103+
tsconfig_dir.join(path).normalize()
104+
}
105+
},
106+
);
94107
compiler_options.set_paths_base(paths_base);
95108
compiler_options.set_paths(tsconfig.compiler_options().paths().cloned());
96109
}

0 commit comments

Comments
 (0)