Skip to content

Commit 1aadbd7

Browse files
Fix GenType path resolution on Windows
1 parent 12749a6 commit 1aadbd7

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎compiler/gentype/module_resolver.ml‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,10 @@ let resolve_module ~(config : Config.t) ~import_extension ~output_file_relative
133133
if Sys.file_exists module_name_res_file then candidate
134134
else
135135
let rec path_to_list path =
136-
let is_root = path |> Filename.basename = path in
137-
match is_root with
138-
| true -> [path]
139-
| false ->
140-
(path |> Filename.basename) :: (path |> Filename.dirname |> path_to_list)
136+
let parent = Filename.dirname path in
137+
(* On Windows, the basename of a drive root omits the drive prefix. *)
138+
if Filename.basename path = path || parent = path then [path]
139+
else (path |> Filename.basename) :: path_to_list parent
141140
in
142141
match module_name |> apply ~resolver ~use_bs_dependencies with
143142
| None -> candidate

‎rewatch/src/build/parse.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,18 @@ fn generate_ast(
444444
// generate the dir of the ast_path (it mirrors the source file dir)
445445
let ast_parent_path = package.get_build_path().join(ast_path.parent().unwrap());
446446
helpers::create_path(&ast_parent_path);
447+
// Keep absolute source locations in the AST in the same path form as the
448+
// canonical project root passed during compilation. This matters for
449+
// Windows paths that have both short and expanded representations.
450+
let parse_working_dir = build_path_abs
451+
.canonicalize()
452+
.map(helpers::StrippedVerbatimPath::to_stripped_verbatim_path)
453+
.unwrap_or_else(|_| build_path_abs.clone());
447454

448455
/* Create .ast */
449456
let result = match Some(
450457
Command::new(&build_state.compiler_info.bsc_path)
451-
.current_dir(&build_path_abs)
458+
.current_dir(&parse_working_dir)
452459
.args(parser_args)
453460
.output()
454461
.map_err(|e| {

‎rewatch/src/config.rs‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,15 @@ impl Config {
10551055
}
10561056

10571057
pub fn get_project_root_args(&self) -> Vec<String> {
1058-
vec![
1059-
"-bs-project-root".to_string(),
1060-
self.get_package_root().to_string_lossy().to_string(),
1061-
]
1058+
// The parser records locations from a canonical working directory.
1059+
// Use the same representation here so GenType can strip this prefix,
1060+
// including when Windows supplied the project through an 8.3 path.
1061+
let root = self
1062+
.get_package_root()
1063+
.canonicalize()
1064+
.map(helpers::StrippedVerbatimPath::to_stripped_verbatim_path)
1065+
.unwrap_or_else(|_| self.get_package_root().to_path_buf());
1066+
vec!["-bs-project-root".to_string(), root.to_string_lossy().to_string()]
10621067
}
10631068

10641069
pub fn get_warning_args(&self, is_local_dep: bool, warn_error_override: Option<String>) -> Vec<String> {
@@ -1655,6 +1660,31 @@ pub mod tests {
16551660
assert_eq!(platform_args[suffix_idx + 1], "");
16561661
}
16571662

1663+
#[test]
1664+
fn test_compiler_project_root_is_canonicalized() {
1665+
let temp_dir = tempfile::tempdir().unwrap();
1666+
let nested_dir = temp_dir.path().join("nested");
1667+
fs::create_dir(&nested_dir).unwrap();
1668+
let config_path = nested_dir.join("..").join("rescript.json");
1669+
fs::write(
1670+
temp_dir.path().join("rescript.json"),
1671+
r#"{"name":"test","gentypeconfig":{}}"#,
1672+
)
1673+
.unwrap();
1674+
1675+
let config = Config::new(&config_path).unwrap();
1676+
let expected_root = temp_dir
1677+
.path()
1678+
.canonicalize()
1679+
.map(helpers::StrippedVerbatimPath::to_stripped_verbatim_path)
1680+
.unwrap()
1681+
.to_string_lossy()
1682+
.to_string();
1683+
1684+
let project_root_args = config.get_project_root_args();
1685+
assert_eq!(project_root_args, ["-bs-project-root", &expected_root]);
1686+
}
1687+
16581688
#[test]
16591689
fn test_gentype_shims_object_and_array_forms() {
16601690
let object_form = serde_json::from_str::<GenTypeShims>(r#"{"From": "To"}"#).unwrap();

0 commit comments

Comments
 (0)