@@ -209,7 +209,7 @@ fn verify_dependencies(
209
209
// This extra hostname check is mostly to assist with testing,
210
210
// but also prevents someone using `--index` to specify
211
211
// something that points to crates.io.
212
- if registry_src. is_default_registry ( ) || registry. host_is_crates_io ( ) {
212
+ if registry_src. is_crates_io ( ) || registry. host_is_crates_io ( ) {
213
213
bail ! ( "crates cannot be published to crates.io with dependencies sourced from other\n \
214
214
registries. `{}` needs to be published to crates.io before publishing this crate.\n \
215
215
(crate `{}` is pulled from {})",
@@ -391,6 +391,22 @@ pub fn registry_configuration(
391
391
} ;
392
392
// `registry.default` is handled in command-line parsing.
393
393
let ( token, process) = match registry {
394
+ Some ( "crates-io" ) | None => {
395
+ // Use crates.io default.
396
+ config. check_registry_index_not_set ( ) ?;
397
+ let token = config. get_string ( "registry.token" ) ?. map ( |p| p. val ) ;
398
+ let process = if config. cli_unstable ( ) . credential_process {
399
+ let process =
400
+ config. get :: < Option < config:: PathAndArgs > > ( "registry.credential-process" ) ?;
401
+ if token. is_some ( ) && process. is_some ( ) {
402
+ return err_both ( "registry.token" , "registry.credential-process" ) ;
403
+ }
404
+ process
405
+ } else {
406
+ None
407
+ } ;
408
+ ( token, process)
409
+ }
394
410
Some ( registry) => {
395
411
let token_key = format ! ( "registries.{registry}.token" ) ;
396
412
let token = config. get_string ( & token_key) ?. map ( |p| p. val ) ;
@@ -411,22 +427,6 @@ pub fn registry_configuration(
411
427
} ;
412
428
( token, process)
413
429
}
414
- None => {
415
- // Use crates.io default.
416
- config. check_registry_index_not_set ( ) ?;
417
- let token = config. get_string ( "registry.token" ) ?. map ( |p| p. val ) ;
418
- let process = if config. cli_unstable ( ) . credential_process {
419
- let process =
420
- config. get :: < Option < config:: PathAndArgs > > ( "registry.credential-process" ) ?;
421
- if token. is_some ( ) && process. is_some ( ) {
422
- return err_both ( "registry.token" , "registry.credential-process" ) ;
423
- }
424
- process
425
- } else {
426
- None
427
- } ;
428
- ( token, process)
429
- }
430
430
} ;
431
431
432
432
let credential_process =
@@ -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,45 @@ 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.
906
+ ///
907
+ /// The source for crates.io may be GitHub, index.crates.io, or a test-only registry depending
908
+ /// on configuration.
909
+ ///
910
+ /// If `reg` is set, source replacement is not followed.
911
+ ///
912
+ /// The return value is a pair of `SourceId`s: The first may be a built-in replacement of
913
+ /// crates.io (such as index.crates.io), while the second is always the original source.
914
+ fn get_source_id (
915
+ config : & Config ,
916
+ index : Option < & str > ,
917
+ reg : Option < & str > ,
918
+ ) -> CargoResult < ( SourceId , SourceId ) > {
919
+ let sid = match ( reg, index) {
920
+ ( None , None ) | ( Some ( "crates-io" ) , None ) => SourceId :: crates_io ( config) ?,
921
+ ( Some ( r) , None ) => SourceId :: alt_registry ( config, r) ?,
922
+ ( None , Some ( i) ) => SourceId :: for_registry ( & i. into_url ( ) ?) ?,
923
+ ( Some ( _) , Some ( _) ) => {
924
+ bail ! ( "both `--index` and `--registry` should not be set at the same time" )
925
+ }
926
+ } ;
927
+ // Load source replacements that are built-in to Cargo.
928
+ let builtin_replacement_sid = SourceConfigMap :: empty ( config) ?
929
+ . load ( sid, & HashSet :: new ( ) ) ?
930
+ . replaced_source_id ( ) ;
931
+ let replacement_sid = SourceConfigMap :: new ( config) ?
932
+ . load ( sid, & HashSet :: new ( ) ) ?
933
+ . replaced_source_id ( ) ;
934
+ if reg. is_none ( ) && index. is_none ( ) && replacement_sid != builtin_replacement_sid {
935
+ // Neither --registry nor --index was passed and the user has configured source-replacement.
936
+ if let Some ( replacement_name) = replacement_sid. alt_registry_key ( ) {
937
+ bail ! ( "crates-io is replaced with remote registry {replacement_name};\n include `--registry {replacement_name}` or `--registry crates-io`" ) ;
938
+ } else {
939
+ bail ! ( "crates-io is replaced with non-remote-registry source {replacement_sid};\n include `--registry crates-io` to publish to crates.io" ) ;
955
940
}
941
+ } else {
942
+ Ok ( ( builtin_replacement_sid, sid) )
956
943
}
957
944
}
958
945
@@ -1032,7 +1019,7 @@ pub fn search(
1032
1019
& ColorSpec :: new ( ) ,
1033
1020
) ;
1034
1021
} else if total_crates > limit && limit >= search_max_limit {
1035
- let extra = if source_id. is_default_registry ( ) {
1022
+ let extra = if source_id. is_crates_io ( ) {
1036
1023
format ! (
1037
1024
" (go to https://crates.io/search?q={} to see more)" ,
1038
1025
percent_encode( query. as_bytes( ) , NON_ALPHANUMERIC )
0 commit comments