diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-09-09 15:04:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-09 15:04:21 +0100 |
commit | 29ff0bfa9f4c369b30ec8ad1a81dc567eb9dc569 (patch) | |
tree | 03d3b125a15fcd2ec0d8d3804edf327ecdbdcd6b /cli/util/path.rs | |
parent | c521c5fe771a92a73b2e48c846e1346fb7ca2b20 (diff) |
Reland "refactor(lsp): clean up "enablePaths" handling (#20388)" (#20423)
Diffstat (limited to 'cli/util/path.rs')
-rw-r--r-- | cli/util/path.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/cli/util/path.rs b/cli/util/path.rs index b1e92015c..2c37d84ef 100644 --- a/cli/util/path.rs +++ b/cli/util/path.rs @@ -110,6 +110,28 @@ pub fn specifier_to_file_path( } } +/// Attempts to convert a file path to a specifier. By default, uses the Url +/// crate's `from_file_path()` method, but falls back to try and resolve +/// unix-style paths on Windows. +pub fn specifier_from_file_path( + path: &Path, +) -> Result<ModuleSpecifier, AnyError> { + if cfg!(windows) { + match ModuleSpecifier::from_file_path(path) { + Ok(url) => Ok(url), + Err(()) => { + let mut url = ModuleSpecifier::parse("file:///").unwrap(); + url.set_path(&path.to_string_lossy()); + Ok(url) + } + } + } else { + ModuleSpecifier::from_file_path(path).map_err(|()| { + uri_error(format!("Invalid file path.\n Path: {}", path.display())) + }) + } +} + /// `from.make_relative(to)` but with fixes. pub fn relative_specifier( from: &ModuleSpecifier, |