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(map): say why instead of counting 0 of 0 units (#1524)

RRFZd0c4bb7190d5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4017ef20..0999014b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,6 +47,13 @@
(`` `rust-anything diff <crate>` shows them ``), and `gate`'s fix for a failed `ported` check
names the crate's `src/port.rs` and `rust-anything gate <crate>`. Before, both left the crate
out, so the command failed when run from the repository root (#1485, #1487 by @badrisatyam1-ctrl).
+- `map` no longer counts "0 of 0 units have a Rust port.", which reads like a finished, empty port.
+ In a folder with no source file of a supported language and nothing else to list, it says
+ ``Nothing to map under `<repo>`: no source files of a supported language (...)`` and leaves out
+ the `map --write` hint; `map --json` carries `analyze`'s warning as `no_source`. When `analyze`
+ finds source but no unit (every file ignored, compiled files only), it says so and points at
+ `rust-anything analyze <repo>` for why, keeping the rest of the map (#1444, #1524 by
+ @Plastic-time).
- Python bridge: a function named `error`, `port`, `native_module` or `unpickled_error`, or two
named `area` and `area_inner`, gives a PyO3 crate that builds. Each `#[pyfunction]` wrapper and
method is written as `<name>_py` in Rust, so it no longer clashes with the crate's own items;
diff --git a/src/analyze.rs b/src/analyze.rs
index 081d3513..ea2f9837 100644
--- a/src/analyze.rs
+++ b/src/analyze.rs
@@ -135,6 +135,21 @@ pub struct Report {
pub warnings: Vec<String>,
}
+/// How the warning for a folder with no source of a supported language starts; the folder follows.
+pub const NO_SOURCE: &str = "no source files of a supported language \
+ (TS/JS, Python, C, Go, Ruby, Java, Kotlin, PHP, C#, PowerShell, Swift)";
+
+impl Report {
+ /// The warning that the folder has no source file of a supported language, when `analyze`
+ /// gave it: the plan and the port map say so instead of describing an empty project.
+ pub fn no_source_warning(&self) -> Option<&str> {
+ self.warnings
+ .iter()
+ .map(String::as_str)
+ .find(|w| w.starts_with(NO_SOURCE))
+ }
+}
+
#[derive(Debug, Default, Serialize)]
pub struct Summary {
pub packages: usize,
@@ -907,10 +922,7 @@ fn build(
&& requested_lang.is_none()
&& Lang::detect_optional(root).is_none()
{
- warnings.push(format!(
- "no source files of a supported language (TS/JS, Python, C, Go, Ruby, Java, Kotlin, PHP, C#, PowerShell, Swift) under {}",
- root.display()
- ));
+ warnings.push(format!("{NO_SOURCE} under {}", root.display()));
}
}
Report {
diff --git a/src/port_map.rs b/src/port_map.rs
index 55a54b62..b653ad80 100644
--- a/src/port_map.rs
+++ b/src/port_map.rs
@@ -30,6 +30,9 @@ pub struct PortMap {
pub covered_by_broken: usize,
#[serde(skip_serializing_if = "Option::is_none")]
pub analyze_error: Option<String>,
+ /// `analyze`'s warning when the folder has no source file of a supported language.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub no_source: Option<String>,
}
fn is_zero(n: &usize) -> bool {
@@ -143,6 +146,18 @@ impl PortMap {
.filter(|p| !p.recording_problems.is_empty())
.count()
}
+
+ /// `analyze` found no source of a supported language, and nothing else would go in the map:
+ /// no port, no unit, no whole-app glue and no crate folder that could not be read.
+ fn nothing_to_map(&self) -> bool {
+ self.no_source.is_some()
+ && self.ports.is_empty()
+ && self.not_ported.is_empty()
+ && self.covered_by_broken == 0
+ && self.glue.is_empty()
+ && self.crate_errors.is_empty()
+ && self.analyze_error.is_none()
+ }
}
pub fn map(root: &Path, extra: &[PathBuf]) -> Result<PortMap> {
@@ -221,8 +236,10 @@ pub fn map(root: &Path, extra: &[PathBuf]) -> Result<PortMap> {
}
ports.sort_by(|a, b| a.unit.cmp(&b.unit).then(a.crate_dir.cmp(&b.crate_dir)));
+ let mut no_source = None;
let (not_ported, broken_units, analyze_error) = match crate::analyze::analyze(&root) {
Ok(report) => {
+ no_source = report.no_source_warning().map(str::to_string);
let order = |id: &str| {
report
.start_here
@@ -273,6 +290,7 @@ pub fn map(root: &Path, extra: &[PathBuf]) -> Result<PortMap> {
crate_errors,
covered_by_broken: broken_units,
analyze_error,
+ no_source,
})
}
@@ -390,36 +408,55 @@ fn render(out: &mut String, map: &PortMap, repo: &Path, written: bool) -> std::f
|n| n.to_string_lossy().into(),
);
writeln!(out, "# Port map: {name}\n")?;
+ // "0 of 0 units have a Rust port" reads like a finished, empty port, where the path is most
+ // likely wrong or `analyze` skipped every file (ignore rules, compiled files only).
+ let cli_repo = crate::app::cli_path(repo);
+ if map.nothing_to_map() {
+ // No offer to write the map either: there is nothing to put in it.
+ return writeln!(
+ out,
+ "Nothing to map under `{cli_repo}`: {}.",
+ crate::analyze::NO_SOURCE
+ );
+ }
let ported = map.ported_units();
let total = ported + map.not_ported.len() + map.covered_by_broken;
- write!(
- out,
- "{} of {} {} a Rust port",
- ported,
- if map.analyze_error.is_some() {
- "the".to_string()
- } else {
- total.to_string()
- },
- // One unit reads "1 of 1 unit has", not "units have".
- if total == 1 && map.analyze_error.is_none() {
- "unit has"
- } else {
- "units have"
- }
- )?;
- match map.changed() {
- _ if map.ports.is_empty() => writeln!(out, ".\n")?,
- 0 if !map.ports.iter().any(|p| p.recorded) => writeln!(out, "; none recorded yet.\n")?,
- 0 => writeln!(out, "; no original changed since it was recorded.\n")?,
- 1 => writeln!(
+ if total == 0 && map.analyze_error.is_none() {
+ writeln!(
out,
- "; **1 changed upstream** since it was recorded: re-record it and run `diff`.\n"
- )?,
- n => writeln!(
+ "`analyze` found no unit to port under `{cli_repo}`, and no unit has a Rust port: \
+ `rust-anything analyze {cli_repo}` says why.\n"
+ )?;
+ } else {
+ write!(
out,
- "; **{n} changed upstream** since they were recorded: re-record them and run `diff`.\n"
- )?,
+ "{} of {} {} a Rust port",
+ ported,
+ if map.analyze_error.is_some() {
+ "the".to_string()
+ } else {
+ total.to_string()
+ },
+ // One unit reads "1 of 1 unit has", not "units have".
+ if total == 1 && map.analyze_error.is_none() {
+ "unit has"
+ } else {
+ "units have"
+ }
+ )?;
+ match map.changed() {
+ _ if map.ports.is_empty() => writeln!(out, ".\n")?,
+ 0 if !map.ports.iter().any(|p| p.recorded) => writeln!(out, "; none recorded yet.\n")?,
+ 0 => writeln!(out, "; no original changed since it was recorded.\n")?,
+ 1 => writeln!(
+ out,
+ "; **1 changed upstream** since it was recorded: re-record it and run `diff`.\n"
+ )?,
+ n => writeln!(
+ out,
+ "; **{n} changed upstream** since they were recorded: re-record them and run `diff`.\n"
+ )?,
+ }
}
if map.untrusted() > 0 {
@@ -629,6 +666,7 @@ mod tests {
crate_errors: Vec::new(),
covered_by_broken: 0,
analyze_error: None,
+ no_source: None,
}
}
@@ -731,14 +769,155 @@ mod tests {
#[test]
fn an_empty_map_says_no_unit_was_found() {
+ // Issue #1444: no "0 of 0" count; `analyze` is the one that says why.
let text = markdown(&empty_map());
+ assert!(
+ text.contains(
+ "`analyze` found no unit to port under `.`, and no unit has a Rust port: \
+ `rust-anything analyze .` says why."
+ ),
+ "{text}"
+ );
assert!(text.contains("No unit to port was found."), "{text}");
+ assert!(!text.contains("0 of 0"), "{text}");
+ assert!(!text.contains("nothing to map"), "{text}");
assert!(
!text.contains("Every unit `analyze` found has a port."),
"{text}"
);
}
+ fn no_source_map() -> PortMap {
+ PortMap {
+ no_source: Some(format!("{} under x", crate::analyze::NO_SOURCE)),
+ ..empty_map()
+ }
+ }
+
+ fn unit(id: &str) -> Unported {
+ Unported {
+ unit: id.into(),
+ loc: 19,
+ readiness: 94,
+ exports: 3,
+ status: crate::analyze::Status::Ready,
+ reasons: Vec::new(),
+ }
+ }
+
+ #[test]
+ fn a_folder_with_no_source_says_there_is_nothing_to_map() {
+ let dir = std::env::temp_dir().join(format!("ra-map-no-source-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::write(dir.join("README.md"), "# docs only\n").unwrap();
+ let map = super::map(&dir, &[]).unwrap();
+ let text = markdown_with(&map, Path::new("docs"), false);
+ std::fs::remove_dir_all(&dir).unwrap();
+ assert!(
+ map.no_source
+ .as_deref()
+ .is_some_and(|w| w.starts_with(crate::analyze::NO_SOURCE)),
+ "{:?}",
+ map.no_source
+ );
+ assert!(
+ text.contains(&format!(
+ "Nothing to map under `docs`: {}.",
+ crate::analyze::NO_SOURCE
+ )),
+ "{text}"
+ );
+ assert!(!text.contains("0 of 0"), "{text}");
+ assert!(!text.contains("--write"), "{text}");
+ assert!(!text.contains("## Ports"), "{text}");
+ }
+
+ #[test]
+ fn a_folder_whose_source_is_all_ignored_is_not_called_empty() {
+ // `analyze` warns that ignore rules hid the source, not that there is none: the map must
+ // not say there is nothing to map.
+ let dir = std::env::temp_dir().join(format!("ra-map-ignored-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(dir.join("src")).unwrap();
+ std::fs::write(dir.join(".ignore"), "src/\n").unwrap();
+ std::fs::write(dir.join("src/a.ts"), "export const a = 1;\n").unwrap();
+ let map = super::map(&dir, &[]).unwrap();
+ let text = markdown_with(&map, Path::new("app"), false);
+ std::fs::remove_dir_all(&dir).unwrap();
+ assert!(map.no_source.is_none(), "{:?}", map.no_source);
+ assert!(!text.contains("nothing to map"), "{text}");
+ assert!(
+ text.contains("`rust-anything analyze app` says why"),
+ "{text}"
+ );
+ assert!(!text.contains("0 of 0"), "{text}");
+ }
+
+ #[test]
+ fn whatever_else_is_in_the_map_keeps_it_from_reading_as_empty() {
+ // Each of these makes a map with no source still worth reading.
+ let glue = PortMap {
+ glue: vec![Glue {
+ file: "rust/app/src/units/foo.rs".into(),
+ falls_back: true,
+ }],
+ ..no_source_map()
+ };
+ let broken = PortMap {
+ crate_errors: vec!["rust/x: rust-anything.json does not parse".into()],
+ ..no_source_map()
+ };
+ let covered = PortMap {
+ covered_by_broken: 1,
+ ..no_source_map()
+ };
+ let failed = PortMap {
+ analyze_error: Some("boom".into()),
+ ..no_source_map()
+ };
+ let unported = PortMap {
+ not_ported: vec![unit("src/util.ts")],
+ ..no_source_map()
+ };
+ // A port whose original is gone: its crate still says what it ports.
+ let ported = PortMap {
+ ports: vec![Port {
+ unit: "src/util.ts".into(),
+ crate_dir: "rust/util-rs".into(),
+ language: "ts".into(),
+ language_name: "TypeScript/JavaScript".into(),
+ kind: "napi".into(),
+ port: String::new(),
+ enabled: false,
+ in_rust: Vec::new(),
+ kept_in_original: Vec::new(),
+ cases: 0,
+ recorded: false,
+ last_diff: None,
+ last_diff_at: None,
+ last_gate: None,
+ changed_upstream: None,
+ recording_problems: Vec::new(),
+ last_change: None,
+ }],
+ ..no_source_map()
+ };
+ for (map, expect) in [
+ (ported, "1 of 1 unit has a Rust port"),
+ (glue, "rust/app/src/units/foo.rs"),
+ (broken, "## Crate folders that could not be read"),
+ (covered, "0 of 1 unit has a Rust port."),
+ (failed, "`analyze` could not list the units: boom"),
+ (unported, "0 of 1 unit has a Rust port."),
+ ] {
+ let text = markdown(&map);
+ assert!(text.contains(expect), "{expect}: {text}");
+ assert!(!text.contains("Nothing to map"), "{expect}: {text}");
+ }
+ assert!(markdown(&no_source_map()).contains("Nothing to map under `.`"));
+ }
+
#[test]
fn footer_names_passed_root_and_omits_commit_claim_on_stdout() {
let map = empty_map();
diff --git a/src/report.rs b/src/report.rs
index a84074c4..82b7fddd 100644
--- a/src/report.rs
+++ b/src/report.rs
@@ -90,10 +90,7 @@ fn whole_app(out: &mut String, w: &crate::whole_app::WholeApp) -> std::fmt::Resu
fn render(out: &mut String, r: &Report, title: &str) -> std::fmt::Result {
let s = &r.summary;
- let names_language = !r
- .warnings
- .iter()
- .any(|w| w.starts_with("no source files of a supported language"));
+ let names_language = r.no_source_warning().is_none();
if names_language {
writeln!(
out,