Skip to content

Commit 6a12cc1

Browse files
committed
Try and fix problem where relative ast paths could be incorrect.
1 parent a84e500 commit 6a12cc1

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

rewatch/src/build/compile.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,24 @@ pub fn compile(
159159

160160
let interface_result = match source_file.interface.to_owned() {
161161
Some(Interface { path, .. }) => {
162+
// FIX: Use absolute paths for interface AST files (same issue as implementation files)
163+
let ast_path = &helpers::get_compiler_asset(
164+
package,
165+
&packages::Namespace::NoNamespace,
166+
&path,
167+
"iast",
168+
);
169+
170+
log::debug!(
171+
"compile: interface ast_path={:?} exists={}",
172+
ast_path,
173+
ast_path.exists()
174+
);
175+
162176
let result = compile_file(
163177
package,
164178
root_package,
165-
&helpers::get_ast_path(&path),
179+
ast_path,
166180
module,
167181
true,
168182
&build_state.bsc_path,
@@ -174,10 +188,27 @@ pub fn compile(
174188
}
175189
_ => None,
176190
};
191+
// FIX: Use absolute paths for AST files to resolve cross-package dependencies correctly
192+
// Same issue as in deps.rs: relative paths fail in workspace builds where AST files
193+
// are created in lib/bs/ and copied to lib/ocaml/. Using get_compiler_asset()
194+
// ensures we get the correct absolute paths.
195+
let impl_ast_path = &helpers::get_compiler_asset(
196+
package,
197+
&packages::Namespace::NoNamespace,
198+
&source_file.implementation.path,
199+
"ast",
200+
);
201+
202+
log::debug!(
203+
"compile: implementation ast_path={:?} exists={}",
204+
impl_ast_path,
205+
impl_ast_path.exists()
206+
);
207+
177208
let result = compile_file(
178209
package,
179210
root_package,
180-
&helpers::get_ast_path(&source_file.implementation.path),
211+
impl_ast_path,
181212
module,
182213
false,
183214
&build_state.bsc_path,

rewatch/src/build/deps.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ fn get_dep_modules(
1313
build_state: &BuildState,
1414
) -> AHashSet<String> {
1515
let mut deps = AHashSet::new();
16-
let ast_file = package.get_build_path().join(ast_file);
16+
17+
// FIX: Handle both absolute and relative paths for AST files
18+
// This is needed because we now pass absolute paths from get_compiler_asset()
19+
// but the function was originally designed for relative paths
20+
let ast_file = if std::path::Path::new(ast_file).is_absolute() {
21+
std::path::PathBuf::from(ast_file)
22+
} else {
23+
package.get_build_path().join(ast_file)
24+
};
25+
1726
match helpers::read_lines(&ast_file) {
1827
Ok(lines) => {
1928
// we skip the first line with is some null characters
@@ -107,7 +116,19 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>
107116
let package = build_state
108117
.get_package(&module.package_name)
109118
.expect("Package not found");
110-
let ast_path = helpers::get_ast_path(&source_file.implementation.path);
119+
120+
// FIX: Use absolute paths for AST files to resolve cross-package dependencies correctly
121+
// The issue: get_ast_path() returns relative paths like "src/Types.ast" which work for
122+
// single-package builds but fail in workspace builds where BSC creates AST files in
123+
// lib/bs/ and they're later copied to lib/ocaml/. Using get_compiler_asset() ensures
124+
// we get absolute paths to the correct lib/ocaml/ location where AST files are stored.
125+
let ast_path = helpers::get_compiler_asset(
126+
package,
127+
&packages::Namespace::NoNamespace,
128+
&source_file.implementation.path,
129+
"ast",
130+
);
131+
111132
if module.deps_dirty || !build_state.deps_initialized {
112133
let mut deps = get_dep_modules(
113134
&ast_path.to_string_lossy(),
@@ -119,16 +140,23 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>
119140
);
120141

121142
if let Some(interface) = &source_file.interface {
122-
let iast_path = helpers::get_ast_path(&interface.path);
143+
// FIX: Use absolute paths for interface AST files (same issue as implementation files)
144+
let iast_path = helpers::get_compiler_asset(
145+
package,
146+
&packages::Namespace::NoNamespace,
147+
&interface.path,
148+
"iast",
149+
);
123150

124-
deps.extend(get_dep_modules(
151+
let interface_deps = get_dep_modules(
125152
&iast_path.to_string_lossy(),
126153
package.namespace.to_suffix(),
127154
package.modules.as_ref().unwrap(),
128155
all_mod,
129156
package,
130157
build_state,
131-
))
158+
);
159+
deps.extend(interface_deps)
132160
}
133161
match &package.namespace {
134162
packages::Namespace::NamespaceWithEntry { namespace: _, entry }

rewatch/src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ pub fn get_compiler_asset(
242242
source_file: &Path,
243243
extension: &str,
244244
) -> PathBuf {
245-
let namespace = match extension {
246-
"ast" | "iast" => &packages::Namespace::NoNamespace,
245+
let namespace = match namespace {
246+
packages::Namespace::NoNamespace => &packages::Namespace::NoNamespace,
247247
_ => namespace,
248248
};
249249
let basename = file_path_to_compiler_asset_basename(source_file, namespace);

0 commit comments

Comments
 (0)