Skip to content

Commit eddf7b7

Browse files
committed
fix(publish): Don't strip non-dev features
First, we added support for stripping of local-only dev-dependencies. This was dual-implemented for `Cargo.toml` and `Summary`. This left off stripping of `dep/feature` that reference dev-dependencies (enabling features within dev-dependencies). When we fixed this, we again dual-implemented it. The `Cargo.toml` version was correct but the `Summary` version was instead stripping too many features, particularly features that reference renamed dependencies. We didn't have tests for this case and it wasn't caught earlier because crates.io re-generates the `Summary` from `Cargo.toml`, ignoring what we post. That makes this only show up with custom registries that trust what Cargo posts. Rather than fixing the `Summary` generation, I remove the dual-implementation and instead generate the `Summary` from the published `Cargo.toml`. Unfortunately, we don't have access directly to the packaged `Cargo.toml`. It could be passed around and I originally did so, hoping to remove use of the local `Package`. However, the local `Package` is needed for things like reading the `README`. So I scaled back and isolate the change to only what needs it. This also makes it easier for `prepare_transmit` callers. Fully open to someone exploring removing this extra `prepare_for_publish` in the future. Fixes #14321
1 parent dc9014d commit eddf7b7

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
288288
} else {
289289
let tarball = create_package(ws, &pkg, ar_files, local_reg.as_ref())?;
290290
if let Some(local_reg) = local_reg.as_mut() {
291-
local_reg.add_package(&pkg, &tarball)?;
291+
local_reg.add_package(ws, &pkg, &tarball)?;
292292
}
293293
outputs.push((pkg, opts, tarball));
294294
}
@@ -1299,7 +1299,12 @@ impl<'a> TmpRegistry<'a> {
12991299
self.root.join("index")
13001300
}
13011301

1302-
fn add_package(&mut self, package: &Package, tar: &FileLock) -> CargoResult<()> {
1302+
fn add_package(
1303+
&mut self,
1304+
ws: &Workspace<'_>,
1305+
package: &Package,
1306+
tar: &FileLock,
1307+
) -> CargoResult<()> {
13031308
debug!(
13041309
"adding package {}@{} to local overlay at {}",
13051310
package.name(),
@@ -1317,7 +1322,7 @@ impl<'a> TmpRegistry<'a> {
13171322
tar_copy.flush()?;
13181323
}
13191324

1320-
let new_crate = super::registry::prepare_transmit(self.gctx, package, self.upstream)?;
1325+
let new_crate = super::registry::prepare_transmit(self.gctx, ws, package, self.upstream)?;
13211326

13221327
tar.file().seek(SeekFrom::Start(0))?;
13231328
let cksum = cargo_util::Sha256::new()

src/cargo/ops/registry/publish.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! [1]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#publish
44
55
use std::collections::BTreeMap;
6-
use std::collections::BTreeSet;
76
use std::collections::HashSet;
87
use std::fs::File;
98
use std::time::Duration;
@@ -21,7 +20,6 @@ use crate::core::dependency::DepKind;
2120
use crate::core::manifest::ManifestMetadata;
2221
use crate::core::resolver::CliFeatures;
2322
use crate::core::Dependency;
24-
use crate::core::FeatureValue;
2523
use crate::core::Package;
2624
use crate::core::PackageIdSpecQuery;
2725
use crate::core::SourceId;
@@ -35,7 +33,7 @@ use crate::sources::CRATES_IO_REGISTRY;
3533
use crate::util::auth;
3634
use crate::util::cache_lock::CacheLockMode;
3735
use crate::util::context::JobsConfig;
38-
use crate::util::interning::InternedString;
36+
use crate::util::toml::prepare_for_publish;
3937
use crate::util::Progress;
4038
use crate::util::ProgressStyle;
4139
use crate::CargoResult;
@@ -185,6 +183,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
185183
.status("Uploading", pkg.package_id().to_string())?;
186184
transmit(
187185
opts.gctx,
186+
ws,
188187
pkg,
189188
tarball.file(),
190189
&mut registry,
@@ -324,16 +323,16 @@ fn verify_dependencies(
324323

325324
pub(crate) fn prepare_transmit(
326325
gctx: &GlobalContext,
326+
ws: &Workspace<'_>,
327327
local_pkg: &Package,
328328
registry_id: SourceId,
329329
) -> CargoResult<NewCrate> {
330-
let deps = local_pkg
330+
let included = None; // don't filter build-targets
331+
let publish_pkg = prepare_for_publish(local_pkg, ws, included)?;
332+
333+
let deps = publish_pkg
331334
.dependencies()
332335
.iter()
333-
.filter(|dep| {
334-
// Skip dev-dependency without version.
335-
dep.is_transitive() || dep.specified_req()
336-
})
337336
.map(|dep| {
338337
// If the dependency is from a different registry, then include the
339338
// registry in the dependency.
@@ -378,7 +377,7 @@ pub(crate) fn prepare_transmit(
378377
})
379378
})
380379
.collect::<CargoResult<Vec<NewCrateDependency>>>()?;
381-
let manifest = local_pkg.manifest();
380+
let manifest = publish_pkg.manifest();
382381
let ManifestMetadata {
383382
ref authors,
384383
ref description,
@@ -395,54 +394,39 @@ pub(crate) fn prepare_transmit(
395394
ref rust_version,
396395
} = *manifest.metadata();
397396
let rust_version = rust_version.as_ref().map(ToString::to_string);
398-
let readme_content = readme
397+
let readme_content = local_pkg
398+
.manifest()
399+
.metadata()
400+
.readme
399401
.as_ref()
400402
.map(|readme| {
401403
paths::read(&local_pkg.root().join(readme)).with_context(|| {
402404
format!("failed to read `readme` file for package `{}`", local_pkg)
403405
})
404406
})
405407
.transpose()?;
406-
if let Some(ref file) = *license_file {
408+
if let Some(ref file) = local_pkg.manifest().metadata().license_file {
407409
if !local_pkg.root().join(file).exists() {
408410
bail!("the license file `{}` does not exist", file)
409411
}
410412
}
411413

412-
let deps_set = deps
413-
.iter()
414-
.map(|dep| dep.name.clone())
415-
.collect::<BTreeSet<String>>();
416-
417414
let string_features = match manifest.resolved_toml().features() {
418415
Some(features) => features
419416
.iter()
420417
.map(|(feat, values)| {
421418
(
422419
feat.to_string(),
423-
values
424-
.iter()
425-
.filter(|fv| {
426-
let feature_value = FeatureValue::new(InternedString::new(fv));
427-
match feature_value {
428-
FeatureValue::Dep { dep_name }
429-
| FeatureValue::DepFeature { dep_name, .. } => {
430-
deps_set.contains(&dep_name.to_string())
431-
}
432-
_ => true,
433-
}
434-
})
435-
.map(|fv| fv.to_string())
436-
.collect(),
420+
values.iter().map(|fv| fv.to_string()).collect(),
437421
)
438422
})
439423
.collect::<BTreeMap<String, Vec<String>>>(),
440424
None => BTreeMap::new(),
441425
};
442426

443427
Ok(NewCrate {
444-
name: local_pkg.name().to_string(),
445-
vers: local_pkg.version().to_string(),
428+
name: publish_pkg.name().to_string(),
429+
vers: publish_pkg.version().to_string(),
446430
deps,
447431
features: string_features,
448432
authors: authors.clone(),
@@ -464,13 +448,14 @@ pub(crate) fn prepare_transmit(
464448

465449
fn transmit(
466450
gctx: &GlobalContext,
451+
ws: &Workspace<'_>,
467452
pkg: &Package,
468453
tarball: &File,
469454
registry: &mut Registry,
470455
registry_id: SourceId,
471456
dry_run: bool,
472457
) -> CargoResult<()> {
473-
let new_crate = prepare_transmit(gctx, pkg, registry_id)?;
458+
let new_crate = prepare_transmit(gctx, ws, pkg, registry_id)?;
474459

475460
// Do not upload if performing a dry run
476461
if dry_run {

tests/testsuite/inheritable_workspace_fields.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,11 @@ You may press ctrl-c to skip waiting; the crate should be available shortly.
752752
"homepage": "https://www.rust-lang.org",
753753
"keywords": ["cli"],
754754
"license": "MIT",
755-
"license_file": "../LICENSE",
755+
"license_file": "LICENSE",
756756
"links": null,
757757
"name": "bar",
758758
"readme": "README.md",
759-
"readme_file": "../README.md",
759+
"readme_file": "README.md",
760760
"repository": "https://github.com/example/example",
761761
"rust_version": "1.60",
762762
"vers": "1.2.3"

tests/testsuite/publish.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,8 @@ You may press ctrl-c to skip waiting; the crate should be available shortly.
18651865
"target-build-only/cat",
18661866
"target-normal-and-dev/cat",
18671867
"optional-dep-feature/cat",
1868-
"dep:optional-namespaced"
1868+
"dep:optional-namespaced",
1869+
"dep:optional-renamed-namespaced10"
18691870
]
18701871
},
18711872
"homepage": "foo",

0 commit comments

Comments
 (0)