File tree Expand file tree Collapse file tree 2 files changed +66
-5
lines changed Expand file tree Collapse file tree 2 files changed +66
-5
lines changed Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ impl<'cfg> SourceConfigMap<'cfg> {
90
90
id : SourceId :: crates_io ( config) ?,
91
91
replace_with : None ,
92
92
} ,
93
- ) ;
93
+ ) ? ;
94
94
Ok ( base)
95
95
}
96
96
@@ -185,9 +185,22 @@ restore the source replacement configuration to continue the build
185
185
Ok ( Box :: new ( ReplacedSource :: new ( id, new_id, new_src) ) )
186
186
}
187
187
188
- fn add ( & mut self , name : & str , cfg : SourceConfig ) {
189
- self . id2name . insert ( cfg. id , name. to_string ( ) ) ;
188
+ fn add ( & mut self , name : & str , cfg : SourceConfig ) -> CargoResult < ( ) > {
189
+ if let Some ( old_name) = self . id2name . insert ( cfg. id , name. to_string ( ) ) {
190
+ // The user is allowed to redefine the built-in crates-io
191
+ // definition from `empty()`.
192
+ if name != CRATES_IO_REGISTRY {
193
+ bail ! (
194
+ "source `{}` defines source {}, but that source is already defined by `{}`\n \
195
+ note: Sources are not allowed to be defined multiple times.",
196
+ name,
197
+ cfg. id,
198
+ old_name
199
+ ) ;
200
+ }
201
+ }
190
202
self . cfgs . insert ( name. to_string ( ) , cfg) ;
203
+ Ok ( ( ) )
191
204
}
192
205
193
206
fn add_config ( & mut self , name : String , def : SourceConfigDef ) -> CargoResult < ( ) > {
@@ -262,7 +275,7 @@ restore the source replacement configuration to continue the build
262
275
id : src,
263
276
replace_with,
264
277
} ,
265
- ) ;
278
+ ) ? ;
266
279
267
280
return Ok ( ( ) ) ;
268
281
Original file line number Diff line number Diff line change @@ -974,7 +974,6 @@ fn bad_source_config4() {
974
974
".cargo/config" ,
975
975
r#"
976
976
[source.crates-io]
977
- registry = 'https://example.com'
978
977
replace-with = 'bar'
979
978
980
979
[source.bar]
@@ -1394,3 +1393,52 @@ fn bad_target_links_overrides() {
1394
1393
. with_stderr ( "[ERROR] `warning` is not supported in build script overrides" )
1395
1394
. run ( ) ;
1396
1395
}
1396
+
1397
+ #[ cargo_test]
1398
+ fn redefined_sources ( ) {
1399
+ // Cannot define a source multiple times.
1400
+ let p = project ( )
1401
+ . file (
1402
+ ".cargo/config" ,
1403
+ r#"
1404
+ [source.foo]
1405
+ registry = "https://github.com/rust-lang/crates.io-index"
1406
+ "# ,
1407
+ )
1408
+ . file ( "src/lib.rs" , "" )
1409
+ . build ( ) ;
1410
+
1411
+ p. cargo ( "check" )
1412
+ . with_status ( 101 )
1413
+ . with_stderr (
1414
+ "\
1415
+ [ERROR] source `foo` defines source registry `https://github.com/rust-lang/crates.io-index`, \
1416
+ but that source is already defined by `crates-io`
1417
+ note: Sources are not allowed to be defined multiple times.
1418
+ " ,
1419
+ )
1420
+ . run ( ) ;
1421
+
1422
+ p. change_file (
1423
+ ".cargo/config" ,
1424
+ r#"
1425
+ [source.one]
1426
+ directory = "index"
1427
+
1428
+ [source.two]
1429
+ directory = "index"
1430
+ "# ,
1431
+ ) ;
1432
+
1433
+ // Name is `[..]` because we can't guarantee the order.
1434
+ p. cargo ( "check" )
1435
+ . with_status ( 101 )
1436
+ . with_stderr (
1437
+ "\
1438
+ [ERROR] source `[..]` defines source dir [..]/foo/index, \
1439
+ but that source is already defined by `[..]`
1440
+ note: Sources are not allowed to be defined multiple times.
1441
+ " ,
1442
+ )
1443
+ . run ( ) ;
1444
+ }
You can’t perform that action at this time.
0 commit comments