Skip to content

Commit

Permalink
Merge pull request #209 from dtolnay/cratetypes
Browse files Browse the repository at this point in the history
Recognize when the current package has no lib target
  • Loading branch information
dtolnay committed Nov 27, 2022
2 parents e734d1c + 96764d0 commit cf55d34
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
11 changes: 9 additions & 2 deletions src/cargo.rs
Expand Up @@ -12,12 +12,19 @@ use std::{env, fs, iter};
pub struct Metadata {
pub target_directory: Directory,
pub workspace_root: Directory,
pub packages: Vec<Package>,
pub packages: Vec<PackageMetadata>,
}

#[derive(Deserialize)]
pub struct Package {
pub struct PackageMetadata {
pub name: String,
pub targets: Vec<BuildTarget>,
pub manifest_path: PathBuf,
}

#[derive(Deserialize)]
pub struct BuildTarget {
pub crate_types: Vec<String>,
}

fn raw_cargo() -> Command {
Expand Down
53 changes: 35 additions & 18 deletions src/run.rs
@@ -1,4 +1,4 @@
use crate::cargo::{self, Metadata};
use crate::cargo::{self, Metadata, PackageMetadata};
use crate::dependencies::{self, Dependency, EditionOrInherit};
use crate::directory::Directory;
use crate::env::Update;
Expand Down Expand Up @@ -155,6 +155,7 @@ impl Runner {
&workspace,
&project_name,
&source_dir,
&packages,
tests,
source_manifest,
)?;
Expand Down Expand Up @@ -205,6 +206,7 @@ impl Runner {
workspace: &Directory,
project_name: &str,
source_dir: &Directory,
packages: &[PackageMetadata],
tests: &[ExpandedTest],
source_manifest: dependencies::Manifest,
) -> Result<Manifest> {
Expand All @@ -223,22 +225,35 @@ impl Runner {
let mut dependencies = Map::new();
dependencies.extend(source_manifest.dependencies);
dependencies.extend(source_manifest.dev_dependencies);
dependencies.insert(
crate_name.clone(),
Dependency {
version: None,
path: Some(source_dir.clone()),
optional: false,
default_features: false,
features: Vec::new(),
git: None,
branch: None,
tag: None,
rev: None,
workspace: false,
rest: Map::new(),
},
);

let cargo_toml_path = source_dir.join("Cargo.toml");
let mut has_lib_target = true;
for package_metadata in packages {
if package_metadata.manifest_path == cargo_toml_path {
has_lib_target = package_metadata
.targets
.iter()
.any(|target| target.crate_types != ["bin"]);
}
}
if has_lib_target {
dependencies.insert(
crate_name.clone(),
Dependency {
version: None,
path: Some(source_dir.clone()),
optional: false,
default_features: false,
features: Vec::new(),
git: None,
branch: None,
tag: None,
rev: None,
workspace: false,
rest: Map::new(),
},
);
}

let mut targets = source_manifest.target;
for target in targets.values_mut() {
Expand All @@ -265,7 +280,9 @@ impl Runner {
}
false
});
enables.insert(0, format!("{}/{}", crate_name, feature));
if has_lib_target {
enables.insert(0, format!("{}/{}", crate_name, feature));
}
}

let mut manifest = Manifest {
Expand Down

0 comments on commit cf55d34

Please sign in to comment.