Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::cli::{BuildArgs, Cli, Commands};
use crate::{ir::BuildGraph, manifest, ninja_gen};
use anyhow::{Context, Result};
use serde_json;
use std::borrow::Cow;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Cow import no longer needed if you simplify to &Path

Drop this import once you replace Cow<Path> with &Path as suggested below.

- use std::borrow::Cow;

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/runner.rs around line 11, the use std::borrow::Cow import is unnecessary
once you replace Cow<Path> with &Path; update function signatures and local
variables that currently use Cow<Path> to accept &Path instead, adjust any call
sites to pass a reference (and replace Cow-specific methods with &Path
equivalents like as_ref()/to_path_buf() or direct &), then remove the `use
std::borrow::Cow;` line; ensure lifetimes and mutable/ownership expectations are
satisfied by borrowing or cloning where required.

use std::fs;
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -114,18 +115,28 @@ fn handle_build(cli: &Cli, args: &BuildArgs) -> Result<()> {
let ninja = generate_ninja(cli)?;
let targets = BuildTargets::new(&args.targets);

// Normalise the build file path and keep the temporary file alive for the
// duration of the Ninja invocation.
let (build_path, _tmp): (PathBuf, Option<NamedTempFile>) = if let Some(path) = &args.emit {
// Normalize the build file path and keep the temporary file alive for the
// duration of the Ninja invocation. Borrow the emitted path when provided
// to avoid unnecessary allocation.
Comment on lines +118 to +120
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Correct the comment: path canonicalization happens in run_ninja, not here

Avoid misleading readers about where normalisation occurs; keep the focus on lifetimes and borrowing.

-    // Normalize the build file path and keep the temporary file alive for the
-    // duration of the Ninja invocation. Borrow the emitted path when provided
-    // to avoid unnecessary allocation.
+    // Keep the temporary build file alive for the duration of the Ninja
+    // invocation. Borrow the emitted path when provided to avoid unnecessary
+    // allocation; run_ninja canonicalizes the path.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Normalize the build file path and keep the temporary file alive for the
// duration of the Ninja invocation. Borrow the emitted path when provided
// to avoid unnecessary allocation.
// Keep the temporary build file alive for the duration of the Ninja
// invocation. Borrow the emitted path when provided to avoid unnecessary
// allocation; run_ninja canonicalizes the path.
🤖 Prompt for AI Agents
In src/runner.rs around lines 118 to 120, the comment incorrectly states that
path canonicalization/normalization happens here; update the comment to remove
that claim and instead describe only keeping the temporary file alive for the
duration of the Ninja invocation and borrowing the emitted path to avoid
allocation, focusing on lifetimes/borrowing rather than normalization location.

let build_path: Cow<Path>;
let mut tmp_file: Option<NamedTempFile> = None;
if let Some(path) = &args.emit {
write_ninja_file(path, &ninja)?;
(path.clone(), None)
build_path = Cow::Borrowed(path.as_path());
} else {
let tmp = create_temp_ninja_file(&ninja)?;
(tmp.path().to_path_buf(), Some(tmp))
};
tmp_file = Some(tmp);
build_path = Cow::Borrowed(
tmp_file
.as_ref()
.expect("temporary Ninja file should exist")
.path(),
);
}
Comment on lines +121 to +135
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Simplify: replace Cow<Path> with &Path; the owned variant is never used

Both branches produce borrowed paths. Remove Cow to reduce indirection and imports while preserving lifetimes.

-    let build_path: Cow<Path>;
+    let build_path: &Path;
     let mut tmp_file: Option<NamedTempFile> = None;
     if let Some(path) = &args.emit {
         write_ninja_file(path, &ninja)?;
-        build_path = Cow::Borrowed(path.as_path());
+        build_path = path.as_path();
     } else {
         let tmp = create_temp_ninja_file(&ninja)?;
         tmp_file = Some(tmp);
-        build_path = Cow::Borrowed(
-            tmp_file
-                .as_ref()
-                .expect("temporary Ninja file should exist")
-                .path(),
-        );
+        build_path = tmp_file
+            .as_ref()
+            .expect("temporary Ninja file should exist")
+            .path();
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let build_path: Cow<Path>;
let mut tmp_file: Option<NamedTempFile> = None;
if let Some(path) = &args.emit {
write_ninja_file(path, &ninja)?;
(path.clone(), None)
build_path = Cow::Borrowed(path.as_path());
} else {
let tmp = create_temp_ninja_file(&ninja)?;
(tmp.path().to_path_buf(), Some(tmp))
};
tmp_file = Some(tmp);
build_path = Cow::Borrowed(
tmp_file
.as_ref()
.expect("temporary Ninja file should exist")
.path(),
);
}
let build_path: &Path;
let mut tmp_file: Option<NamedTempFile> = None;
if let Some(path) = &args.emit {
write_ninja_file(path, &ninja)?;
build_path = path.as_path();
} else {
let tmp = create_temp_ninja_file(&ninja)?;
tmp_file = Some(tmp);
build_path = tmp_file
.as_ref()
.expect("temporary Ninja file should exist")
.path();
}
🤖 Prompt for AI Agents
In src/runner.rs around lines 121 to 135, replace the Cow<Path> usage with a
borrowed &Path since neither branch ever produces an owned Path: change the
declaration to let build_path: &Path; keep tmp_file: Option<NamedTempFile>
declared before so its lifetime spans the borrow; in the if branch assign
build_path = path.as_path(); in the else branch assign build_path =
tmp_file.as_ref().expect("temporary Ninja file should exist").path(); and remove
the now-unused Cow import.


let program = resolve_ninja_program();
run_ninja(program.as_path(), cli, &build_path, &targets)?;
run_ninja(program.as_path(), cli, build_path.as_ref(), &targets)?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Pass build_path directly after the simplification

If you adopt the &Path refactor above, remove the .as_ref() call here.

-    run_ninja(program.as_path(), cli, build_path.as_ref(), &targets)?;
+    run_ninja(program.as_path(), cli, build_path, &targets)?;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
run_ninja(program.as_path(), cli, build_path.as_ref(), &targets)?;
run_ninja(program.as_path(), cli, build_path, &targets)?;
🤖 Prompt for AI Agents
In src/runner.rs around line 138, the call currently uses build_path.as_ref();
remove the unnecessary .as_ref() and pass build_path directly (or as a borrowed
reference if needed, e.g. &build_path) so the argument matches the simplified
&Path type expected by run_ninja.

drop(tmp_file);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Remove redundant explicit drop

tmp_file drops at end-of-scope immediately after this line anyway. Avoid noise.

-    drop(tmp_file);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
drop(tmp_file);
🤖 Prompt for AI Agents
In src/runner.rs around line 139, the explicit call drop(tmp_file); is redundant
because tmp_file will be dropped automatically at the end of the scope; remove
that line to avoid noise and rely on Rust's RAII for cleanup (ensure no code
depends on immediate drop timing before deleting).

Ok(())
}

Expand Down
Loading