Skip to content

Commit 5f782a9

Browse files
JounQinahabhgk
andauthored
fix: support for resolving empty tsconfig file (#602)
back port web-infra-dev/rspack-resolver#55 Co-authored-by: Gengkun <[email protected]>
1 parent 1c76a2d commit 5f782a9

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

fixtures/tsconfig/cases/empty/index.js

Whitespace-only changes.

fixtures/tsconfig/cases/empty/tsconfig.json

Whitespace-only changes.

src/tests/tsconfig_paths.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ fn broken() {
131131
assert_eq!(resolved_path, Err(error));
132132
}
133133

134+
#[test]
135+
fn empty() {
136+
let f = super::fixture_root().join("tsconfig/cases/empty");
137+
138+
let resolver = Resolver::new(ResolveOptions {
139+
tsconfig: Some(TsconfigOptions {
140+
config_file: f.join("tsconfig.json"),
141+
references: TsconfigReferences::Auto,
142+
}),
143+
..ResolveOptions::default()
144+
});
145+
146+
let resolved_path = resolver.resolve(&f, "./index").map(|f| f.full_path());
147+
assert_eq!(resolved_path, Ok(f.join("index.js")));
148+
}
149+
134150
// <https://github.com/parcel-bundler/parcel/blob/c8f5c97a01f643b4d5c333c02d019ef2618b44a5/packages/utils/node-resolver-rs/src/tsconfig.rs#L193C12-L193C12>
135151
#[test]
136152
fn test_paths() {

src/tsconfig.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,8 @@ impl TsConfig {
625625
pub fn parse(root: bool, path: &Path, json: &mut str) -> Result<Self, serde_json::Error> {
626626
let json = trim_start_matches_mut(json, '\u{feff}'); // strip bom
627627
_ = json_strip_comments::strip(json);
628-
let mut tsconfig: Self = serde_json::from_str(json)?;
628+
let mut tsconfig: Self =
629+
serde_json::from_str(if json.trim().is_empty() { "{}" } else { json })?;
629630
tsconfig.root = root;
630631
tsconfig.path = path.to_path_buf();
631632
Ok(tsconfig)

0 commit comments

Comments
 (0)