From ab33bc0c36d66acc0f1c19237fb7d631a286672e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 18 Feb 2020 11:40:04 -0800 Subject: [PATCH 1/2] Add a warning if license-file points to a file that does not exist. --- src/cargo/ops/cargo_package.rs | 20 ++++++++++++++++++++ tests/testsuite/package.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 21522c57307..0d0665a61c5 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -200,6 +200,26 @@ fn check_metadata(pkg: &Package, config: &Config) -> CargoResult<()> { things = things ))? } + + if let Some(license_path) = &md.license_file { + let license_path = Path::new(license_path); + let abs_license_path = pkg.root().join(license_path); + if !abs_license_path.exists() { + let rel_msg = if license_path.is_absolute() { + "".to_string() + } else { + format!(" (relative to `{}`)", pkg.root().display()) + }; + config.shell().warn(&format!( + "license-file `{}` does not appear to exist{}.\n\ + Please update the license-file setting in the manifest at `{}`\n\ + This may become a hard error in the future.", + license_path.display(), + rel_msg, + pkg.manifest_path().display() + ))?; + } + } Ok(()) } diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index dce30877139..ae3eb2a7444 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -1445,3 +1445,33 @@ fn exclude_dot_files_and_directories_by_default() { ", ); } + +#[cargo_test] +fn invalid_license_file_path() { + // Test warning when license-file points to a non-existent file. + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "1.0.0" + license-file = "does-not-exist" + description = "foo" + homepage = "foo" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("package --no-verify") + .with_stderr( + "\ +[WARNING] license-file `does-not-exist` does not appear to exist (relative to `[..]/foo`). +Please update the license-file setting in the manifest at `[..]/foo/Cargo.toml` +This may become a hard error in the future. +[PACKAGING] foo v1.0.0 ([..]/foo) +", + ) + .run(); +} From 90887707ed5aea9f9a474d5708fc19e8966defcd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 19 Feb 2020 10:46:54 -0800 Subject: [PATCH 2/2] Copy license-file into package if outside of root. --- src/cargo/core/package.rs | 5 +- src/cargo/ops/cargo_package.rs | 409 +++++++++++++++------------- src/cargo/util/toml/mod.rs | 22 +- tests/testsuite/git.rs | 1 + tests/testsuite/package.rs | 222 +++++++++++++-- tests/testsuite/publish_lockfile.rs | 25 +- tests/testsuite/registry.rs | 7 +- 7 files changed, 464 insertions(+), 227 deletions(-) diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index f45867febde..ad1a536fa38 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -203,7 +203,10 @@ impl Package { } pub fn to_registry_toml(&self, config: &Config) -> CargoResult { - let manifest = self.manifest().original().prepare_for_publish(config)?; + let manifest = self + .manifest() + .original() + .prepare_for_publish(config, self.root())?; let toml = toml::to_string(&manifest)?; Ok(format!( "# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO\n\ diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 0d0665a61c5..9678dd4d368 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -1,26 +1,20 @@ use std::collections::{BTreeSet, HashMap}; -use std::fmt::Display; use std::fs::{self, File}; use std::io::prelude::*; use std::io::SeekFrom; -use std::path::{self, Path, PathBuf}; +use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::Arc; use std::time::SystemTime; use flate2::read::GzDecoder; -use flate2::write::GzEncoder; use flate2::{Compression, GzBuilder}; use log::debug; -use serde_json::{self, json}; use tar::{Archive, Builder, EntryType, Header}; use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor}; - -use crate::core::Feature; -use crate::core::{ - Package, PackageId, PackageSet, Resolve, Source, SourceId, Verbosity, Workspace, -}; +use crate::core::{Feature, Verbosity, Workspace}; +use crate::core::{Package, PackageId, PackageSet, Resolve, Source, SourceId}; use crate::ops; use crate::sources::PathSource; use crate::util::errors::{CargoResult, CargoResultExt}; @@ -41,7 +35,24 @@ pub struct PackageOpts<'cfg> { pub no_default_features: bool, } -static VCS_INFO_FILE: &str = ".cargo_vcs_info.json"; +const VCS_INFO_FILE: &str = ".cargo_vcs_info.json"; + +struct ArchiveFile { + /// The relative path in the archive (not including the top-level package + /// name directory). + rel_path: PathBuf, + /// String variant of `rel_path`, for convenience. + rel_str: String, + /// The contents to add to the archive. + contents: FileContents, +} + +enum FileContents { + /// Absolute path to the file on disk to add to the archive. + OnDisk(PathBuf), + /// Contents of a file generated in memory. + Generated(String), +} pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult> { if ws.root().join("Cargo.lock").exists() { @@ -68,40 +79,23 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult