Skip to content

Commit ca2346b

Browse files
committed
Auto merge of #14328 - epage:14321-cherry-pick, r=weihanglo
[beta-1.81] fix(publish): Don't strip non-dev features Beta backports * #14325 In order to make CI pass, the following PRs are also cherry-picked: * #14282 363ddd4
2 parents a2b58c3 + 25ecfce commit ca2346b

File tree

6 files changed

+114
-155
lines changed

6 files changed

+114
-155
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ fn tar(
732732
.iter()
733733
.map(|ar_file| ar_file.rel_path.clone())
734734
.collect::<Vec<_>>();
735-
let publish_pkg = prepare_for_publish(pkg, ws, &included)?;
735+
let publish_pkg = prepare_for_publish(pkg, ws, Some(&included))?;
736736

737737
let mut uncompressed_size = 0;
738738
for ar_file in ar_files {

src/cargo/ops/registry/publish.rs

Lines changed: 21 additions & 36 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;
@@ -184,6 +182,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
184182
.status("Uploading", pkg.package_id().to_string())?;
185183
transmit(
186184
opts.gctx,
185+
ws,
187186
pkg,
188187
tarball.file(),
189188
&mut registry,
@@ -323,19 +322,19 @@ fn verify_dependencies(
323322

324323
fn transmit(
325324
gctx: &GlobalContext,
326-
pkg: &Package,
325+
ws: &Workspace<'_>,
326+
local_pkg: &Package,
327327
tarball: &File,
328328
registry: &mut Registry,
329329
registry_id: SourceId,
330330
dry_run: bool,
331331
) -> CargoResult<()> {
332-
let deps = pkg
332+
let included = None; // don't filter build-targets
333+
let publish_pkg = prepare_for_publish(local_pkg, ws, included)?;
334+
335+
let deps = publish_pkg
333336
.dependencies()
334337
.iter()
335-
.filter(|dep| {
336-
// Skip dev-dependency without version.
337-
dep.is_transitive() || dep.specified_req()
338-
})
339338
.map(|dep| {
340339
// If the dependency is from a different registry, then include the
341340
// registry in the dependency.
@@ -380,7 +379,7 @@ fn transmit(
380379
})
381380
})
382381
.collect::<CargoResult<Vec<NewCrateDependency>>>()?;
383-
let manifest = pkg.manifest();
382+
let manifest = publish_pkg.manifest();
384383
let ManifestMetadata {
385384
ref authors,
386385
ref description,
@@ -397,15 +396,19 @@ fn transmit(
397396
ref rust_version,
398397
} = *manifest.metadata();
399398
let rust_version = rust_version.as_ref().map(ToString::to_string);
400-
let readme_content = readme
399+
let readme_content = local_pkg
400+
.manifest()
401+
.metadata()
402+
.readme
401403
.as_ref()
402404
.map(|readme| {
403-
paths::read(&pkg.root().join(readme))
404-
.with_context(|| format!("failed to read `readme` file for package `{}`", pkg))
405+
paths::read(&local_pkg.root().join(readme)).with_context(|| {
406+
format!("failed to read `readme` file for package `{}`", local_pkg)
407+
})
405408
})
406409
.transpose()?;
407-
if let Some(ref file) = *license_file {
408-
if !pkg.root().join(file).exists() {
410+
if let Some(ref file) = local_pkg.manifest().metadata().license_file {
411+
if !local_pkg.root().join(file).exists() {
409412
bail!("the license file `{}` does not exist", file)
410413
}
411414
}
@@ -416,31 +419,13 @@ fn transmit(
416419
return Ok(());
417420
}
418421

419-
let deps_set = deps
420-
.iter()
421-
.map(|dep| dep.name.clone())
422-
.collect::<BTreeSet<String>>();
423-
424422
let string_features = match manifest.resolved_toml().features() {
425423
Some(features) => features
426424
.iter()
427425
.map(|(feat, values)| {
428426
(
429427
feat.to_string(),
430-
values
431-
.iter()
432-
.filter(|fv| {
433-
let feature_value = FeatureValue::new(InternedString::new(fv));
434-
match feature_value {
435-
FeatureValue::Dep { dep_name }
436-
| FeatureValue::DepFeature { dep_name, .. } => {
437-
deps_set.contains(&dep_name.to_string())
438-
}
439-
_ => true,
440-
}
441-
})
442-
.map(|fv| fv.to_string())
443-
.collect(),
428+
values.iter().map(|fv| fv.to_string()).collect(),
444429
)
445430
})
446431
.collect::<BTreeMap<String, Vec<String>>>(),
@@ -450,8 +435,8 @@ fn transmit(
450435
let warnings = registry
451436
.publish(
452437
&NewCrate {
453-
name: pkg.name().to_string(),
454-
vers: pkg.version().to_string(),
438+
name: local_pkg.name().to_string(),
439+
vers: local_pkg.version().to_string(),
455440
deps,
456441
features: string_features,
457442
authors: authors.clone(),

src/cargo/util/toml/mod.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,7 @@ fn unused_dep_keys(
25752575
pub fn prepare_for_publish(
25762576
me: &Package,
25772577
ws: &Workspace<'_>,
2578-
included: &[PathBuf],
2578+
included: Option<&[PathBuf]>,
25792579
) -> CargoResult<Package> {
25802580
let contents = me.manifest().contents();
25812581
let document = me.manifest().document();
@@ -2612,7 +2612,7 @@ fn prepare_toml_for_publish(
26122612
me: &manifest::TomlManifest,
26132613
ws: &Workspace<'_>,
26142614
package_root: &Path,
2615-
included: &[PathBuf],
2615+
included: Option<&[PathBuf]>,
26162616
) -> CargoResult<manifest::TomlManifest> {
26172617
let gctx = ws.gctx();
26182618

@@ -2629,7 +2629,8 @@ fn prepare_toml_for_publish(
26292629
package.workspace = None;
26302630
if let Some(StringOrBool::String(path)) = &package.build {
26312631
let path = paths::normalize_path(Path::new(path));
2632-
let build = if included.contains(&path) {
2632+
let included = included.map(|i| i.contains(&path)).unwrap_or(true);
2633+
let build = if included {
26332634
let path = path
26342635
.into_os_string()
26352636
.into_string()
@@ -2898,7 +2899,7 @@ fn prepare_toml_for_publish(
28982899

28992900
fn prepare_targets_for_publish(
29002901
targets: Option<&Vec<manifest::TomlTarget>>,
2901-
included: &[PathBuf],
2902+
included: Option<&[PathBuf]>,
29022903
context: &str,
29032904
gctx: &GlobalContext,
29042905
) -> CargoResult<Option<Vec<manifest::TomlTarget>>> {
@@ -2923,19 +2924,21 @@ fn prepare_targets_for_publish(
29232924

29242925
fn prepare_target_for_publish(
29252926
target: &manifest::TomlTarget,
2926-
included: &[PathBuf],
2927+
included: Option<&[PathBuf]>,
29272928
context: &str,
29282929
gctx: &GlobalContext,
29292930
) -> CargoResult<Option<manifest::TomlTarget>> {
29302931
let path = target.path.as_ref().expect("previously resolved");
29312932
let path = normalize_path(&path.0);
2932-
if !included.contains(&path) {
2933-
let name = target.name.as_ref().expect("previously resolved");
2934-
gctx.shell().warn(format!(
2935-
"ignoring {context} `{name}` as `{}` is not included in the published package",
2936-
path.display()
2937-
))?;
2938-
return Ok(None);
2933+
if let Some(included) = included {
2934+
if !included.contains(&path) {
2935+
let name = target.name.as_ref().expect("previously resolved");
2936+
gctx.shell().warn(format!(
2937+
"ignoring {context} `{name}` as `{}` is not included in the published package",
2938+
path.display()
2939+
))?;
2940+
return Ok(None);
2941+
}
29392942
}
29402943

29412944
let mut target = target.clone();

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/lints/implicit_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ unused_optional_dependency = "allow"
127127
.masquerade_as_nightly_cargo(&["cargo-lints", "edition2024"])
128128
.with_stderr_data(str![[r#"
129129
[UPDATING] `dummy-registry` index
130-
[LOCKING] 2 packages to latest Rust 1.81.0-nightly compatible versions
130+
[LOCKING] 2 packages to latest Rust [..] compatible versions
131131
[CHECKING] foo v0.1.0 ([ROOT]/foo)
132132
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
133133

0 commit comments

Comments
 (0)