@@ -444,11 +444,9 @@ pub fn registry_configuration(
444
444
///
445
445
/// * `token`: The token from the command-line. If not set, uses the token
446
446
/// from the config.
447
- /// * `index`: The index URL from the command-line. This is ignored if
448
- /// `registry` is set.
447
+ /// * `index`: The index URL from the command-line.
449
448
/// * `registry`: The registry name from the command-line. If neither
450
- /// `registry`, or `index` are set, then uses `crates-io`, honoring
451
- /// `[source]` replacement if defined.
449
+ /// `registry`, or `index` are set, then uses `crates-io`.
452
450
/// * `force_update`: If `true`, forces the index to be updated.
453
451
/// * `validate_token`: If `true`, the token must be set.
454
452
fn registry (
@@ -459,24 +457,8 @@ fn registry(
459
457
force_update : bool ,
460
458
validate_token : bool ,
461
459
) -> CargoResult < ( Registry , RegistryConfig , SourceId ) > {
462
- if index. is_some ( ) && registry. is_some ( ) {
463
- // Otherwise we would silently ignore one or the other.
464
- bail ! ( "both `--index` and `--registry` should not be set at the same time" ) ;
465
- }
466
- // Parse all configuration options
460
+ let ( sid, sid_no_replacement) = get_source_id ( config, index, registry) ?;
467
461
let reg_cfg = registry_configuration ( config, registry) ?;
468
- let opt_index = registry
469
- . map ( |r| config. get_registry_index ( r) )
470
- . transpose ( ) ?
471
- . map ( |u| u. to_string ( ) ) ;
472
- let sid = get_source_id ( config, opt_index. as_deref ( ) . or ( index) , registry) ?;
473
- if !sid. is_remote_registry ( ) {
474
- bail ! (
475
- "{} does not support API commands.\n \
476
- Check for a source-replacement in .cargo/config.",
477
- sid
478
- ) ;
479
- }
480
462
let api_host = {
481
463
let _lock = config. acquire_package_cache_lock ( ) ?;
482
464
let mut src = RegistrySource :: remote ( sid, & HashSet :: new ( ) , config) ?;
@@ -503,42 +485,18 @@ fn registry(
503
485
}
504
486
token
505
487
} else {
506
- // Check `is_default_registry` so that the crates.io index can
507
- // change config.json's "api" value, and this won't affect most
508
- // people. It will affect those using source replacement, but
509
- // hopefully that's a relatively small set of users.
510
- if token. is_none ( )
511
- && reg_cfg. is_token ( )
512
- && registry. is_none ( )
513
- && !sid. is_default_registry ( )
514
- && !crates_io:: is_url_crates_io ( & api_host)
515
- {
516
- config. shell ( ) . warn (
517
- "using `registry.token` config value with source \
518
- replacement is deprecated\n \
519
- This may become a hard error in the future; \
520
- see <https://github.com/rust-lang/cargo/issues/xxx>.\n \
521
- Use the --token command-line flag to remove this warning.",
522
- ) ?;
523
- reg_cfg. as_token ( ) . map ( |t| t. to_owned ( ) )
524
- } else {
525
- let token =
526
- auth:: auth_token ( config, token. as_deref ( ) , & reg_cfg, registry, & api_host) ?;
527
- Some ( token)
528
- }
488
+ let token = auth:: auth_token ( config, token. as_deref ( ) , & reg_cfg, registry, & api_host) ?;
489
+ Some ( token)
529
490
}
530
491
} else {
531
492
None
532
493
} ;
533
494
let handle = http_handle ( config) ?;
534
- // Workaround for the sparse+https://index.crates.io replacement index. Use the non-replaced
535
- // source_id so that the original (github) url is used when publishing a crate.
536
- let sid = if sid. is_default_registry ( ) {
537
- SourceId :: crates_io ( config) ?
538
- } else {
539
- sid
540
- } ;
541
- Ok ( ( Registry :: new_handle ( api_host, token, handle) , reg_cfg, sid) )
495
+ Ok ( (
496
+ Registry :: new_handle ( api_host, token, handle) ,
497
+ reg_cfg,
498
+ sid_no_replacement,
499
+ ) )
542
500
}
543
501
544
502
/// Creates a new HTTP handle with appropriate global configuration for cargo.
@@ -943,16 +901,41 @@ pub fn yank(
943
901
/// Gets the SourceId for an index or registry setting.
944
902
///
945
903
/// The `index` and `reg` values are from the command-line or config settings.
946
- /// If both are None, returns the source for crates.io.
947
- fn get_source_id ( config : & Config , index : Option < & str > , reg : Option < & str > ) -> CargoResult < SourceId > {
948
- match ( reg, index) {
949
- ( Some ( r) , _) => SourceId :: alt_registry ( config, r) ,
950
- ( _, Some ( i) ) => SourceId :: for_registry ( & i. into_url ( ) ?) ,
951
- _ => {
952
- let map = SourceConfigMap :: new ( config) ?;
953
- let src = map. load ( SourceId :: crates_io ( config) ?, & HashSet :: new ( ) ) ?;
954
- Ok ( src. replaced_source_id ( ) )
904
+ /// If both are None, and no source-replacement is configured, returns the source for crates.io.
905
+ /// If both are None, and source replacement is configured, returns an error (except when testing Cargo).
906
+ ///
907
+ /// If `reg` is set, source replacement is not followed.
908
+ fn get_source_id (
909
+ config : & Config ,
910
+ index : Option < & str > ,
911
+ reg : Option < & str > ,
912
+ ) -> CargoResult < ( SourceId , SourceId ) > {
913
+ let map = SourceConfigMap :: new ( config) ?;
914
+ let sid = match ( reg, index) {
915
+ ( None , None ) | ( Some ( "crates-io" ) , None ) => SourceId :: crates_io ( config) ?,
916
+ ( Some ( r) , None ) => SourceId :: alt_registry ( config, r) ?,
917
+ ( None , Some ( i) ) => SourceId :: for_registry ( & i. into_url ( ) ?) ?,
918
+ ( Some ( _) , Some ( _) ) => {
919
+ bail ! ( "both `--index` and `--registry` should not be set at the same time" )
955
920
}
921
+ } ;
922
+ let src = map. load ( sid, & HashSet :: new ( ) ) ?;
923
+ if src. is_replaced ( ) && reg. is_none ( ) && index. is_none ( ) {
924
+ // Neither --registry nor --index was passed and source replacement is configured.
925
+ let replacement_sid = src. replaced_source_id ( ) ;
926
+ if replacement_sid. is_default_registry ( )
927
+ || std:: env:: var ( "__CARGO_TEST_ALLOW_CRATES_IO_REPLACEMENT_DO_NOT_USE" ) . is_ok ( )
928
+ {
929
+ // Allow replacement of crates.io for the sparse crates.io endpoint or for Cargo's
930
+ // testing framework.
931
+ Ok ( ( replacement_sid, sid) )
932
+ } else if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
933
+ bail ! ( "crates-io is replaced with remote registry {replacement_name};\n include `--registry {replacement_name}` or `--registry crates-io`" ) ;
934
+ } else {
935
+ bail ! ( "crates-io is replaced with non-remote-registry source {replacement_sid};\n include `--registry crates-io` to publish to crates.io" ) ;
936
+ }
937
+ } else {
938
+ Ok ( ( sid. clone ( ) , sid) )
956
939
}
957
940
}
958
941
0 commit comments