Skip to content

Commit

Permalink
Merge pull request #1170 from messense/always-include-lockfile
Browse files Browse the repository at this point in the history
Include `Cargo.lock` by default in source distribution
  • Loading branch information
messense authored Oct 6, 2022
2 parents 72051bf + 0a2da0e commit 8f4d1be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix `maturin develop` in Windows conda virtual environment in [#1146](https://github.com/PyO3/maturin/pull/1146)
* Fix build for crate using `pyo3` and `build.rs` without `cdylib` crate type in [#1150](https://github.com/PyO3/maturin/pull/1150)
* Fix build on some 32-bit platform by downgrading `indicatif` in [#1163](https://github.com/PyO3/maturin/pull/1163)
* Include `Cargo.lock` by default in source distribution in [#1170](https://github.com/PyO3/maturin/pull/1170)

## [0.13.5] - 2022-09-27

Expand Down
36 changes: 27 additions & 9 deletions src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use fs_err as fs;
use fs_err::File;
use ignore::WalkBuilder;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fmt::Write as _;
#[cfg(target_family = "unix")]
Expand Down Expand Up @@ -312,6 +312,7 @@ impl WheelWriter {
pub struct SDistWriter {
tar: tar::Builder<GzEncoder<File>>,
path: PathBuf,
files: HashSet<PathBuf>,
}

impl ModuleWriter for SDistWriter {
Expand All @@ -325,35 +326,48 @@ impl ModuleWriter for SDistWriter {
bytes: &[u8],
permissions: u32,
) -> Result<()> {
let target = target.as_ref();
if self.files.contains(target) {
// Ignore duplicate files
return Ok(());
}
let mut header = tar::Header::new_gnu();
header.set_size(bytes.len() as u64);
header.set_mode(permissions);
header.set_cksum();
self.tar
.append_data(&mut header, &target, bytes)
.append_data(&mut header, target, bytes)
.context(format!(
"Failed to add {} bytes to sdist as {}",
bytes.len(),
target.as_ref().display()
target.display()
))?;
self.files.insert(target.to_path_buf());
Ok(())
}

fn add_file(&mut self, target: impl AsRef<Path>, source: impl AsRef<Path>) -> Result<()> {
if source.as_ref() == self.path {
let source = source.as_ref();
let target = target.as_ref();
if source == self.path {
bail!(
"Attempting to include the sdist output tarball {} into itself! Check 'cargo package --list' output.",
source.as_ref().display()
source.display()
);
}
if self.files.contains(target) {
// Ignore duplicate files
return Ok(());
}

self.tar
.append_path_with_name(&source, &target)
.append_path_with_name(source, target)
.context(format!(
"Failed to add file from {} to sdist as {}",
source.as_ref().display(),
target.as_ref().display(),
source.display(),
target.display(),
))?;
self.files.insert(target.to_path_buf());
Ok(())
}
}
Expand All @@ -371,7 +385,11 @@ impl SDistWriter {
let enc = GzEncoder::new(tar_gz, Compression::default());
let tar = tar::Builder::new(enc);

Ok(Self { tar, path })
Ok(Self {
tar,
path,
files: HashSet::new(),
})
}

/// Finished the .tar.gz archive
Expand Down
15 changes: 10 additions & 5 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ fn add_crate_to_source_distribution(
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)?;
}
Expand Down Expand Up @@ -468,12 +469,16 @@ 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 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() {
writer.add_file(root_dir.join("Cargo.lock"), &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
Expand Down
1 change: 1 addition & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8f4d1be

Please sign in to comment.