Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ cfg-if = "1"
compact_str = "0.9"
fast-glob = "1"
indexmap = { version = "2", features = ["serde"] }
memchr = "2"
json-strip-comments = "3.1"
nodejs-built-in-modules = "1.0.0"
once_cell = "1" # Use `std::sync::OnceLock::get_or_try_init` when it is stable.
Expand Down
1 change: 1 addition & 0 deletions fixtures/tsconfig/cases/query-params/src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "foo";
2 changes: 2 additions & 0 deletions fixtures/tsconfig/cases/query-params/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import foo from "@alias/foo.js";
export default foo;
9 changes: 9 additions & 0 deletions fixtures/tsconfig/cases/query-params/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"paths": {
"@alias/*": ["./src/*"]
}
},
"include": ["src/**/*"]
}
6 changes: 6 additions & 0 deletions fixtures/tsconfig/cases/query-params/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"include": [],
"references": [
{ "path": "./tsconfig.app.json" }
]
}
38 changes: 37 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
//! Code adapted from the following libraries
//! * [path-absolutize](https://docs.rs/path-absolutize)
//! * [normalize_path](https://docs.rs/normalize-path)
use std::path::{Component, Path, PathBuf};
use std::{
ffi::OsStr,
path::{Component, Path, PathBuf},
};

pub const SLASH_START: &[char; 2] = &['/', '\\'];

/// Strip query parameters (`?...`) and hash fragments (`#...`) from a file path.
pub fn strip_query_and_fragment(path: &Path) -> &Path {
let bytes = path.as_os_str().as_encoded_bytes();
let Some(end) = memchr::memchr2(b'?', b'#', bytes) else {
return path;
};
// SAFETY: Splitting at ASCII `?` or `#` preserves valid OsStr encoding.
Path::new(unsafe { OsStr::from_encoded_bytes_unchecked(&bytes[..end]) })
}

/// Extension trait to add path normalization to std's [`Path`].
pub trait PathUtil {
/// Normalize this path without performing I/O.
Expand Down Expand Up @@ -157,3 +170,26 @@ fn normalize_relative() {
assert_eq!(Path::new("foo../../..").normalize_relative(), Path::new(".."));
assert_eq!(Path::new("jest-runner-../../").normalize_relative(), Path::new(""));
}

#[test]
fn test_strip_query_and_fragment() {
assert_eq!(strip_query_and_fragment(Path::new("/src/foo.ts")), Path::new("/src/foo.ts"));
assert_eq!(
strip_query_and_fragment(Path::new("/src/foo.ts?custom=foo")),
Path::new("/src/foo.ts")
);
assert_eq!(
strip_query_and_fragment(Path::new("/src/foo.ts#fragment")),
Path::new("/src/foo.ts")
);
assert_eq!(
strip_query_and_fragment(Path::new("/src/foo.ts?key=val#frag")),
Path::new("/src/foo.ts")
);
assert_eq!(
strip_query_and_fragment(Path::new("/src/foo.ts#frag?key=val")),
Path::new("/src/foo.ts")
);
assert_eq!(strip_query_and_fragment(Path::new("")), Path::new(""));
assert_eq!(strip_query_and_fragment(Path::new("?query")), Path::new(""));
}
43 changes: 43 additions & 0 deletions src/tests/tsconfig_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ fn tsconfig_discovery_virtual_file_importer() {
assert_eq!(resolved_path, Err(ResolveError::NotFound("random-import".into())));
}

/// When the importer path has query parameters (e.g. `file.tsx?custom=foo`),
/// auto-discovery should strip them before walking parent directories
/// and discover the correct tsconfig.json.
///
/// Uses a fixture with project references where the root tsconfig has `include: []`
/// and a referenced tsconfig.app.json has `include: ["src/**/*"]` with path aliases.
/// Without stripping query params, `resolve_tsconfig_solution` fails to match the
/// file against the reference's include pattern, returning the wrong tsconfig.
#[test]
fn tsconfig_discovery_query_params() {
let f = super::fixture_root().join("tsconfig/cases/query-params");
let expected_tsconfig = f.join("tsconfig.app.json");

let resolver = Resolver::new(ResolveOptions {
tsconfig: Some(TsconfigDiscovery::Auto),
..ResolveOptions::default()
});

let clean_path = f.join("src/index.ts");

// Baseline — clean path discovers tsconfig.app.json (via project references)
let tsconfig = resolver.find_tsconfig(&clean_path).unwrap().unwrap();
assert_eq!(tsconfig.path, expected_tsconfig, "baseline: should select referenced tsconfig");

// With query parameter — should discover the same referenced tsconfig
let path_with_query = format!("{}?custom=foo", clean_path.display());
let tsconfig = resolver.find_tsconfig(&path_with_query).unwrap().unwrap();
assert_eq!(tsconfig.path, expected_tsconfig, "query param: should select referenced tsconfig");

// With fragment — should discover the same referenced tsconfig
let path_with_fragment = format!("{}#fragment", clean_path.display());
let tsconfig = resolver.find_tsconfig(&path_with_fragment).unwrap().unwrap();
assert_eq!(tsconfig.path, expected_tsconfig, "fragment: should select referenced tsconfig");

// With both query and fragment — should discover the same referenced tsconfig
let path_with_both = format!("{}?custom=foo#fragment", clean_path.display());
let tsconfig = resolver.find_tsconfig(&path_with_both).unwrap().unwrap();
assert_eq!(
tsconfig.path, expected_tsconfig,
"query+fragment: should select referenced tsconfig"
);
}

/// When a tsconfig.json exists but is not readable (e.g. permission denied),
/// auto-discovery should skip it and return `Ok(None)` instead of erroring.
#[test]
Expand Down
3 changes: 2 additions & 1 deletion src/tsconfig_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
&self,
path: P,
) -> Result<Option<Arc<TsConfig>>, ResolveError> {
let path = path.as_ref();
// Vite plugins may append query params to real file paths (e.g. `file.tsx?custom=foo`), which are not valid filesystem path components.
let path = crate::path::strip_query_and_fragment(path.as_ref());
let cached_path = self.cache.value(path);
self.find_tsconfig_tracing(&cached_path)
}
Expand Down
Loading