Repository: slint-ui/slint (language: rust)

Issue:
More helpful error message when file has a BOM at start of file
### Feature Description

We ran into the same issue as #3581. The error message here could be a lot more helpful. Or optionally the parser could be adjusted to accept BOM encoded files.

### Product Impact

_No response_


Discussion / hints:
Thanks.
I think we should silently ignore the BOM. That's what rustc and cargo do.

Fix patch:
diff --git a/internal/compiler/lexer.rs b/internal/compiler/lexer.rs
index 9887da03296..ae9231ce930 100644
--- a/internal/compiler/lexer.rs
+++ b/internal/compiler/lexer.rs
@@ -198,6 +198,17 @@ pub fn lex(mut source: &str) -> Vec<crate::parser::Token> {
     let mut result = vec![];
     let mut offset = 0;
     let mut state = LexState::default();
+    if source.starts_with("\u{FEFF}") {
+        // Skip BOM
+        result.push(crate::parser::Token {
+            kind: SyntaxKind::Whitespace,
+            text: source[..3].into(),
+            offset: 0,
+            ..Default::default()
+        });
+        source = &source[3..];
+        offset += 3;
+    }
     while !source.is_empty() {
         if let Some((len, kind)) = crate::parser::lex_next_token(source, &mut state) {
             result.push(crate::parser::Token {


Test patch:
diff --git a/internal/compiler/tests/syntax/basic/bom-simple.slint b/internal/compiler/tests/syntax/basic/bom-simple.slint
new file mode 100644
index 00000000000..f0f12220b8b
--- /dev/null
+++ b/internal/compiler/tests/syntax/basic/bom-simple.slint
@@ -0,0 +1,12 @@
+﻿// Copyright © SixtyFPS GmbH <info@slint.dev>
+// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
+
+// This file contains an UTF-8 BOM at the beginning of the file.
+
+  component Foo {  }
+//^warning{Component is neither used nor exported}
+
+
+export component XX {
+
+}
\ No newline at end of file
diff --git a/internal/compiler/tests/syntax_tests.rs b/internal/compiler/tests/syntax_tests.rs
index 95cb5260376..130657f1463 100644
--- a/internal/compiler/tests/syntax_tests.rs
+++ b/internal/compiler/tests/syntax_tests.rs
@@ -69,6 +69,12 @@ fn syntax_tests() -> std::io::Result<()> {
 
 fn process_file(path: &std::path::Path) -> std::io::Result<bool> {
     let source = std::fs::read_to_string(path)?;
+    if path.to_str().unwrap_or("").contains("bom-") && !source.starts_with("\u{FEFF}") {
+        // make sure that the file still contains BOM and it wasn't remove by some tools
+        return Err(std::io::Error::other(format!(
+            "{path:?} does not contains BOM while it should"
+        )));
+    }
     std::panic::catch_unwind(|| process_file_source(path, source, false)).unwrap_or_else(|err| {
         println!("Panic while processing {}: {:?}", path.display(), err);
         Ok(false)