Skip to content

Commit 08a7a89

Browse files
committed
publish: Read dependencies metadata from embedded Cargo.toml file
1 parent 963d83b commit 08a7a89

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ axum = { version = "=0.6.20", features = ["headers", "macros", "matched-path"] }
3838
axum-extra = { version = "=0.8.0", features = ["cookie-signed"] }
3939
base64 = "=0.21.4"
4040
bigdecimal = "=0.4.1"
41+
cargo-manifest = "=0.12.0"
4142
crates_io_index = { path = "crates_io_index" }
4243
crates_io_markdown = { path = "crates_io_markdown" }
4344
crates_io_tarball = { path = "crates_io_tarball" }
@@ -98,7 +99,6 @@ url = "=2.4.1"
9899

99100
[dev-dependencies]
100101
bytes = "=1.5.0"
101-
cargo-manifest = "=0.12.0"
102102
crates_io_index = { path = "crates_io_index", features = ["testing"] }
103103
crates_io_tarball = { path = "crates_io_tarball", features = ["builder"] }
104104
claims = "=0.7.1"

src/controllers/krate/publish.rs

+80-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::auth::AuthCheck;
44
use crate::background_jobs::{Job, PRIORITY_RENDER_README};
55
use axum::body::Bytes;
6+
use cargo_manifest::{Dependency, DepsSet, TargetDepsSet};
67
use crates_io_tarball::{process_tarball, TarballError};
78
use diesel::connection::DefaultLoadingMode;
89
use diesel::dsl::{exists, select};
@@ -305,12 +306,19 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
305306
VersionAction::Publish,
306307
)?;
307308

308-
for dep in &metadata.deps {
309+
let deps = convert_dependencies(
310+
tarball_info.manifest.dependencies.as_ref(),
311+
tarball_info.manifest.dev_dependencies.as_ref(),
312+
tarball_info.manifest.build_dependencies.as_ref(),
313+
tarball_info.manifest.target.as_ref()
314+
);
315+
316+
for dep in &deps {
309317
validate_dependency(dep)?;
310318
}
311319

312320
// Link this new version to all dependencies
313-
add_dependencies(conn, &metadata.deps, version.id)?;
321+
add_dependencies(conn, &deps, version.id)?;
314322

315323
// Update all keywords for this crate
316324
Keyword::update_crate(conn, &krate, &keywords)?;
@@ -455,6 +463,76 @@ fn missing_metadata_error_message(missing: &[&str]) -> String {
455463
)
456464
}
457465

466+
fn convert_dependencies(
467+
normal_deps: Option<&DepsSet>,
468+
dev_deps: Option<&DepsSet>,
469+
build_deps: Option<&DepsSet>,
470+
targets: Option<&TargetDepsSet>,
471+
) -> Vec<EncodableCrateDependency> {
472+
use DependencyKind as Kind;
473+
474+
let mut result = vec![];
475+
476+
let mut add = |deps_set: &DepsSet, kind: Kind, target: Option<&str>| {
477+
for (name, dep) in deps_set {
478+
result.push(convert_dependency(name, dep, kind, target));
479+
}
480+
};
481+
482+
if let Some(deps) = normal_deps {
483+
add(deps, Kind::Normal, None);
484+
}
485+
if let Some(deps) = dev_deps {
486+
add(deps, Kind::Dev, None);
487+
}
488+
if let Some(deps_set) = build_deps {
489+
add(deps_set, Kind::Build, None);
490+
}
491+
if let Some(target_deps_set) = targets {
492+
for (target, deps) in target_deps_set {
493+
add(&deps.dependencies, Kind::Normal, Some(target));
494+
add(&deps.dev_dependencies, Kind::Dev, Some(target));
495+
add(&deps.build_dependencies, Kind::Build, Some(target));
496+
}
497+
}
498+
499+
result
500+
}
501+
502+
fn convert_dependency(
503+
name: &str,
504+
dep: &Dependency,
505+
kind: DependencyKind,
506+
target: Option<&str>,
507+
) -> EncodableCrateDependency {
508+
let details = dep.detail();
509+
let req = dep.req();
510+
511+
let (crate_name, explicit_name_in_toml) = match details.and_then(|it| it.package.clone()) {
512+
None => (name.to_string(), None),
513+
Some(package) => (package, Some(name.to_string())),
514+
};
515+
516+
let optional = details.and_then(|it| it.optional).unwrap_or(false);
517+
let default_features = details.and_then(|it| it.default_features).unwrap_or(true);
518+
let features = details
519+
.and_then(|it| it.features.clone())
520+
.unwrap_or_default();
521+
let registry = details.and_then(|it| it.registry.clone());
522+
523+
EncodableCrateDependency {
524+
name: crate_name,
525+
version_req: req.to_string(),
526+
optional,
527+
default_features,
528+
features,
529+
target: target.map(ToString::to_string),
530+
kind: Some(kind),
531+
explicit_name_in_toml,
532+
registry,
533+
}
534+
}
535+
458536
pub fn validate_dependency(dep: &EncodableCrateDependency) -> AppResult<()> {
459537
if !Crate::valid_name(&dep.name) {
460538
return Err(cargo_err(&format_args!(

src/tests/builders/publish.rs

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ impl PublishBuilder {
136136
let metadata = u::PublishMetadata {
137137
name: self.krate_name.clone(),
138138
vers: self.version.to_string(),
139-
deps: self.deps.clone(),
140139
readme: self.readme,
141140
readme_file: None,
142141
};

src/views/krate_publish.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ use crate::models::DependencyKind;
1111
pub struct PublishMetadata {
1212
pub name: String,
1313
pub vers: String,
14-
pub deps: Vec<EncodableCrateDependency>,
1514
pub readme: Option<String>,
1615
pub readme_file: Option<String>,
1716
}
1817

19-
#[derive(Serialize, Deserialize, Clone, Debug)]
18+
#[derive(Debug)]
2019
pub struct EncodableCrateDependency {
2120
pub optional: bool,
2221
pub default_features: bool,

0 commit comments

Comments
 (0)