@@ -471,13 +471,17 @@ async fn run_check(args: &ApplyArgs, manifest_path: &Path) -> i32 {
471471 // The same PnP gate `apply` runs: a PnP tree hides every npm copy, so
472472 // without it each npm patch would read as not installed and pass calm.
473473 if matches ! (
474- detect_npm_pkg_manager( & args. common. cwd) ,
474+ detect_npm_pkg_manager( args . common . read_scope ( ) , & args. common. cwd) ,
475475 NpmPkgManager :: YarnBerryPnP
476476 ) && eco_in_local_scope ( & args. common , Ecosystem :: Npm )
477477 && manifest_targets_npm ( & tree)
478478 {
479479 return refuse_yarn_pnp ( args) ;
480480 }
481+ // The same cargo gate `apply` runs (see [`cargo_config_outside_root`]).
482+ if let Some ( why) = cargo_config_outside_root ( args, & tree) {
483+ return refuse_cargo_config_outside_root ( args, & why, & [ ] ) ;
484+ }
481485 let mut in_sync: Vec < String > = Vec :: new ( ) ;
482486 let mut not_installed: Vec < String > = Vec :: new ( ) ;
483487 if !tree. patches . is_empty ( ) {
@@ -948,6 +952,52 @@ pub(crate) fn result_to_event(result: &ApplyResult, dry_run: bool) -> PatchEvent
948952/// True when the manifest records at least one npm patch — the only kind a
949953/// PnP layout can block (a polyglot repo's pypi/gem/go patches live outside
950954/// `node_modules` and apply fine).
955+ /// The refusal code of [`cargo_config_outside_root`].
956+ const CARGO_CONFIG_OUTSIDE_ROOT : & str = "cargo_config_outside_root" ;
957+
958+ /// Why a workspace apply cannot patch this project's Cargo crates: a
959+ /// `.cargo/config` / `config.toml` sits above the repository root (stat
960+ /// only; it is never read), and cargo merges it into the build, so it may
961+ /// replace crates.io with another source while apply patches the shared
962+ /// registry cache. `None` for a global run, a run without Cargo patches
963+ /// or a project that is no Cargo project.
964+ fn cargo_config_outside_root ( args : & ApplyArgs , manifest : & PatchManifest ) -> Option < String > {
965+ let cwd = & args. common . cwd ;
966+ if !eco_in_local_scope ( & args. common , Ecosystem :: Cargo )
967+ || !manifest
968+ . patches
969+ . keys ( )
970+ . any ( |p| Ecosystem :: from_purl ( p) == Some ( Ecosystem :: Cargo ) )
971+ || !( cwd. join ( "Cargo.toml" ) . is_file ( ) || cwd. join ( "Cargo.lock" ) . is_file ( ) )
972+ {
973+ return None ;
974+ }
975+ socket_patch_core:: vendor:: cargo_config:: config_above_repo ( cwd) . map ( |why| {
976+ format ! (
977+ "{why}: it may replace crates.io with another source, so the registry copy apply \
978+ patches may not be what cargo builds; move the settings into the repository's \
979+ .cargo/config.toml (or run with --global to patch the registry cache anyway)"
980+ )
981+ } )
982+ }
983+
984+ /// Print the [`cargo_config_outside_root`] refusal and return the exit
985+ /// code.
986+ fn refuse_cargo_config_outside_root ( args : & ApplyArgs , why : & str , skipped : & [ PatchEvent ] ) -> i32 {
987+ if args. common . json {
988+ let mut env = Envelope :: new ( Command :: Apply ) ;
989+ env. dry_run = args. common . dry_run ;
990+ for skip in skipped {
991+ env. record ( skip. clone ( ) ) ;
992+ }
993+ env. mark_error ( EnvelopeError :: new ( CARGO_CONFIG_OUTSIDE_ROOT , why) ) ;
994+ println ! ( "{}" , env. to_pretty_json( ) ) ;
995+ } else {
996+ eprintln ! ( "Error: {why}" ) ;
997+ }
998+ 1
999+ }
1000+
9511001fn manifest_targets_npm ( manifest : & PatchManifest ) -> bool {
9521002 manifest
9531003 . patches
@@ -1061,7 +1111,7 @@ pub async fn run(args: ApplyArgs) -> i32 {
10611111 // filter excluding npm never touches it.
10621112 if eco_in_local_scope ( & args. common , Ecosystem :: Npm )
10631113 && matches ! (
1064- detect_npm_pkg_manager( & args. common. cwd) ,
1114+ detect_npm_pkg_manager( args . common . read_scope ( ) , & args. common. cwd) ,
10651115 NpmPkgManager :: YarnBerryPnP
10661116 )
10671117 {
@@ -1388,7 +1438,17 @@ pub(crate) async fn run_locked(
13881438 // informational note; the substantive safety is core's rename-over
13891439 // write (`utils::fs::atomic_write_bytes` never touches the store's
13901440 // shared inode).
1391- match detect_npm_pkg_manager ( & args. common . cwd ) {
1441+ // A cargo config above the repository root is never read in a
1442+ // workspace run; it may move crates.io elsewhere, so the Cargo patches
1443+ // refuse instead of patching the registry cache blind.
1444+ if let Some ( why) = cargo_config_outside_root ( & args, & manifest) {
1445+ let code = refuse_cargo_config_outside_root ( & args, & why, & user_patch_skips) ;
1446+ return ApplyRunReport {
1447+ skipped : user_patch_skips,
1448+ ..ApplyRunReport :: run_failure ( code, CARGO_CONFIG_OUTSIDE_ROOT , why)
1449+ } ;
1450+ }
1451+ match detect_npm_pkg_manager ( args. common . read_scope ( ) , & args. common . cwd ) {
13921452 NpmPkgManager :: YarnBerryPnP => {
13931453 if eco_in_local_scope ( & args. common , Ecosystem :: Npm ) && manifest_targets_npm ( & manifest) {
13941454 let code = refuse_yarn_pnp_with_skips ( & args, & user_patch_skips) ;
@@ -1512,6 +1572,7 @@ pub(crate) async fn run_locked(
15121572 } ) ;
15131573 cache_warnings. extend (
15141574 socket_patch_core:: utils:: cargo_build_cache:: invalidate_project (
1575+ args. common . read_scope ( ) ,
15151576 & args. common . cwd ,
15161577 rewritten. map ( |r| r. package_key . as_str ( ) ) ,
15171578 )
@@ -2712,6 +2773,7 @@ async fn apply_patches_inner(
27122773 // extension, or is refused with nothing written (#1996).
27132774 let result = if Ecosystem :: from_purl ( purl) == Some ( Ecosystem :: Gem ) {
27142775 let applied = gem_ext:: apply_gem_package_patch (
2776+ args. common . read_scope ( ) ,
27152777 variant_purl,
27162778 pkg_path,
27172779 & patch. files ,
@@ -2735,6 +2797,7 @@ async fn apply_patches_inner(
27352797 applied. result
27362798 } else {
27372799 apply_package_patch (
2800+ args. common . read_scope ( ) ,
27382801 variant_purl,
27392802 pkg_path,
27402803 & patch. files ,
@@ -2892,6 +2955,7 @@ async fn apply_patches_inner(
28922955 }
28932956 None => {
28942957 apply_package_patch (
2958+ args. common . read_scope ( ) ,
28952959 purl,
28962960 pkg_path,
28972961 & patch. files ,
@@ -3423,6 +3487,7 @@ async fn apply_maven_base(m: &MavenBase<'_>) -> MavenApplied {
34233487 }
34243488 attempted = true ;
34253489 let mut result = apply_package_patch (
3490+ args. common . read_scope ( ) ,
34263491 variant,
34273492 & dir,
34283493 & files,
@@ -3443,6 +3508,7 @@ async fn apply_maven_base(m: &MavenBase<'_>) -> MavenApplied {
34433508 if copy_attempted && !absent. is_empty ( ) {
34443509 attempted = true ;
34453510 let result = apply_package_patch (
3511+ args. common . read_scope ( ) ,
34463512 variant,
34473513 copy,
34483514 & absent,
@@ -3743,6 +3809,58 @@ mod tests {
37433809 AppliedVia as CoreAppliedVia , ApplyResult , VerifyResult , VerifyStatus ,
37443810 } ;
37453811
3812+ /// Review finding 3: a workspace apply of Cargo patches refuses when a
3813+ /// `.cargo/config.toml` above the repository root (never read) may
3814+ /// replace crates.io; a global run, or one without it, does not.
3815+ #[ test]
3816+ fn cargo_patches_refuse_under_a_cargo_config_above_the_repository ( ) {
3817+ #[ derive( clap:: Parser ) ]
3818+ struct Wrap {
3819+ #[ command( flatten) ]
3820+ args : ApplyArgs ,
3821+ }
3822+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
3823+ let outer = std:: fs:: canonicalize ( tmp. path ( ) ) . unwrap ( ) ;
3824+ let app = outer. join ( "repo" ) ;
3825+ std:: fs:: create_dir_all ( app. join ( ".git" ) ) . unwrap ( ) ;
3826+ std:: fs:: write ( app. join ( "Cargo.toml" ) , "[package]\n name = \" app\" \n " ) . unwrap ( ) ;
3827+ let record: socket_patch_core:: manifest:: schema:: PatchRecord =
3828+ serde_json:: from_value ( serde_json:: json!( {
3829+ "uuid" : "11111111-1111-4111-8111-111111111111" ,
3830+ "exportedAt" : "" ,
3831+ "files" : { } ,
3832+ "vulnerabilities" : { } ,
3833+ "description" : "" ,
3834+ "license" : "" ,
3835+ "tier" : "free" ,
3836+ } ) )
3837+ . unwrap ( ) ;
3838+ let mut manifest = PatchManifest :: default ( ) ;
3839+ manifest
3840+ . patches
3841+ . insert ( "pkg:cargo/serde@1.0.0" . to_string ( ) , record) ;
3842+ let parse = |extra : & [ & str ] | {
3843+ let cwd = app. display ( ) . to_string ( ) ;
3844+ let mut argv = vec ! [ "t" , "--cwd" , cwd. as_str( ) ] ;
3845+ argv. extend_from_slice ( extra) ;
3846+ <Wrap as clap:: Parser >:: try_parse_from ( argv) . unwrap ( ) . args
3847+ } ;
3848+ assert_eq ! ( cargo_config_outside_root( & parse( & [ ] ) , & manifest) , None ) ;
3849+ std:: fs:: create_dir_all ( outer. join ( ".cargo" ) ) . unwrap ( ) ;
3850+ std:: fs:: write (
3851+ outer. join ( ".cargo/config.toml" ) ,
3852+ "[source.crates-io]\n replace-with = \" v\" \n [source.v]\n directory = \" vendor\" \n " ,
3853+ )
3854+ . unwrap ( ) ;
3855+ let why = cargo_config_outside_root ( & parse ( & [ ] ) , & manifest) . unwrap ( ) ;
3856+ assert ! ( why. contains( ".cargo/config.toml" ) , "{why}" ) ;
3857+ assert ! ( why. contains( "outside the repository" ) , "{why}" ) ;
3858+ assert_eq ! (
3859+ cargo_config_outside_root( & parse( & [ "--global" ] ) , & manifest) ,
3860+ None
3861+ ) ;
3862+ }
3863+
37463864 /// A new-file collision's empty expected hash reads `(new file)` in the
37473865 /// verbose summary; a real hash is shown as is.
37483866 #[ test]
0 commit comments