Skip to content

Commit 19090ae

Browse files
committed
chore: Make pup.ron discovery respect workspace root
1 parent 68f7df7 commit 19090ae

8 files changed

Lines changed: 109 additions & 8 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ anyhow = "1.0.95"
2626
regex = "1.11.1"
2727
tempfile = "3.14.0"
2828
ron = "0.8.1"
29+
cargo_metadata = "0.18"
2930

3031
[dependencies]
3132
ansi_term = "0.12.1"
3233
anyhow = { workspace = true }
3334
tempfile = { workspace = true }
3435
ron = { workspace = true }
36+
cargo_metadata = { workspace = true }
3537
cargo_pup_common = { path = "cargo_pup_common", version = "0.1.1" }
3638
cargo_pup_lint_impl = { path = "cargo_pup_lint_impl", version = "0.1.1" }
3739
cargo_pup_lint_config = { path = "cargo_pup_lint_config", version = "0.1.1" }

cargo_pup_common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ serde = { workspace = true }
1313
serde_json = {workspace = true }
1414
anyhow = { workspace = true }
1515
tempfile.workspace = true
16+
cargo_metadata = { workspace = true }

cargo_pup_common/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
pub mod cli;
44
pub mod project_context;
5+
pub mod workspace;

cargo_pup_common/src/workspace.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This product includes software developed at Datadog (https://www.datadoghq.com/) Copyright 2024 Datadog, Inc.
2+
3+
use cargo_metadata::MetadataCommand;
4+
use std::path::PathBuf;
5+
6+
/// Find pup.ron in workspace root using cargo metadata
7+
pub fn find_workspace_pup_ron() -> Option<PathBuf> {
8+
let metadata = MetadataCommand::new().no_deps().exec().ok()?;
9+
let pup_ron = metadata.workspace_root.join("pup.ron");
10+
if pup_ron.exists() {
11+
Some(pup_ron.into_std_path_buf())
12+
} else {
13+
None
14+
}
15+
}

src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ use cargo_pup_common::cli::{PupArgs, PupCli, PupCommand};
6969
use ansi_term::Colour::{Blue, Cyan, Green, Red, Yellow};
7070
use ansi_term::Style;
7171
use cargo_pup_common::project_context::{PUP_DIR, ProjectContext};
72+
use cargo_pup_common::workspace::find_workspace_pup_ron;
7273
use cargo_pup_lint_config::LintBuilder;
7374
use std::env;
7475
use std::error::Error;
7576
use std::fmt;
76-
use std::path::Path;
77+
use std::path::{Path, PathBuf};
7778
use std::process::{Command, exit};
7879

7980
#[derive(Debug, PartialEq)]
@@ -105,8 +106,11 @@ impl Error for CommandExitStatus {}
105106
/// Validates the current directory to determine the project type
106107
fn validate_project(config_path: Option<&str>) -> ProjectType {
107108
let pup_ron_path = match config_path {
108-
Some(path) => Path::new(path),
109-
None => Path::new("./pup.ron"),
109+
Some(path) => PathBuf::from(path),
110+
None => {
111+
// Prioritize workspace root pup.ron, fallback to local only if not in workspace
112+
find_workspace_pup_ron().unwrap_or_else(|| PathBuf::from("./pup.ron"))
113+
}
110114
};
111115
let cargo_toml_path = Path::new("./Cargo.toml");
112116

@@ -123,6 +127,7 @@ fn validate_project(config_path: Option<&str>) -> ProjectType {
123127
}
124128
}
125129

130+
126131
fn show_ascii_puppy() {
127132
println!(
128133
"{}",
@@ -352,9 +357,11 @@ where
352357
cmd.arg("run").arg(&toolchain).arg("cargo");
353358

354359
// Set up environment variables
360+
let current_dir = env::current_dir().map_err(|_| CommandExitStatus(1))?;
355361
cmd.env("RUSTC_WORKSPACE_WRAPPER", get_pup_path())
356362
.env("PUP_CLI_ARGS", cli_args)
357363
.env("PUP_CARGO_ARGS", cargo_args_str)
364+
.env("PUP_ORIGINAL_DIR", current_dir.to_str().unwrap())
358365
.arg("check")
359366
.arg("--target-dir")
360367
.arg(".pup");

src/pup_driver.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate rustc_trait_selection;
1818

1919
use anyhow::Result;
2020
use cargo_pup_common::cli::{PupCli, PupCommand};
21+
use cargo_pup_common::workspace::find_workspace_pup_ron;
2122

2223
use rustc_session::{EarlyDiagCtxt, config::ErrorOutputType};
2324
use std::{
@@ -31,6 +32,7 @@ use std::{
3132
use cargo_pup_lint_impl::{ArchitectureLintCollection, ArchitectureLintRunner, Mode};
3233
use cargo_pup_lint_impl::lints::configuration_factory::LintConfigurationFactory;
3334

35+
3436
pub fn main() -> Result<()> {
3537

3638
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
@@ -124,13 +126,22 @@ pub fn main() -> Result<()> {
124126
PupCli::from_env_str(cli_args)
125127
};
126128

127-
let cwd = env::current_dir()?;
129+
let original_dir = if let Ok(original_dir) = env::var("PUP_ORIGINAL_DIR") {
130+
PathBuf::from(original_dir)
131+
} else {
132+
env::current_dir()?
133+
};
134+
128135
let config_path = if let Some(path) = cli_config.config_path {
129-
// Use provided config path
130-
PathBuf::from(path)
136+
// Use provided config path, resolve relative to original directory
137+
if Path::new(&path).is_absolute() {
138+
PathBuf::from(path)
139+
} else {
140+
original_dir.join(path)
141+
}
131142
} else {
132-
// Default to pup.ron in current directory
133-
cwd.join("pup.ron")
143+
// Default to workspace root pup.ron, fallback to local only if not in workspace
144+
find_workspace_pup_ron().unwrap_or_else(|| original_dir.join("pup.ron"))
134145
};
135146

136147
if config_path.exists() {

test_app/Cargo.lock

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)