@@ -49,6 +49,8 @@ pub enum ConfigurationError {
4949 ConditionalWithoutMatch ( path:: PathBuf ) ,
5050 #[ error( "'Never' can't be used with arguments for path {0:?}" ) ]
5151 NeverWithArguments ( path:: PathBuf ) ,
52+ #[ error( "Only relative paths for 'file' and 'output' when 'directory' is relative." ) ]
53+ OnlyRelativePaths ,
5254}
5355
5456/// FilterAndFormat is a transformation that filters and formats the compiler calls.
@@ -134,6 +136,14 @@ mod formatter {
134136 type Error = ConfigurationError ;
135137
136138 fn try_from ( config : & config:: PathFormat ) -> Result < Self , Self :: Error > {
139+ use config:: PathResolver :: Relative ;
140+
141+ // When the directory is relative, the file and output must be relative too.
142+ if config. directory == Relative
143+ && ( config. file != Relative || config. output != Relative )
144+ {
145+ return Err ( ConfigurationError :: OnlyRelativePaths ) ;
146+ }
137147 Ok ( Self :: DoFormat ( config. clone ( ) , env:: current_dir ( ) ?) )
138148 }
139149 }
@@ -457,6 +467,42 @@ mod formatter {
457467 assert_eq ! ( result. unwrap( ) , expected) ;
458468 }
459469 }
470+
471+ #[ test]
472+ fn test_path_formatter_try_from ( ) {
473+ // Valid configuration: Canonical paths
474+ let config = PathFormat {
475+ directory : PathResolver :: Canonical ,
476+ file : PathResolver :: Canonical ,
477+ output : PathResolver :: Canonical ,
478+ } ;
479+ let result = PathFormatter :: try_from ( & config) ;
480+ assert ! ( result. is_ok( ) ) ;
481+ assert ! ( matches!( result. unwrap( ) , PathFormatter :: DoFormat ( ..) ) ) ;
482+
483+ // Valid configuration: Relative paths
484+ let config = PathFormat {
485+ directory : PathResolver :: Relative ,
486+ file : PathResolver :: Relative ,
487+ output : PathResolver :: Relative ,
488+ } ;
489+ let result = PathFormatter :: try_from ( & config) ;
490+ assert ! ( result. is_ok( ) ) ;
491+ assert ! ( matches!( result. unwrap( ) , PathFormatter :: DoFormat ( ..) ) ) ;
492+
493+ // Invalid configuration: Relative directory with canonical file config
494+ let config = PathFormat {
495+ directory : PathResolver :: Relative ,
496+ file : PathResolver :: Canonical ,
497+ output : PathResolver :: Relative ,
498+ } ;
499+ let result = PathFormatter :: try_from ( & config) ;
500+ assert ! ( result. is_err( ) ) ;
501+ assert ! ( matches!(
502+ result. err( ) . unwrap( ) ,
503+ ConfigurationError :: OnlyRelativePaths
504+ ) ) ;
505+ }
460506 }
461507}
462508
0 commit comments