@@ -26,7 +26,7 @@ use turborepo_repository::package_graph::PackageName;
26
26
pub use crate :: turbo_json:: { RawTurboJson , UIMode } ;
27
27
use crate :: {
28
28
cli:: { EnvMode , LogOrder } ,
29
- turbo_json:: CONFIG_FILE ,
29
+ turbo_json:: { CONFIG_FILE , CONFIG_FILE_JSONC } ,
30
30
} ;
31
31
32
32
#[ derive( Debug , Error , Diagnostic ) ]
@@ -74,10 +74,15 @@ pub enum Error {
74
74
#[ error( transparent) ]
75
75
PackageJson ( #[ from] turborepo_repository:: package_json:: Error ) ,
76
76
#[ error(
77
- "Could not find turbo.json.\n Follow directions at https://turbo.build/repo/docs to create \
77
+ "Could not find turbo.json or turbo.jsonc .\n Follow directions at https://turbo.build/repo/docs to create \
78
78
one."
79
79
) ]
80
80
NoTurboJSON ,
81
+ #[ error(
82
+ "Found both turbo.json and turbo.jsonc in the same directory: {directory}\n Remove either \
83
+ turbo.json or turbo.jsonc so there is only one."
84
+ ) ]
85
+ MultipleTurboConfigs { directory : String } ,
81
86
#[ error( transparent) ]
82
87
SerdeJson ( #[ from] serde_json:: Error ) ,
83
88
#[ error( transparent) ]
@@ -396,10 +401,29 @@ impl ConfigurationOptions {
396
401
self . run_summary . unwrap_or_default ( )
397
402
}
398
403
399
- pub fn root_turbo_json_path ( & self , repo_root : & AbsoluteSystemPath ) -> AbsoluteSystemPathBuf {
400
- self . root_turbo_json_path
401
- . clone ( )
402
- . unwrap_or_else ( || repo_root. join_component ( CONFIG_FILE ) )
404
+ pub fn root_turbo_json_path (
405
+ & self ,
406
+ repo_root : & AbsoluteSystemPath ,
407
+ ) -> Result < AbsoluteSystemPathBuf , Error > {
408
+ if let Some ( path) = & self . root_turbo_json_path {
409
+ return Ok ( path. clone ( ) ) ;
410
+ }
411
+
412
+ // Check if both files exist
413
+ let turbo_json_path = repo_root. join_component ( CONFIG_FILE ) ;
414
+ let turbo_jsonc_path = repo_root. join_component ( CONFIG_FILE_JSONC ) ;
415
+ let turbo_json_exists = turbo_json_path. try_exists ( ) ?;
416
+ let turbo_jsonc_exists = turbo_jsonc_path. try_exists ( ) ?;
417
+
418
+ match ( turbo_json_exists, turbo_jsonc_exists) {
419
+ ( true , true ) => Err ( Error :: MultipleTurboConfigs {
420
+ directory : repo_root. to_string ( ) ,
421
+ } ) ,
422
+ ( true , false ) => Ok ( turbo_json_path) ,
423
+ ( false , true ) => Ok ( turbo_jsonc_path) ,
424
+ // Default to turbo.json if neither exists
425
+ ( false , false ) => Ok ( turbo_json_path) ,
426
+ }
403
427
}
404
428
405
429
pub fn allow_no_turbo_json ( & self ) -> bool {
@@ -450,16 +474,6 @@ impl TurborepoConfigBuilder {
450
474
self
451
475
}
452
476
453
- // Getting all of the paths.
454
- #[ allow( dead_code) ]
455
- fn root_package_json_path ( & self ) -> AbsoluteSystemPathBuf {
456
- self . repo_root . join_component ( "package.json" )
457
- }
458
- #[ allow( dead_code) ]
459
- fn root_turbo_json_path ( & self ) -> AbsoluteSystemPathBuf {
460
- self . repo_root . join_component ( "turbo.json" )
461
- }
462
-
463
477
fn get_environment ( & self ) -> HashMap < OsString , OsString > {
464
478
self . environment
465
479
. clone ( )
@@ -517,9 +531,12 @@ mod test {
517
531
use tempfile:: TempDir ;
518
532
use turbopath:: { AbsoluteSystemPath , AbsoluteSystemPathBuf } ;
519
533
520
- use crate :: config:: {
521
- ConfigurationOptions , TurborepoConfigBuilder , DEFAULT_API_URL , DEFAULT_LOGIN_URL ,
522
- DEFAULT_TIMEOUT ,
534
+ use crate :: {
535
+ config:: {
536
+ ConfigurationOptions , TurborepoConfigBuilder , DEFAULT_API_URL , DEFAULT_LOGIN_URL ,
537
+ DEFAULT_TIMEOUT ,
538
+ } ,
539
+ turbo_json:: { CONFIG_FILE , CONFIG_FILE_JSONC } ,
523
540
} ;
524
541
525
542
#[ test]
@@ -542,7 +559,7 @@ mod test {
542
559
} )
543
560
. unwrap ( ) ;
544
561
assert_eq ! (
545
- defaults. root_turbo_json_path( repo_root) ,
562
+ defaults. root_turbo_json_path( repo_root) . unwrap ( ) ,
546
563
repo_root. join_component( "turbo.json" )
547
564
)
548
565
}
@@ -635,4 +652,66 @@ mod test {
635
652
assert ! ( !config. preflight( ) ) ;
636
653
assert_eq ! ( config. timeout( ) , 123 ) ;
637
654
}
655
+
656
+ #[ test]
657
+ fn test_multiple_turbo_configs ( ) {
658
+ let tmp_dir = TempDir :: new ( ) . unwrap ( ) ;
659
+ let repo_root = AbsoluteSystemPath :: from_std_path ( tmp_dir. path ( ) ) . unwrap ( ) ;
660
+
661
+ // Create both turbo.json and turbo.jsonc
662
+ let turbo_json_path = repo_root. join_component ( CONFIG_FILE ) ;
663
+ let turbo_jsonc_path = repo_root. join_component ( CONFIG_FILE_JSONC ) ;
664
+
665
+ turbo_json_path. create_with_contents ( "{}" ) . unwrap ( ) ;
666
+ turbo_jsonc_path. create_with_contents ( "{}" ) . unwrap ( ) ;
667
+
668
+ // Test ConfigurationOptions.root_turbo_json_path
669
+ let config = ConfigurationOptions :: default ( ) ;
670
+ let result = config. root_turbo_json_path ( repo_root) ;
671
+ assert ! ( result. is_err( ) ) ;
672
+ }
673
+
674
+ #[ test]
675
+ fn test_only_turbo_json ( ) {
676
+ let tmp_dir = TempDir :: new ( ) . unwrap ( ) ;
677
+ let repo_root = AbsoluteSystemPath :: from_std_path ( tmp_dir. path ( ) ) . unwrap ( ) ;
678
+
679
+ // Create only turbo.json
680
+ let turbo_json_path = repo_root. join_component ( CONFIG_FILE ) ;
681
+ turbo_json_path. create_with_contents ( "{}" ) . unwrap ( ) ;
682
+
683
+ // Test ConfigurationOptions.root_turbo_json_path
684
+ let config = ConfigurationOptions :: default ( ) ;
685
+ let result = config. root_turbo_json_path ( repo_root) ;
686
+
687
+ assert_eq ! ( result. unwrap( ) , turbo_json_path) ;
688
+ }
689
+
690
+ #[ test]
691
+ fn test_only_turbo_jsonc ( ) {
692
+ let tmp_dir = TempDir :: new ( ) . unwrap ( ) ;
693
+ let repo_root = AbsoluteSystemPath :: from_std_path ( tmp_dir. path ( ) ) . unwrap ( ) ;
694
+
695
+ // Create only turbo.jsonc
696
+ let turbo_jsonc_path = repo_root. join_component ( CONFIG_FILE_JSONC ) ;
697
+ turbo_jsonc_path. create_with_contents ( "{}" ) . unwrap ( ) ;
698
+
699
+ // Test ConfigurationOptions.root_turbo_json_path
700
+ let config = ConfigurationOptions :: default ( ) ;
701
+ let result = config. root_turbo_json_path ( repo_root) ;
702
+
703
+ assert_eq ! ( result. unwrap( ) , turbo_jsonc_path) ;
704
+ }
705
+
706
+ #[ test]
707
+ fn test_no_turbo_config ( ) {
708
+ let tmp_dir = TempDir :: new ( ) . unwrap ( ) ;
709
+ let repo_root = AbsoluteSystemPath :: from_std_path ( tmp_dir. path ( ) ) . unwrap ( ) ;
710
+
711
+ // Test ConfigurationOptions.root_turbo_json_path
712
+ let config = ConfigurationOptions :: default ( ) ;
713
+ let result = config. root_turbo_json_path ( repo_root) ;
714
+
715
+ assert_eq ! ( result. unwrap( ) , repo_root. join_component( CONFIG_FILE ) ) ;
716
+ }
638
717
}
0 commit comments