Skip to content

Commit 3d32523

Browse files
stormslowlyCopilot
andauthored
feat: track tsconfig files as dependencies (#200)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7ceeb40 commit 3d32523

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
322322
) -> Result<CachedPath, ResolveError> {
323323
// tsconfig-paths
324324
if let Some(path) = self
325-
.load_tsconfig_paths(cached_path, specifier, &mut Ctx::default())
325+
.load_tsconfig_paths(cached_path, specifier, ctx)
326326
.await?
327327
{
328328
return Ok(path);
@@ -1447,6 +1447,9 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
14471447
&tsconfig_options.references,
14481448
)
14491449
.await?;
1450+
for dependency in &tsconfig.file_dependencies {
1451+
ctx.add_file_dependency(dependency);
1452+
}
14501453
let paths = tsconfig.resolve(cached_path.path(), specifier);
14511454
for path in paths {
14521455
let cached_path = self.cache.value(&path);
@@ -1522,7 +1525,7 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
15221525
let directory = tsconfig.directory().to_path_buf();
15231526
for reference in &mut tsconfig.references {
15241527
let reference_tsconfig_path = directory.normalize_with(&reference.path);
1525-
let tsconfig = self
1528+
let reference_tsconfig = self
15261529
.cache
15271530
.tsconfig(
15281531
/* root */ true,
@@ -1537,7 +1540,10 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
15371540
},
15381541
)
15391542
.await?;
1540-
reference.tsconfig.replace(tsconfig);
1543+
tsconfig
1544+
.file_dependencies
1545+
.extend(reference_tsconfig.file_dependencies.iter().cloned());
1546+
reference.tsconfig.replace(reference_tsconfig);
15411547
}
15421548
}
15431549

src/tests/tsconfig_project_references.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Tests for tsconfig project references
22
3-
use crate::{ResolveError, ResolveOptions, Resolver, TsconfigOptions, TsconfigReferences};
3+
use crate::{
4+
ResolveContext, ResolveError, ResolveOptions, Resolver, TsconfigOptions, TsconfigReferences,
5+
};
46

57
#[tokio::test]
68
async fn auto() {
@@ -41,6 +43,44 @@ async fn auto() {
4143
}
4244
}
4345

46+
#[tokio::test]
47+
async fn tsconfig_file_as_file_dependencies() {
48+
let f = super::fixture_root().join("tsconfig/cases/project_references");
49+
50+
let resolver = Resolver::new(ResolveOptions {
51+
tsconfig: Some(TsconfigOptions {
52+
config_file: f.join("app"),
53+
references: TsconfigReferences::Auto,
54+
}),
55+
..ResolveOptions::default()
56+
});
57+
let mut ctx = ResolveContext::default();
58+
59+
let resolved_path = resolver
60+
.resolve_with_context(&f.join("project_b/src"), "@/index.ts", &mut ctx)
61+
.await
62+
.map(|f| f.full_path());
63+
assert_eq!(resolved_path, Ok(f.join("project_b/src/aliased/index.ts")));
64+
65+
let expected_dependencies = [
66+
f.join("app/tsconfig.json"),
67+
f.join("tsconfig.base.json"),
68+
f.join("project_a/conf.json"),
69+
f.join("project_b/tsconfig.json"),
70+
f.join("project_c/tsconfig.json"),
71+
f.parent()
72+
.unwrap()
73+
.join("paths_template_variable/tsconfig2.json"),
74+
];
75+
for dependency in expected_dependencies {
76+
assert!(
77+
ctx.file_dependencies.contains(&dependency),
78+
"missing tsconfig file dependency {dependency:?}: {:?}",
79+
ctx.file_dependencies
80+
);
81+
}
82+
}
83+
4484
#[tokio::test]
4585
async fn disabled() {
4686
let f = super::fixture_root().join("tsconfig/cases/project_references");

src/tsconfig.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub struct TsConfig {
3333
#[serde(skip)]
3434
pub(crate) path: PathBuf,
3535

36+
#[serde(skip)]
37+
pub(crate) file_dependencies: Vec<PathBuf>,
38+
3639
#[serde(default)]
3740
pub extends: Option<ExtendsField>,
3841

@@ -82,11 +85,13 @@ impl TsConfig {
8285
let mut tsconfig: Self = serde_json::from_str("{}")?;
8386
tsconfig.root = root;
8487
tsconfig.path = path.to_path_buf();
88+
tsconfig.file_dependencies.push(path.to_path_buf());
8589
return Ok(tsconfig);
8690
}
8791
let mut tsconfig: Self = serde_json::from_str(json)?;
8892
tsconfig.root = root;
8993
tsconfig.path = path.to_path_buf();
94+
tsconfig.file_dependencies.push(path.to_path_buf());
9095
let directory = tsconfig.directory().to_path_buf();
9196
if let Some(base_url) = &tsconfig.compiler_options.base_url {
9297
// keep the `${configDir}` template variable in the baseUrl
@@ -159,6 +164,9 @@ impl TsConfig {
159164
.base_url
160165
.clone_from(&other_config.compiler_options.base_url);
161166
}
167+
self
168+
.file_dependencies
169+
.extend(other_config.file_dependencies.iter().cloned());
162170
}
163171

164172
pub fn resolve(&self, path: &Path, specifier: &str) -> Vec<PathBuf> {

0 commit comments

Comments
 (0)