Skip to content

Commit 40f0387

Browse files
committed
fix: verbatim windows paths
1 parent 45d6c51 commit 40f0387

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

rewatch/src/build/compile.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::logs;
77
use super::packages;
88
use crate::config;
99
use crate::helpers;
10+
use crate::helpers::StrippedVerbatimPath;
1011
use ahash::{AHashMap, AHashSet};
1112
use anyhow::anyhow;
1213
use console::style;
@@ -448,17 +449,12 @@ pub fn compiler_args(
448449
"{}:{}:{}",
449450
spec.module,
450451
if spec.in_source {
451-
Path::new(file_path)
452-
.parent()
453-
.unwrap()
454-
.to_str()
455-
.unwrap()
456-
.to_string()
452+
file_path.parent().unwrap().to_str().unwrap().to_string()
457453
} else {
458454
Path::new("lib")
459455
.join(Path::join(
460456
Path::new(&spec.get_out_of_source_dir()),
461-
Path::new(file_path).parent().unwrap(),
457+
file_path.parent().unwrap(),
462458
))
463459
.to_str()
464460
.unwrap()
@@ -623,7 +619,13 @@ fn compile_file(
623619
);
624620

625621
let to_mjs = Command::new(bsc_path)
626-
.current_dir(build_path_abs.canonicalize().ok().unwrap())
622+
.current_dir(
623+
build_path_abs
624+
.canonicalize()
625+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
626+
.ok()
627+
.unwrap(),
628+
)
627629
.args(to_mjs_args)
628630
.output();
629631

rewatch/src/build/namespaces.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::build::packages;
2+
use crate::helpers::StrippedVerbatimPath;
23
use ahash::AHashSet;
34
use std::fs::File;
45
use std::io::Write;
@@ -56,7 +57,13 @@ pub fn compile_mlmap(package: &packages::Package, namespace: &str, bsc_path: &Pa
5657
let args = vec!["-w", "-49", "-color", "always", "-no-alias-deps", &mlmap_name];
5758

5859
let _ = Command::new(bsc_path)
59-
.current_dir(build_path_abs.canonicalize().ok().unwrap())
60+
.current_dir(
61+
build_path_abs
62+
.canonicalize()
63+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
64+
.ok()
65+
.unwrap(),
66+
)
6067
.args(args)
6168
.output()
6269
.expect("err");

rewatch/src/build/packages.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::packages;
44
use crate::config;
55
use crate::helpers;
66
use crate::helpers::emojis::*;
7+
use crate::helpers::StrippedVerbatimPath;
78
use ahash::{AHashMap, AHashSet};
89
use anyhow::{anyhow, Result};
910
use console::style;
@@ -265,7 +266,7 @@ pub fn read_dependency(
265266
)),
266267
}?;
267268

268-
let canonical_path = match path.canonicalize() {
269+
let canonical_path = match path.canonicalize().map(StrippedVerbatimPath::to_stripped_verbatim_path) {
269270
Ok(canonical_path) => Ok(canonical_path),
270271
Err(e) => {
271272
Err(format!(
@@ -429,7 +430,10 @@ fn make_package(config: config::Config, package_path: &Path, is_pinned_dep: bool
429430
namespace: config.get_namespace(),
430431
modules: None,
431432
// we canonicalize the path name so it's always the same
432-
path: package_path.canonicalize().expect("Could not canonicalize"),
433+
path: package_path
434+
.canonicalize()
435+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
436+
.expect("Could not canonicalize"),
433437
dirs: None,
434438
is_pinned_dep,
435439
is_local_dep: !package_path.components().any(|c| c.as_os_str() == "node_modules"),

rewatch/src/helpers.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,53 @@ pub mod emojis {
3636
pub static LINE_CLEAR: &str = "\x1b[2K\r";
3737
}
3838

39+
/// This trait is used to strip the verbatim prefix from a Windows path.
40+
/// On non-Windows systems, it simply returns the original path.
41+
/// This is needed until the rescript compiler can handle such paths.
42+
pub trait StrippedVerbatimPath {
43+
fn to_stripped_verbatim_path(self) -> PathBuf;
44+
}
45+
46+
impl StrippedVerbatimPath for PathBuf {
47+
fn to_stripped_verbatim_path(self) -> PathBuf {
48+
if cfg!(not(target_os = "windows")) {
49+
return self;
50+
}
51+
52+
let mut stripped = PathBuf::new();
53+
for component in self.components() {
54+
match component {
55+
Component::Prefix(prefix_component) => {
56+
if prefix_component.kind().is_verbatim() {
57+
stripped.push(
58+
prefix_component
59+
.as_os_str()
60+
.to_string_lossy()
61+
.strip_prefix("\\\\?\\")
62+
.unwrap(),
63+
);
64+
} else {
65+
stripped.push(prefix_component.as_os_str());
66+
}
67+
}
68+
Component::RootDir => {
69+
stripped.push(Component::RootDir);
70+
}
71+
Component::CurDir => {
72+
stripped.push(Component::CurDir);
73+
}
74+
Component::ParentDir => {
75+
stripped.push(Component::ParentDir);
76+
}
77+
Component::Normal(os_str) => {
78+
stripped.push(os_str);
79+
}
80+
}
81+
}
82+
stripped
83+
}
84+
}
85+
3986
pub trait LexicalAbsolute {
4087
fn to_lexical_absolute(&self) -> std::io::Result<PathBuf>;
4188
}
@@ -152,7 +199,8 @@ pub fn get_bsc(root_path: &Path, workspace_root: &Option<PathBuf>) -> PathBuf {
152199
.join(subfolder)
153200
.join("bin")
154201
.join("bsc.exe")
155-
.canonicalize(),
202+
.canonicalize()
203+
.map(StrippedVerbatimPath::to_stripped_verbatim_path),
156204
workspace_root.as_ref().map(|workspace_root| {
157205
workspace_root
158206
.join("node_modules")
@@ -161,6 +209,7 @@ pub fn get_bsc(root_path: &Path, workspace_root: &Option<PathBuf>) -> PathBuf {
161209
.join("bin")
162210
.join("bsc.exe")
163211
.canonicalize()
212+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
164213
}),
165214
) {
166215
(Ok(path), _) => path,
@@ -212,7 +261,10 @@ pub fn get_compiler_asset(
212261
}
213262

214263
pub fn canonicalize_string_path(path: &str) -> Option<PathBuf> {
215-
return Path::new(path).canonicalize().ok();
264+
return Path::new(path)
265+
.canonicalize()
266+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
267+
.ok();
216268
}
217269

218270
pub fn get_bs_compiler_asset(

rewatch/src/watcher.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::build::clean;
44
use crate::cmd;
55
use crate::helpers;
66
use crate::helpers::emojis::*;
7+
use crate::helpers::StrippedVerbatimPath;
78
use crate::queue::FifoQueue;
89
use crate::queue::*;
910
use futures_timer::Delay;
@@ -144,7 +145,10 @@ async fn async_watch(
144145
) => {
145146
// if we are going to compile incrementally, we need to mark the exact files
146147
// dirty
147-
if let Ok(canonicalized_path_buf) = path_buf.canonicalize() {
148+
if let Ok(canonicalized_path_buf) = path_buf
149+
.canonicalize()
150+
.map(StrippedVerbatimPath::to_stripped_verbatim_path)
151+
{
148152
for module in build_state.modules.values_mut() {
149153
match module.source_type {
150154
SourceType::SourceFile(ref mut source_file) => {

0 commit comments

Comments
 (0)