1- use std:: collections:: { BTreeSet , HashMap , HashSet } ;
1+ use std:: collections:: { BTreeSet , HashMap } ;
22use std:: fs:: { self , File } ;
33use std:: io:: prelude:: * ;
44use std:: io:: SeekFrom ;
@@ -16,7 +16,7 @@ use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1616use crate :: ops:: lockfile:: LOCKFILE_NAME ;
1717use crate :: ops:: registry:: { infer_registry, RegistryOrIndex } ;
1818use crate :: sources:: registry:: index:: { IndexPackage , RegistryDependency } ;
19- use crate :: sources:: { PathSource , SourceConfigMap , CRATES_IO_REGISTRY } ;
19+ use crate :: sources:: { PathSource , CRATES_IO_REGISTRY } ;
2020use crate :: util:: cache_lock:: CacheLockMode ;
2121use crate :: util:: context:: JobsConfig ;
2222use crate :: util:: errors:: CargoResult ;
@@ -202,19 +202,28 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
202202 // below, and will be validated during the verification step.
203203 }
204204
205+ let deps = local_deps ( pkgs. iter ( ) . map ( |( p, f) | ( ( * p) . clone ( ) , f. clone ( ) ) ) ) ;
205206 let just_pkgs: Vec < _ > = pkgs. iter ( ) . map ( |p| p. 0 ) . collect ( ) ;
206- let publish_reg = get_registry ( ws. gctx ( ) , & just_pkgs, opts. reg_or_index . clone ( ) ) ?;
207- debug ! ( "packaging for registry {publish_reg}" ) ;
207+
208+ // The publish registry doesn't matter unless there are local dependencies,
209+ // so only try to get one if we need it. If they explicitly passed a
210+ // registry on the CLI, we check it no matter what.
211+ let sid = if deps. has_no_dependencies ( ) && opts. reg_or_index . is_none ( ) {
212+ None
213+ } else {
214+ let sid = get_registry ( ws. gctx ( ) , & just_pkgs, opts. reg_or_index . clone ( ) ) ?;
215+ debug ! ( "packaging for registry {}" , sid) ;
216+ Some ( sid)
217+ } ;
208218
209219 let mut local_reg = if ws. gctx ( ) . cli_unstable ( ) . package_workspace {
210220 let reg_dir = ws. target_dir ( ) . join ( "package" ) . join ( "tmp-registry" ) ;
211- Some ( TmpRegistry :: new ( ws. gctx ( ) , reg_dir, publish_reg) ?)
221+ sid. map ( |sid| TmpRegistry :: new ( ws. gctx ( ) , reg_dir, sid) )
222+ . transpose ( ) ?
212223 } else {
213224 None
214225 } ;
215226
216- let deps = local_deps ( pkgs. iter ( ) . map ( |( p, f) | ( ( * p) . clone ( ) , f. clone ( ) ) ) ) ;
217-
218227 // Packages need to be created in dependency order, because dependencies must
219228 // be added to our local overlay before we can create lockfiles that depend on them.
220229 let sorted_pkgs = deps. sort ( ) ;
@@ -258,52 +267,35 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
258267/// packages that we're packaging: if we're packaging foo-bin and foo-lib, and foo-bin
259268/// depends on foo-lib, then the foo-lib entry in foo-bin's lockfile will depend on the
260269/// registry that we're building packages for.
261- pub ( crate ) fn get_registry (
270+ fn get_registry (
262271 gctx : & GlobalContext ,
263272 pkgs : & [ & Package ] ,
264273 reg_or_index : Option < RegistryOrIndex > ,
265274) -> CargoResult < SourceId > {
266- let reg_or_index = match reg_or_index {
275+ let reg_or_index = match reg_or_index. clone ( ) {
267276 Some ( r) => Some ( r) ,
268277 None => infer_registry ( pkgs) ?,
269278 } ;
270279
280+ // Validate the registry against the packages' allow-lists.
271281 let reg = reg_or_index
272282 . clone ( )
273283 . unwrap_or_else ( || RegistryOrIndex :: Registry ( CRATES_IO_REGISTRY . to_owned ( ) ) ) ;
274-
275- // Validate the registry against the packages' allow-lists. For backwards compatibility, we
276- // skip this if only a single package is being published (because in that case the registry
277- // doesn't affect the packaging step).
278- if pkgs. len ( ) > 1 {
279- if let RegistryOrIndex :: Registry ( reg_name) = & reg {
280- for pkg in pkgs {
281- if let Some ( allowed) = pkg. publish ( ) . as_ref ( ) {
282- if !allowed. iter ( ) . any ( |a| a == reg_name) {
283- bail ! (
284+ if let RegistryOrIndex :: Registry ( reg_name) = reg {
285+ for pkg in pkgs {
286+ if let Some ( allowed) = pkg. publish ( ) . as_ref ( ) {
287+ if !allowed. iter ( ) . any ( |a| a == & reg_name) {
288+ bail ! (
284289 "`{}` cannot be packaged.\n \
285290 The registry `{}` is not listed in the `package.publish` value in Cargo.toml.",
286291 pkg. name( ) ,
287292 reg_name
288293 ) ;
289- }
290294 }
291295 }
292296 }
293297 }
294-
295- let sid = match reg {
296- RegistryOrIndex :: Index ( url) => SourceId :: for_registry ( & url) ?,
297- RegistryOrIndex :: Registry ( reg) if reg == CRATES_IO_REGISTRY => SourceId :: crates_io ( gctx) ?,
298- RegistryOrIndex :: Registry ( reg) => SourceId :: alt_registry ( gctx, & reg) ?,
299- } ;
300-
301- // Load source replacements that are built-in to Cargo.
302- let sid = SourceConfigMap :: empty ( gctx) ?
303- . load ( sid, & HashSet :: new ( ) ) ?
304- . replaced_source_id ( ) ;
305-
306- Ok ( sid)
298+ Ok ( ops:: registry:: get_source_id ( gctx, reg_or_index. as_ref ( ) ) ?. replacement )
307299}
308300
309301/// Just the part of the dependency graph that's between the packages we're packaging.
@@ -322,6 +314,12 @@ impl LocalDependencies {
322314 . map ( |name| self . packages [ & name] . clone ( ) )
323315 . collect ( )
324316 }
317+
318+ pub fn has_no_dependencies ( & self ) -> bool {
319+ self . graph
320+ . iter ( )
321+ . all ( |node| self . graph . edges ( node) . next ( ) . is_none ( ) )
322+ }
325323}
326324
327325/// Build just the part of the dependency graph that's between the given packages,
0 commit comments