Skip to content
Draft
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
7 changes: 7 additions & 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 @@ -84,6 +84,7 @@ tracing = "0.1.41"

simd-json = { version = "0.17.0", features = ["serde_impl", "runtime-detection"], default-features = false }

camino = "1.2.1"
cfg-if = "1.0"
dunce = "1.0.5" # Normalize Windows paths to the most compatible format, avoiding UNC where possible
indexmap = { version = "2.12.0", features = ["serde"] }
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use crate::{
cache::{Cache, CachedPath},
context::ResolveContext as Ctx,
package_json::JSONMap,
path::{path_to_str, PathUtil, SLASH_START},
path::{path_to_str, path_to_utf8, PathUtil, SLASH_START},
specifier::Specifier,
tsconfig::{ExtendsField, ProjectReference, TsConfig},
};
Expand Down Expand Up @@ -691,7 +691,7 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
if ctx.fully_specified {
return Ok(None);
}
let path = path_to_str(path.path());
let path = path_to_utf8(path.path()).as_str();
// 8 is wild guess for max extension length
let mut path_with_extension_buffer = String::with_capacity(path.len() + 8);
path_with_extension_buffer.push_str(path);
Expand Down Expand Up @@ -1332,7 +1332,7 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
Cow::Borrowed(alias_value)
} else {
let normalized = alias_path.normalize_with(tail);
Cow::Owned(path_to_str(&normalized).to_string())
Cow::Owned(path_to_utf8(&normalized).as_str().to_string())
}
};

Expand Down Expand Up @@ -1397,14 +1397,14 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
// Create a meaningful error message.
let dir = path.parent().unwrap().to_path_buf();
let filename_without_extension = Path::new(filename).with_extension("");
let filename_without_extension = path_to_str(&filename_without_extension);
let filename_without_extension = path_to_utf8(&filename_without_extension).as_str();
let files = extensions
.iter()
.map(|ext| format!("{filename_without_extension}{ext}"))
.collect::<Vec<_>>()
.join(",");
Err(ResolveError::ExtensionAlias(
filename.to_str().expect("path should be UTF-8").to_string(),
path_to_utf8(Path::new(filename)).as_str().to_string(),
files,
dir,
))
Expand Down
8 changes: 7 additions & 1 deletion src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
//! * [normalize_path](https://docs.rs/normalize-path)
use std::path::{Component, Path, PathBuf};

use camino::Utf8Path;

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

pub fn path_to_utf8(path: &Path) -> &Utf8Path {
Utf8Path::from_path(path).expect("path should be UTF-8")
}

pub fn path_to_str(path: &Path) -> &str {
path.to_str().expect("path should be UTF-8")
path_to_utf8(path).as_str()
}

/// Extension trait to add path normalization to std's [`Path`].
Expand Down
18 changes: 12 additions & 6 deletions src/tsconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indexmap::IndexMap;
use rustc_hash::FxHasher;
use serde::Deserialize;

use crate::path::{path_to_str, PathUtil};
use crate::path::{path_to_utf8, PathUtil};

pub type CompilerOptionsPathsMap = IndexMap<String, Vec<String>, BuildHasherDefault<FxHasher>>;

Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct ProjectReference {
}

impl TsConfig {
#[cfg_attr(feature="enable_instrument", tracing::instrument(level=tracing::Level::DEBUG, skip_all, fields(path = path_to_str(path))))]
#[cfg_attr(feature="enable_instrument", tracing::instrument(level=tracing::Level::DEBUG, skip_all, fields(path = path_to_utf8(path).as_str())))]
pub fn parse(root: bool, path: &Path, json: &mut str) -> Result<Self, serde_json::Error> {
_ = json_strip_comments::strip(json);
if json.trim().is_empty() {
Expand Down Expand Up @@ -121,12 +121,14 @@ impl TsConfig {
}
}

let mut p = path_to_str(&self.compiler_options.paths_base).to_string();
let mut p = path_to_utf8(&self.compiler_options.paths_base)
.as_str()
.to_string();
Self::substitute_template_variable(&dir, &mut p);
self.compiler_options.paths_base = p.into();

if let Some(base_url) = self.compiler_options.base_url.as_mut() {
let mut p = path_to_str(base_url).to_string();
let mut p = path_to_utf8(base_url).as_str().to_string();
Self::substitute_template_variable(&dir, &mut p);
*base_url = p.into();
}
Expand Down Expand Up @@ -258,9 +260,13 @@ impl TsConfig {
fn substitute_template_variable(directory: &Path, path: &mut String) {
if let Some(stripped_path) = path.strip_prefix(TEMPLATE_VARIABLE) {
if let Some(unleashed_path) = stripped_path.strip_prefix("/") {
*path = path_to_str(&directory.join(unleashed_path)).to_string();
*path = path_to_utf8(&directory.join(unleashed_path))
.as_str()
.to_string();
} else {
*path = path_to_str(&directory.join(stripped_path)).to_string();
*path = path_to_utf8(&directory.join(stripped_path))
.as_str()
.to_string();
}
}
}
Expand Down
Loading