Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(profile): name the subcommand and repository in workload hints (#1521)

RRFZ92cf60e32561
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0999014b..715e5f6c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,12 @@
is accepted when it matches the file and otherwise says to write it there, instead of being
refused. An entry where no command or flag was detected is reported as a site on that family,
never as a library (#1477, reported by @alfa-reza).
+- `go` and `profile`, given a workload they cannot run for Python or Go, suggest the command with
+ the subcommand that was run and the repository as it was given (`` `rust-anything go app --
+ python3 bench.py` ``), as their unsupported-language errors already name the subcommand. Before,
+ both said `rust-anything profile . -- ...`, which under `go` profiles the current folder and ports
+ nothing. A Node too old for `--cpu-prof` in `NODE_OPTIONS`, given a wrapper such as `npx`, also
+ says `go` cannot reach the node, not `profile` (#1309, #1521 by @Plastic-time).
- C: `putc`, `fputc`, `dprintf`, `vprintf` and `vfprintf` count as printing, as `printf` does,
so `bridge` keeps a function that calls one in C with the reason ``prints (`putc`)``. Before, a
port that dropped the output passed `diff`. A member call such as `ops->putc(c)` is still not a
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 15ce4862..ddad9885 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -81,7 +81,8 @@ paths in what you commit. CI runs it on every file.
what `log` and the MCP `log` tool read back from it.
- `tests/sandbox.rs`: `record --sandbox` and `app record --sandbox` (no network, read-only
source tree); skipped where bubblewrap or sandbox-exec cannot run.
- - `tests/profile.rs`: `profile` on real Python (cProfile) and Node (`--cpu-prof`) workloads.
+ - `tests/profile.rs`: `profile` on real Python (cProfile) and Node (`--cpu-prof`) workloads, and
+ the hints `go` and `profile` give for a workload they cannot run.
- `tests/switch.rs`: `enable` and `disable` on real source files.
- `tests/package.rs`: `package` layouts and an installed napi port.
- `tests/doctor.rs`: `doctor --json` and its exit code.
diff --git a/src/go.rs b/src/go.rs
index 0cfd4432..7519f561 100644
--- a/src/go.rs
+++ b/src/go.rs
@@ -390,7 +390,8 @@ pub fn go(opts: &Options) -> Result<Outcome> {
let lang = opts.lang.unwrap_or_else(|| Lang::detect(&root));
let mut steps = Vec::new();
- let report = crate::profile::profile_for("go", &root, opts.command, Some(lang))?;
+ // The root as given, so the hints `profile_for` prints name it as the person typed it.
+ let report = crate::profile::profile_for("go", opts.root, opts.command, Some(lang))?;
let pick = pick(&report, opts.function)?;
say(
&mut steps,
diff --git a/src/profile.rs b/src/profile.rs
index a80b084c..f5408ba6 100644
--- a/src/profile.rs
+++ b/src/profile.rs
@@ -365,19 +365,20 @@ pub fn profile_for(
command: &[String],
lang: Option<Lang>,
) -> Result<Report> {
+ // The command to suggest, with the repository as the person gave it: copied as printed, it
+ // runs the same subcommand on the same folder.
+ let rerun = format!("rust-anything {caller} {}", crate::app::cli_path(root));
if command.is_empty() {
- bail!(
- "give the workload to run after `--`, e.g. `rust-anything {caller} . -- node bench.mjs`"
- );
+ bail!("give the workload to run after `--`, e.g. `{rerun} -- node bench.mjs`");
}
let root = crate::walk::resolve_workload_root(root, caller)?;
let lang = lang.unwrap_or_else(|| Lang::detect(&root));
let work = work_dir()?;
let no_waits = |(functions, total_ms)| (functions, 0.0, total_ms);
let (functions, unowned_ms, total_ms) = match lang {
- Lang::Ts => node(&root, command, work.path())?,
- Lang::Python => python(&root, command, work.path())?,
- Lang::Go => no_waits(go(&root, command, work.path())?),
+ Lang::Ts => node(&root, command, work.path(), caller)?,
+ Lang::Python => python(&root, command, work.path(), &rerun)?,
+ Lang::Go => no_waits(go(&root, command, work.path(), &rerun)?),
Lang::Java => no_waits(java(&root, command, work.path())?),
Lang::Kotlin => bail!(
"`{caller}` does not run Kotlin workloads yet; use JFR (`jfr print --stack-depth 256`) \
@@ -552,11 +553,12 @@ fn node_launch(
refused: Option<&str>,
dir: &Path,
inherited: &str,
+ caller: &str,
) -> Result<NodeLaunch> {
if let Some(version) = refused {
if !is_node(&command[0]) {
bail!(
- "this Node ({version}) refuses --cpu-prof in NODE_OPTIONS, so `profile` cannot reach \
+ "this Node ({version}) refuses --cpu-prof in NODE_OPTIONS, so `{caller}` cannot reach \
the node that `{}` starts. Run the script with `node` directly, or use a newer Node \
(24 allows it).",
command[0]
@@ -583,7 +585,7 @@ fn node_launch(
})
}
-fn node(root: &Path, command: &[String], work: &Path) -> Result<Waited> {
+fn node(root: &Path, command: &[String], work: &Path, caller: &str) -> Result<Waited> {
let dir = work.join("node");
std::fs::create_dir_all(&dir)?;
let probed = if is_node(&command[0]) {
@@ -593,7 +595,7 @@ fn node(root: &Path, command: &[String], work: &Path) -> Result<Waited> {
};
let refused = node_refusing_cpu_prof_option(probed, root);
let inherited = std::env::var("NODE_OPTIONS").unwrap_or_default();
- let launch = node_launch(command, refused.as_deref(), &dir, &inherited)?;
+ let launch = node_launch(command, refused.as_deref(), &dir, &inherited, caller)?;
let mut run = Command::new(&command[0]);
run.stdout(std::io::stderr())
.args(&launch.args)
@@ -780,15 +782,13 @@ for (file, line, name), (cc, nc, tt, ct, callers) in stats.items():
json.dump(out, open(sys.argv[2], "w"))
"#;
-fn python(root: &Path, command: &[String], work: &Path) -> Result<Waited> {
+fn python(root: &Path, command: &[String], work: &Path, rerun: &str) -> Result<Waited> {
let program = Path::new(&command[0])
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
if !program.starts_with("python") {
- bail!(
- "for Python, give the interpreter and script: `rust-anything profile . -- python3 bench.py`"
- );
+ bail!("for Python, give the interpreter and script: `{rerun} -- python3 bench.py`");
}
let stats = work.join("profile.pstats");
let start = std::time::Instant::now();
@@ -1349,14 +1349,19 @@ fn parse_pprof_raw(root: &Path, raw: &str) -> Vec<(Vec<Option<Key>>, f64)> {
/// Go: `go test` with `-cpuprofile` (Go has no profiler to attach from outside), read with
/// `go tool pprof -raw`.
-fn go(root: &Path, command: &[String], work: &Path) -> Result<(HashMap<Key, Acc>, f64)> {
+fn go(
+ root: &Path,
+ command: &[String],
+ work: &Path,
+ rerun: &str,
+) -> Result<(HashMap<Key, Acc>, f64)> {
let program = Path::new(&command[0])
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
if program != "go" || command.get(1).map(String::as_str) != Some("test") {
bail!(
- "for Go, profile a benchmark or test: `rust-anything profile . -- go test -run XXX -bench . ./pkg` \
+ "for Go, profile a benchmark or test: `{rerun} -- go test -run XXX -bench . ./pkg` \
(Go profiles only from inside the process; `go test` adds -cpuprofile)"
);
}
@@ -2868,7 +2873,7 @@ mod tests {
let dir = crate::app::temp_dir("profile with spaces").unwrap();
let root = dir.canonicalize().unwrap();
let command = ["node".into(), "-e".into(), "console.log('workload')".into()];
- let result = super::node(&root, &command, &root);
+ let result = super::node(&root, &command, &root, "profile");
std::fs::remove_dir_all(dir).unwrap();
assert!(result.unwrap().2 > 0.0);
}
@@ -3441,6 +3446,7 @@ mod tests {
Some("v22.14.0"),
dir,
"",
+ "profile",
)
.unwrap();
assert_eq!(
@@ -3461,16 +3467,26 @@ mod tests {
Some("v20.0.0"),
dir,
"",
+ "profile",
)
.unwrap();
assert_eq!(by_path.args[0], "--cpu-prof");
// A wrapper starts node itself, so only NODE_OPTIONS could reach it: say why it cannot.
- let err =
- node_launch(&cmd(&["npx", "tsx", "app.ts"]), Some("v22.14.0"), dir, "").unwrap_err();
+ // The refusal names the subcommand that was run: `go` shares this with `profile`.
+ let err = node_launch(
+ &cmd(&["npx", "tsx", "app.ts"]),
+ Some("v22.14.0"),
+ dir,
+ "",
+ "go",
+ )
+ .unwrap_err();
let err = err.to_string();
assert!(
- err.contains("v22.14.0") && err.contains("`npx`") && err.contains("`node` directly"),
+ err.contains("v22.14.0")
+ && err.contains("so `go` cannot reach the node that `npx` starts")
+ && err.contains("`node` directly"),
"{err}"
);
@@ -3480,6 +3496,7 @@ mod tests {
None,
dir,
"--max-old-space-size=4096",
+ "profile",
)
.unwrap();
assert_eq!(launch.args, cmd(&["run", "bench"]));
diff --git a/tests/profile.rs b/tests/profile.rs
index c7834594..6890639b 100644
--- a/tests/profile.rs
+++ b/tests/profile.rs
@@ -379,6 +379,49 @@ fn go_names_itself_when_the_language_is_not_profiled() {
assert!(!stderr.contains("`profile` does not run"), "{stderr}");
}
+/// Issue #1309: a workload `go` or `profile` cannot run names the subcommand that was run and the
+/// repository as it was given, so the suggested command works when copied. The check comes before
+/// the workload starts, so neither Python nor Go is needed.
+#[test]
+fn the_workload_hint_names_the_subcommand_and_the_repository() {
+ let tmp = TempDir::new("workload-hint");
+ let py = tmp.path().join("py app");
+ std::fs::create_dir_all(&py).unwrap();
+ std::fs::write(py.join("main.py"), "print(1)\n").unwrap();
+ let go = tmp.path().join("goapp");
+ std::fs::create_dir_all(&go).unwrap();
+ std::fs::write(go.join("go.mod"), "module goapp\n\ngo 1.22\n").unwrap();
+ std::fs::write(go.join("main.go"), "package main\n\nfunc main() {}\n").unwrap();
+
+ let run = |args: &[&str]| {
+ let out = Command::new(env!("CARGO_BIN_EXE_rust-anything"))
+ .current_dir(tmp.path())
+ .args(args)
+ .output()
+ .unwrap();
+ assert!(!out.status.success(), "{args:?} succeeded");
+ String::from_utf8_lossy(&out.stderr).into_owned()
+ };
+ let py_app = rust_anything::app::cli_path("py app");
+ let python = |sub: &str| format!("`rust-anything {sub} {py_app} -- python3 bench.py`");
+ let go_test =
+ |sub: &str| format!("`rust-anything {sub} goapp -- go test -run XXX -bench . ./pkg`");
+ let cases: [(&[&str], String); 5] = [
+ (&["go", "py app", "--", "./run.sh"], python("go")),
+ (&["go", "--root", "py app", "--", "./run.sh"], python("go")),
+ (&["profile", "py app", "--", "./run.sh"], python("profile")),
+ (&["go", "goapp", "--", "./run.sh"], go_test("go")),
+ (&["profile", "goapp", "--", "./run.sh"], go_test("profile")),
+ ];
+ for (args, hint) in cases {
+ let stderr = run(args);
+ assert!(stderr.contains(&hint), "{args:?}: {stderr}");
+ if args[0] == "go" {
+ assert!(!stderr.contains("rust-anything profile"), "{stderr}");
+ }
+ }
+}
+
#[test]
fn profile_lang_php_still_names_the_profiler_to_use() {
let tmp = TempDir::new("profile-lang-php");