@@ -303,13 +303,12 @@ fn merge_compile_commands(files: &[String]) -> String {
303303/// # Returns
304304///
305305/// PathBuf pointing to the workspace root directory
306- fn workspace_dir ( ) -> PathBuf {
306+ fn workspace_dir ( ) -> Option < PathBuf > {
307307 let output = Command :: new ( "cargo" )
308308 . args ( [ "locate-project" , "--workspace" , "--message-format=plain" ] )
309- . output ( )
310- . expect ( "failed to run cargo locate-project" ) ;
309+ . output ( ) . ok ( ) ?;
311310 let path = String :: from_utf8 ( output. stdout ) . expect ( "utf8" ) ;
312- PathBuf :: from ( path. trim ( ) ) . parent ( ) . unwrap ( ) . to_path_buf ( )
311+ Some ( PathBuf :: from ( path. trim ( ) ) . parent ( ) ? . to_path_buf ( ) )
313312}
314313
315314/// Main build script entry point.
@@ -388,8 +387,10 @@ fn main() {
388387 // Merge and export compile_commands.json for IDE integration
389388 let merged = merge_compile_commands ( & [ hal_cc, common_cc] ) ;
390389
391- let project_root = workspace_dir ( ) ;
392- let out_file = project_root. join ( "compile_commands.json" ) ;
393-
394- fs:: write ( out_file, merged) . expect ( "write merged compile_commands.json" ) ;
390+ if let Some ( project_root) = workspace_dir ( ) {
391+ let out_file = project_root. join ( "compile_commands.json" ) ;
392+ fs:: write ( out_file, merged) . expect ( "write merged compile_commands.json" ) ;
393+ } else {
394+ println ! ( "cargo::warning=Could not determine workspace root, skipping compile_commands.json generation." ) ;
395+ }
395396}
0 commit comments