Skip to content

Commit

Permalink
Expose all profiles in the workspace.
Browse files Browse the repository at this point in the history
Implement a new method Profiles::all that returns all profiles in the workspace, including the root and predefined profiles.

This new method follows Cargo resolution rules to resolve profile inheritance and ordering.

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera committed Sep 25, 2023
1 parent 30af6de commit 5960ae1
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 310 deletions.
437 changes: 258 additions & 179 deletions src/cargo/core/profiles.rs

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use crate::core::compiler::artifact::match_artifacts_kind_with_targets;
use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::package::SerializedPackage;
use crate::core::profiles::{Profile, Profiles};
use crate::core::resolver::{features::CliFeatures, HasDevUnits, Resolve};
use crate::core::{Package, PackageId, Workspace};
use crate::ops::{self, Packages};
use crate::util::interning::InternedString;
use crate::util::toml::TomlProfiles;
use crate::util::CargoResult;
use cargo_platform::Platform;
use serde::Serialize;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;

const VERSION: u32 = 1;
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
version: VERSION,
workspace_root: ws.root().to_path_buf(),
metadata: ws.custom_metadata().cloned(),
profiles: ws.profiles().cloned(),
profiles: Profiles::all(ws)?,
})
}

Expand All @@ -67,8 +67,7 @@ pub struct ExportInfo {
version: u32,
workspace_root: PathBuf,
metadata: Option<toml::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
profiles: Option<TomlProfiles>,
profiles: HashMap<InternedString, Profile>,
}

#[derive(Serialize)]
Expand Down
26 changes: 17 additions & 9 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use cargo_test_support::registry::{self, Package, RegistryBuilder};
use cargo_test_support::{basic_manifest, paths, project};
use std::fs;

use crate::metadata::DEFAULT_PROFILES;

#[cargo_test]
fn depend_on_alt_registry() {
registry::alt_init();
Expand Down Expand Up @@ -844,7 +846,7 @@ fn alt_reg_metadata() {
// iodep -> altdep2: alternative-registry
p.cargo("metadata --format-version=1 --no-deps")
.with_json(
r#"
&r#"
{
"packages": [
{
Expand Down Expand Up @@ -909,15 +911,17 @@ fn alt_reg_metadata() {
"target_directory": "[..]/foo/target",
"version": 1,
"workspace_root": "[..]/foo",
"metadata": null
}"#,
"metadata": null,
"profiles": $PROFILES
}"#
.replace("$PROFILES", DEFAULT_PROFILES),
)
.run();

// --no-deps uses a different code path, make sure both work.
p.cargo("metadata --format-version=1")
.with_json(
r#"
&r#"
{
"packages": [
{
Expand Down Expand Up @@ -1112,8 +1116,10 @@ fn alt_reg_metadata() {
"target_directory": "[..]/foo/target",
"version": 1,
"workspace_root": "[..]/foo",
"metadata": null
}"#,
"metadata": null,
"profiles": $PROFILES
}"#
.replace("$PROFILES", DEFAULT_PROFILES),
)
.run();
}
Expand Down Expand Up @@ -1160,7 +1166,7 @@ fn unknown_registry() {
// bar -> baz registry = alternate
p.cargo("metadata --format-version=1")
.with_json(
r#"
&r#"
{
"packages": [
{
Expand Down Expand Up @@ -1278,9 +1284,11 @@ fn unknown_registry() {
"target_directory": "[..]/foo/target",
"version": 1,
"workspace_root": "[..]/foo",
"metadata": null
"metadata": null,
"profiles": $PROFILES
}
"#,
"#
.replace("$PROFILES", DEFAULT_PROFILES),
)
.run();
}
Expand Down
10 changes: 7 additions & 3 deletions tests/testsuite/features_namespaced.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Tests for namespaced features.
use crate::metadata::DEFAULT_PROFILES;

use super::features2::switch_to_resolver_2;
use cargo_test_support::registry::{Dependency, Package, RegistryBuilder};
use cargo_test_support::{project, publish};
Expand Down Expand Up @@ -576,7 +578,7 @@ fn json_exposed() {

p.cargo("metadata --no-deps")
.with_json(
r#"
&r#"
{
"packages": [
{
Expand Down Expand Up @@ -614,9 +616,11 @@ fn json_exposed() {
"target_directory": "[..]foo/target",
"version": 1,
"workspace_root": "[..]foo",
"metadata": null
"metadata": null,
"profiles": $PROFILES
}
"#,
"#
.replace("$PROFILES", DEFAULT_PROFILES),
)
.run();
}
Expand Down
6 changes: 5 additions & 1 deletion tests/testsuite/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use cargo_test_support::registry::Package;
use cargo_test_support::{basic_lib_manifest, basic_manifest, git, main_file, path2url, project};
use cargo_test_support::{sleep_ms, t, Project};

use crate::metadata::DEFAULT_PROFILES;

#[cargo_test]
fn cargo_compile_simple_git_dep() {
let project = project();
Expand Down Expand Up @@ -3370,11 +3372,13 @@ fn metadata_master_consistency() {
"target_directory": "[..]",
"version": 1,
"workspace_root": "[..]",
"metadata": null
"metadata": null,
"profiles": $PROFILES
}
"#
.replace("__BAR_SOURCE__", bar_source)
.replace("__BAR_HASH__", &bar_hash)
.replace("$PROFILES", DEFAULT_PROFILES)
};

let bar_source = format!("git+{}?branch=master", git_project.url());
Expand Down
Loading

0 comments on commit 5960ae1

Please sign in to comment.