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(analyze): one sentence and the fix when a folder lacks the language (#1229)

吴佳浩Albenfc8671404632
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7756ea2c..81777f5d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -74,6 +74,10 @@
collapsed as `--add` collapses it. Before, a wrapped rule was cut at its first line, a `* `
rule was dropped, and `--add` of a rule written with two spaces added it a second time
(#1192, #1225 by @ifaizk5).
+- `analyze --lang` for a language the folder lacks says so once and ends with the fix, such as
+ `drop --lang, or pass --lang ts`, with the `--lang` value of the language it found. Before, it
+ repeated the mixed-folder warning, whose advice was to pass `--lang` (#1206, #1229 by
+ @XiaoBinGan).
- `app front-door` names the folder it read as a library when it finds no exports. It shows how to
record a CLI or server first and asks library users to check the root folder (#1194, #1222 by
@Plastic-time).
diff --git a/src/analyze.rs b/src/analyze.rs
index 6314b8d3..c16f41e5 100644
--- a/src/analyze.rs
+++ b/src/analyze.rs
@@ -410,9 +410,31 @@ pub fn analyze_opts(root: &Path, requested_lang: Option<Lang>) -> Result<Report>
&& let Some(detected) = Lang::detect_optional(root)
&& detected != lang
{
- let reasons: String = report.warnings.iter().map(|w| format!(". {w}")).collect();
+ // The mixed-folder warning says this again, and its advice ("passing `--lang` picks
+ // another") is what the user just did (issue #1206).
+ let mixed = format!("no {} source file was analyzed, but ", lang.name());
+ let reasons: String = report
+ .warnings
+ .iter()
+ .filter(|w| !w.starts_with(&mixed))
+ .map(|w| {
+ let mut chars = w.chars();
+ match chars.next() {
+ Some(first) => format!(". {}{}", first.to_uppercase(), chars.as_str()),
+ None => String::new(),
+ }
+ })
+ .collect();
+ // The value the CLI accepts, not the file extension: `cs` is `csharp`, `ps1` is `powershell`
+ // (review of #1206). The retained warnings come before the fix, so the sentence ends with
+ // what to do.
+ use clap::ValueEnum as _;
+ let hint = detected
+ .to_possible_value()
+ .map(|v| v.get_name().to_string())
+ .unwrap_or_default();
bail!(
- "no {} source files found; the code here looks like {}{reasons}",
+ "no {} source files found; the code here looks like {}{reasons}: drop --lang, or pass --lang {hint}",
lang.name(),
detected.name()
);
diff --git a/tests/analyze.rs b/tests/analyze.rs
index 35796687..a36286c4 100644
--- a/tests/analyze.rs
+++ b/tests/analyze.rs
@@ -378,7 +378,32 @@ fn a_language_the_folder_lacks_is_an_error_not_an_empty_plan() {
let err = analyze_lang(&mini, Lang::Python).unwrap_err().to_string();
assert!(err.contains("no Python source files"), "{err}");
assert!(err.contains("TypeScript/JavaScript"), "{err}");
+ // Issue #1206: one sentence, ending with the fix, and the mixed-folder note not repeated.
+ assert_eq!(err.matches("no Python source").count(), 1, "{err}");
+ assert!(err.ends_with(": drop --lang, or pass --lang ts"), "{err}");
+ // The hint is the value the CLI accepts, not the file extension: C# is `csharp`, not `cs`.
+ let csharp = analyze_lang(&mini, Lang::Csharp).unwrap_err().to_string();
+ assert!(csharp.ends_with("--lang ts"), "{csharp}");
+ let powershell = analyze_lang(&mini, Lang::Powershell)
+ .unwrap_err()
+ .to_string();
+ assert!(powershell.ends_with("--lang ts"), "{powershell}");
+ assert!(!err.contains("passing `--lang` picks another"), "{err}");
assert!(analyze_lang(&mini, Lang::Ts).is_ok());
+ for (fixture, flag) in [
+ ("python", "python"),
+ ("csharp", "csharp"),
+ ("kotlin", "kotlin"),
+ ] {
+ let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("tests/fixtures")
+ .join(fixture);
+ let err = analyze_lang(&dir, Lang::Go).unwrap_err().to_string();
+ assert!(
+ err.ends_with(&format!(": drop --lang, or pass --lang {flag}")),
+ "{err}"
+ );
+ }
// A folder that is empty in its own detected language still gets its (empty) plan.
let empty = std::env::temp_dir().join(format!("ra-lang-empty-{}", std::process::id()));