Skip to content

Commit 1127b78

Browse files
committed
fix(go-runner): ignore corrupted or template go files which can't be parsed
1 parent ca3050a commit 1127b78

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

go-runner/src/builder/patcher.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,22 @@ pub fn patch_imports<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
4646
let content =
4747
fs::read_to_string(&go_file).context(format!("Failed to read Go file: {go_file:?}"))?;
4848

49-
let patched_content = patch_imports_for_source(&content)?;
50-
if patched_content != content {
51-
fs::write(&go_file, patched_content)
52-
.context(format!("Failed to write patched Go file: {go_file:?}"))?;
53-
54-
debug!("Patched imports in: {go_file:?}");
55-
patched_files += 1;
49+
// Skip files that can't be parsed as Go code (e.g., template files like template.go)
50+
// This gracefully handles template files mixed in with the source code
51+
match patch_imports_for_source(&content) {
52+
Ok(patched_content) => {
53+
if patched_content != content {
54+
fs::write(&go_file, patched_content)
55+
.context(format!("Failed to write patched Go file: {go_file:?}"))?;
56+
57+
debug!("Patched imports in: {go_file:?}");
58+
patched_files += 1;
59+
}
60+
}
61+
Err(e) => {
62+
debug!("Skipping file that cannot be parsed as Go code: {go_file:?} (error: {e})");
63+
continue;
64+
}
5665
}
5766
}
5867
debug!("Patched {patched_files} files");
@@ -64,7 +73,12 @@ pub fn patch_imports<P: AsRef<Path>>(folder: P) -> anyhow::Result<()> {
6473
pub fn patch_imports_for_source(source: &str) -> anyhow::Result<String> {
6574
let replace_import =
6675
|mut source: String, import_path: &str, replacement: &str| -> anyhow::Result<String> {
67-
let parsed = gosyn::parse_source(&source)?;
76+
// If we can't parse the source, skip this replacement
77+
// This can happen with template files or malformed Go code
78+
let parsed = match gosyn::parse_source(&source) {
79+
Ok(p) => p,
80+
Err(_) => return Ok(source),
81+
};
6882

6983
if let Some(import) = parsed
7084
.imports

0 commit comments

Comments
 (0)