Skip to content

Don't continue translations checking when error parsing #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# CHANGELOG

## Unreleased - [0.2.15]
## 2025-06-14 - [0.2.15]

### Enhancements

- Hygienize re-exports of third party dependencies.

### Bug fixes

- Fix translations checker detecting incorrect missing translations
when a file has a syntax error (regression from v0.2.13).

## 2025-06-11 - [0.2.14]

### Deprecations
Expand Down Expand Up @@ -852,7 +857,7 @@ version to `0.1` during installation.

- Added all ISO-639-1 and ISO-639-2 languages.

[0.2.15]: https://github.com/mondeja/leptos-fluent/compare/v0.2.14...master
[0.2.15]: https://github.com/mondeja/leptos-fluent/compare/v0.2.14...v0.2.15
[0.2.14]: https://github.com/mondeja/leptos-fluent/compare/v0.2.13...v0.2.14
[0.2.13]: https://github.com/mondeja/leptos-fluent/compare/v0.2.12...v0.2.13
[0.2.12]: https://github.com/mondeja/leptos-fluent/compare/v0.2.11...v0.2.12
Expand Down
52 changes: 27 additions & 25 deletions leptos-fluent-macros/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,7 @@ impl Parse for I18nLoader {
}

if let Some(ref check_translations_enum) = check_translations {
let tr_macros = match check_translations_enum {
let maybe_tr_macros = match check_translations_enum {
LitBoolOrStr::Str(litstr) => {
gather_tr_macro_defs_from_globstr(
manifest_path.join(litstr.value()),
Expand All @@ -1971,36 +1971,38 @@ impl Parse for I18nLoader {
&mut errors,
)
} else {
Vec::new()
Ok(Vec::new())
}
}
};
let check_messages = crate::translations_checker::run(
&fluent_entries,
&tr_macros,
);
if let Ok(tr_macros) = maybe_tr_macros {
let check_messages = crate::translations_checker::run(
&fluent_entries,
&tr_macros,
);

let mut report = String::new();
if !check_messages.is_empty() {
report.push_str(&format!(
"Translations check failed:\n- {}",
check_messages.join("\n- "),
));
let mut report = String::new();
if !check_messages.is_empty() {
report.push_str(&format!(
"Translations check failed:\n- {}",
check_messages.join("\n- "),
));
if !errors.is_empty() {
report.push_str("\n\n");
}
}
if !errors.is_empty() {
report.push_str("\n\n");
report.push_str(&format!(
"Unrecoverable errors:\n- {}",
errors.join("\n- "),
));
}
if !report.is_empty() {
return Err(syn::Error::new(
check_translations_enum.span(),
report,
));
}
}
if !errors.is_empty() {
report.push_str(&format!(
"Unrecoverable errors:\n- {}",
errors.join("\n- "),
));
}
if !report.is_empty() {
return Err(syn::Error::new(
check_translations_enum.span(),
report,
));
}
}
}
Expand Down
26 changes: 18 additions & 8 deletions leptos-fluent-macros/src/tr_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) fn gather_tr_macro_defs_from_globstr(
globstr: impl AsRef<Path>,
errors: &mut Vec<String>,
#[cfg(not(test))] workspace_path: impl AsRef<Path>,
) -> Vec<TranslationMacro> {
) -> Result<Vec<TranslationMacro>, ()> {
let mut tr_macros = Vec::new();

let globstr = globstr.as_ref();
Expand All @@ -18,7 +18,7 @@ pub(crate) fn gather_tr_macro_defs_from_globstr(
&globstr.to_string_lossy(),
#[cfg(not(test))]
&workspace_path.as_ref().to_string_lossy(),
);
)?;
} else {
let glob_pattern = globstr.to_string_lossy();
match globwalk::glob(&glob_pattern) {
Expand All @@ -33,7 +33,7 @@ pub(crate) fn gather_tr_macro_defs_from_globstr(
&path.to_string_lossy(),
#[cfg(not(test))]
&workspace_path.as_ref().to_string_lossy(),
);
)?;
}
Err(error) => {
errors.push(format!("Error reading file: {error}"));
Expand All @@ -49,13 +49,13 @@ pub(crate) fn gather_tr_macro_defs_from_globstr(
}
}

tr_macros
Ok(tr_macros)
}

pub(crate) fn gather_tr_macro_defs_from_workspace(
manifest_path: impl AsRef<Path>,
errors: &mut Vec<String>,
) -> Vec<TranslationMacro> {
) -> Result<Vec<TranslationMacro>, ()> {
let abs_manifest_path =
std::path::absolute(manifest_path.as_ref()).unwrap();
let abs_manifest_path_clone = abs_manifest_path.clone();
Expand Down Expand Up @@ -91,7 +91,7 @@ pub(crate) fn gather_tr_macro_defs_from_workspace(
&path.to_string_lossy(),
#[cfg(not(test))]
&workspace_path.display().to_string(),
);
)?;
}
Err(error) => {
errors.push(format!("Error reading file: {error}"));
Expand All @@ -107,7 +107,7 @@ pub(crate) fn gather_tr_macro_defs_from_workspace(
};
}

tr_macros
Ok(tr_macros)
}

/// Get *./target* directory for the workspace.
Expand Down Expand Up @@ -136,12 +136,19 @@ fn find_workspace_root(mut dir: PathBuf) -> Option<PathBuf> {
None
}

/// Parse `tr!` and `move_tr!` macro definitions from the path to a file.
///
/// Collects macros and errors into the provided vectors as mutable references.
///
/// Can `Result` in an error unit type if the file can not be parsed. In that case,
/// we don't need to raise an error, just ignore the file and stop the checking,
/// because the Rust compiler will raise an error if the file has a syntax error.
fn tr_macros_from_file_path(
tr_macros: &mut Vec<TranslationMacro>,
errors: &mut Vec<String>,
file_path: &str,
#[cfg(not(test))] workspace_path: &str,
) {
) -> Result<(), ()> {
if let Ok(file_content) = std::fs::read_to_string(file_path) {
match syn::parse_file(&file_content) {
Ok(ast) => {
Expand All @@ -154,14 +161,17 @@ fn tr_macros_from_file_path(
workspace_path,
);
visitor.visit_file(&ast);
Ok(())
}
Err(_) => {
// This error is a syntax error. Let the Rust compiler handle it.
// See https://github.com/mondeja/leptos-fluent/issues/330
Err(())
}
}
} else {
errors.push(format!("Error reading file: {file_path}"));
Ok(())
}
}

Expand Down
8 changes: 7 additions & 1 deletion leptos-fluent-macros/src/translations_filler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ pub(crate) fn run(
errors: &mut Vec<String>,
) -> Vec<(String, Vec<String>)> {
let ws_path = manifest_path.as_ref();
let tr_macros = gather_tr_macro_defs_from_globstr(
let maybe_tr_macros = gather_tr_macro_defs_from_globstr(
ws_path.join(globstr),
errors,
#[cfg(not(test))]
ws_path,
);
if maybe_tr_macros.is_err() {
// If we can't gather the macros, we just return an empty vector.
// The Rust compiler will raise an error if the file has a syntax error.
return Vec::new();
}
let tr_macros = maybe_tr_macros.unwrap();

let mut missing_message_names_by_lang: HashMap<Rc<String>, Vec<String>> =
HashMap::new();
Expand Down
Loading