Skip to content

Commit

Permalink
Include Cargo.lock by default in source distribution
Browse files Browse the repository at this point in the history
* Required if `--locked`/`--frozen` is set
* Include by default when exists, otherwise warn
  • Loading branch information
messense committed Oct 6, 2022
1 parent 72051bf commit 64a6526
Showing 2 changed files with 23 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use crate::{BuildContext, PyProjectToml, SDistWriter};
use anyhow::{bail, Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
use fs_err as fs;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
@@ -236,7 +236,7 @@ fn add_crate_to_source_distribution(
prefix: impl AsRef<Path>,
known_path_deps: &HashMap<String, PathBuf>,
root_crate: bool,
) -> Result<()> {
) -> Result<HashSet<PathBuf>> {
let manifest_path = manifest_path.as_ref();
let pyproject_toml_path = pyproject_toml_path.as_ref();
let output = Command::new("cargo")
@@ -336,17 +336,22 @@ fn add_crate_to_source_distribution(
let prefix = prefix.as_ref();
writer.add_directory(prefix)?;

let mut added_files = HashSet::new();
added_files.insert(prefix.to_path_buf());

let cargo_toml = if cargo_toml_in_subdir {
prefix.join(manifest_path)
} else {
prefix.join(manifest_path.file_name().unwrap())
};
writer.add_bytes(cargo_toml, rewritten_cargo_toml.as_bytes())?;
for (target, source) in target_source {
writer.add_file(prefix.join(target), source)?;
let target = prefix.join(target);
writer.add_file(&target, source)?;
added_files.insert(target);
}

Ok(())
Ok(added_files)
}

/// Finds all path dependencies of the crate
@@ -457,7 +462,7 @@ pub fn source_distribution(
}

// Add the main crate
add_crate_to_source_distribution(
let added_files = add_crate_to_source_distribution(
&mut writer,
&pyproject_toml_path,
&manifest_path,
@@ -468,12 +473,19 @@ pub fn source_distribution(
)?;

let manifest_dir = manifest_path.parent().unwrap();
let include_cargo_lock =
let cargo_lock_path = manifest_dir.join("Cargo.lock");
let target = root_dir.join("Cargo.lock");
let cargo_lock_required =
build_context.cargo_options.locked || build_context.cargo_options.frozen;
if include_cargo_lock {
let cargo_lock_path = manifest_dir.join("Cargo.lock");
let target = root_dir.join("Cargo.lock");
writer.add_file(&target, &cargo_lock_path)?;
if cargo_lock_required || cargo_lock_path.exists() {
if !added_files.contains(&target) {
writer.add_file(&target, &cargo_lock_path)?;
}
} else {
println!(
"⚠️ Warning: Cargo.lock is not found, it is recommended \
to include it in the source distribution"
);
}

// Add readme, license and python source files
1 change: 1 addition & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
@@ -289,6 +289,7 @@ fn lib_with_path_dep_sdist() {
"sdist_with_path_dep-0.1.0/local_dependencies/transitive_path_dep/Cargo.toml",
"sdist_with_path_dep-0.1.0/local_dependencies/transitive_path_dep/src/lib.rs",
"sdist_with_path_dep-0.1.0/Cargo.toml",
"sdist_with_path_dep-0.1.0/Cargo.lock",
"sdist_with_path_dep-0.1.0/pyproject.toml",
"sdist_with_path_dep-0.1.0/src/lib.rs",
"sdist_with_path_dep-0.1.0/PKG-INFO",

0 comments on commit 64a6526

Please sign in to comment.