@@ -653,42 +653,54 @@ impl GlobalContext {
653653
654654 /// The directory to use for intermediate build artifacts.
655655 ///
656- /// Falls back to the target directory if not specified.
656+ /// Callers should prefer [`Workspace::build_dir`] instead.
657+ pub fn build_dir ( & self , workspace_manifest_path : & Path ) -> CargoResult < Option < Filesystem > > {
658+ let Some ( val) = & self . build_config ( ) ?. build_dir else {
659+ return Ok ( None ) ;
660+ } ;
661+ self . custom_build_dir ( val, workspace_manifest_path)
662+ . map ( Some )
663+ }
664+
665+ /// The directory to use for intermediate build artifacts.
657666 ///
658667 /// Callers should prefer [`Workspace::build_dir`] instead.
659- pub fn build_dir ( & self , workspace_manifest_path : & PathBuf ) -> CargoResult < Option < Filesystem > > {
660- if let Some ( val) = & self . build_config ( ) ?. build_dir {
661- let replacements = vec ! [
662- (
663- "{workspace-root}" ,
664- workspace_manifest_path
665- . parent( )
666- . unwrap( )
667- . to_str( )
668- . context( "workspace root was not valid utf-8" ) ?
669- . to_string( ) ,
670- ) ,
671- (
672- "{cargo-cache-home}" ,
673- self . home( )
674- . as_path_unlocked( )
675- . to_str( )
676- . context( "cargo home was not valid utf-8" ) ?
677- . to_string( ) ,
678- ) ,
679- ( "{workspace-path-hash}" , {
680- let real_path = std:: fs:: canonicalize( workspace_manifest_path) ?;
681- let hash = crate :: util:: hex:: short_hash( & real_path) ;
682- format!( "{}{}{}" , & hash[ 0 ..2 ] , std:: path:: MAIN_SEPARATOR , & hash[ 2 ..] )
683- } ) ,
684- ] ;
685-
686- let template_variables = replacements
687- . iter ( )
688- . map ( |( key, _) | key[ 1 ..key. len ( ) - 1 ] . to_string ( ) )
689- . collect_vec ( ) ;
668+ pub fn custom_build_dir (
669+ & self ,
670+ val : & ConfigRelativePath ,
671+ workspace_manifest_path : & Path ,
672+ ) -> CargoResult < Filesystem > {
673+ let replacements = [
674+ (
675+ "{workspace-root}" ,
676+ workspace_manifest_path
677+ . parent ( )
678+ . unwrap ( )
679+ . to_str ( )
680+ . context ( "workspace root was not valid utf-8" ) ?
681+ . to_string ( ) ,
682+ ) ,
683+ (
684+ "{cargo-cache-home}" ,
685+ self . home ( )
686+ . as_path_unlocked ( )
687+ . to_str ( )
688+ . context ( "cargo home was not valid utf-8" ) ?
689+ . to_string ( ) ,
690+ ) ,
691+ ( "{workspace-path-hash}" , {
692+ let real_path = std:: fs:: canonicalize ( workspace_manifest_path) ?;
693+ let hash = crate :: util:: hex:: short_hash ( & real_path) ;
694+ format ! ( "{}{}{}" , & hash[ 0 ..2 ] , std:: path:: MAIN_SEPARATOR , & hash[ 2 ..] )
695+ } ) ,
696+ ] ;
697+
698+ let template_variables = replacements
699+ . iter ( )
700+ . map ( |( key, _) | key[ 1 ..key. len ( ) - 1 ] . to_string ( ) )
701+ . collect_vec ( ) ;
690702
691- let path = val
703+ let path = val
692704 . resolve_templated_path ( self , replacements)
693705 . map_err ( |e| match e {
694706 path:: ResolveTemplateError :: UnexpectedVariable {
@@ -716,20 +728,15 @@ impl GlobalContext {
716728 }
717729 } ) ?;
718730
719- // Check if the target directory is set to an empty string in the config.toml file.
720- if val. raw_value ( ) . is_empty ( ) {
721- bail ! (
722- "the build directory is set to an empty string in {}" ,
723- val. value( ) . definition
724- )
725- }
726-
727- Ok ( Some ( Filesystem :: new ( path) ) )
728- } else {
729- // For now, fallback to the previous implementation.
730- // This will change in the future.
731- return self . target_dir ( ) ;
731+ // Check if the target directory is set to an empty string in the config.toml file.
732+ if val. raw_value ( ) . is_empty ( ) {
733+ bail ! (
734+ "the build directory is set to an empty string in {}" ,
735+ val. value( ) . definition
736+ )
732737 }
738+
739+ Ok ( Filesystem :: new ( path) )
733740 }
734741
735742 /// Get a configuration value by key.
@@ -1364,7 +1371,9 @@ impl GlobalContext {
13641371 let abs = |path : & str , def : & Definition | -> ( String , PathBuf , Definition ) {
13651372 let abs_path = match def {
13661373 Definition :: Path ( p) | Definition :: Cli ( Some ( p) ) => p. parent ( ) . unwrap ( ) . join ( & path) ,
1367- Definition :: Environment ( _) | Definition :: Cli ( None ) => self . cwd ( ) . join ( & path) ,
1374+ Definition :: Environment ( _) | Definition :: Cli ( None ) | Definition :: BuiltIn => {
1375+ self . cwd ( ) . join ( & path)
1376+ }
13681377 } ;
13691378 ( path. to_string ( ) , abs_path, def. clone ( ) )
13701379 } ;
0 commit comments