Skip to content

Commit

Permalink
Filter to the host platform when running the default command (#313)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Pinkus <pinkus@amazon.com>
  • Loading branch information
alex-pinkus and Alex Pinkus authored Oct 2, 2024
1 parent 9f27bc1 commit b689701
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use watchexec::{error::Result, run::watch};
mod args;
mod options;
mod root;
mod rustc;
mod watch;

fn main() -> Result<()> {
Expand Down
36 changes: 28 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ use watchexec::{
Shell,
};

pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) {
/// Sets all the requested commands on the passed-in `builder`.
///
/// If no commands are provided, this defaults to `cargo check`, and returns `true` to indicate
/// that it used the default.
pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) -> bool {
let mut commands: Vec<String> = Vec::new();

// --features are injected just after applicable cargo subcommands
Expand Down Expand Up @@ -114,17 +118,23 @@ pub fn set_commands(builder: &mut ConfigBuilder, matches: &ArgMatches) {
}

// Default to `cargo check`
if commands.is_empty() {
let default_command = if commands.is_empty() {
let mut cmd: String = "cargo check".into();
if let Some(features) = features.as_ref() {
cmd.push_str(" --features ");
cmd.push_str(features);
}
commands.push(cmd);
}

true
} else {
false
};

debug!("Commands: {:?}", commands);
builder.cmd(commands);

default_command
}

pub fn set_ignores(builder: &mut ConfigBuilder, matches: &ArgMatches) {
Expand Down Expand Up @@ -192,8 +202,13 @@ pub fn set_debounce(builder: &mut ConfigBuilder, matches: &ArgMatches) {
}
}

fn find_local_deps() -> Result<Vec<PathBuf>, String> {
fn find_local_deps(filter_platform: Option<String>) -> Result<Vec<PathBuf>, String> {
let options: Vec<String> = filter_platform
.map(|platform| format!("--filter-platform={}", platform))
.into_iter()
.collect();
let metadata = MetadataCommand::new()
.other_options(options)
.exec()
.map_err(|e| format!("Failed to execute `cargo metadata`: {}", e))?;

Expand Down Expand Up @@ -250,7 +265,7 @@ fn find_local_deps() -> Result<Vec<PathBuf>, String> {
Ok(local_deps.into_iter().collect::<Vec<PathBuf>>())
}

pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches) {
pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches, only_default_command: bool) {
let mut watches = Vec::new();
if matches.is_present("watch") {
for watch in values_t!(matches, "watch", String).unwrap_or_else(|e| e.exit()) {
Expand All @@ -260,7 +275,12 @@ pub fn set_watches(builder: &mut ConfigBuilder, matches: &ArgMatches) {

if watches.is_empty() {
if !matches.is_present("skip-local-deps") {
match find_local_deps() {
let filter_platform = if only_default_command {
Some(crate::rustc::host_triple())
} else {
None
};
match find_local_deps(filter_platform) {
Ok(dirs) => {
if dirs.is_empty() {
debug!("Found no local deps");
Expand Down Expand Up @@ -322,8 +342,8 @@ pub fn get_options(matches: &ArgMatches) -> Config {

set_ignores(&mut builder, matches);
set_debounce(&mut builder, matches);
set_watches(&mut builder, matches);
set_commands(&mut builder, matches);
let only_default_command = set_commands(&mut builder, matches);
set_watches(&mut builder, matches, only_default_command);

let mut args = builder.build().unwrap();
args.once = matches.is_present("once");
Expand Down
21 changes: 21 additions & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use clap::{Error, ErrorKind};
use std::{io::BufRead, process::Command};

/// Queries `rustc` to identify the host platform's target triple.
pub fn host_triple() -> String {
Command::new("rustc")
.arg("-vV")
.output()
.map_err(|err| err.to_string())
.and_then(|out| {
// Look for a line starting with `host: `, just like `cargo` does, to identify the
// host platform -- see:
// https://github.com/rust-lang/cargo/blob/631b8774e512a69402a55367b4eb9c195220e404/src/cargo/util/rustc.rs#L68
out.stdout
.lines()
.map_while(Result::ok)
.find_map(|line| line.strip_prefix("host: ").map(ToString::to_string))
.ok_or_else(|| "`rustc -vV` didn't have a line for `host`.".to_string())
})
.unwrap_or_else(|err| Error::with_description(&err, ErrorKind::Io).exit())
}

0 comments on commit b689701

Please sign in to comment.