Phase 23: Package System — Multi-File FTL mit Imports - #44
Conversation
Add import statement support to FTL, enabling multi-file programs: - Grammar: `import "path/to/module.ftl"` before node definitions - AST: `Program.imports: Vec<String>` (empty for existing programs) - Parser: parses import_stmt rules, strips quotes from paths - Pipeline: `resolve_imports()` recursively resolves imports with: - Circular import detection - Duplicate node ID detection across modules - Relative path resolution from importing file - Pipeline: `run_check_program()` / `run_check_program_with_bmc()` accept pre-parsed Programs (for use after import resolution) - CLI: cmd_check and cmd_build resolve imports for file-based input - MCP: flux_check accepts optional `base_path` for import resolution - Tests: 6 new tests covering success, circular, missing, duplicate, no-imports, and parse-syntax cases Backward compatible: programs without imports work exactly as before. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! Dieser Pull Request führt ein Paket-System für FTL ein, das die Entwicklung von Multi-Datei-Programmen durch einen Import-Mechanismus ermöglicht. Dies verbessert die Modularität und Wiederverwendbarkeit von FTL-Code erheblich, indem es Entwicklern erlaubt, ihre Programme in separate Module zu organisieren und diese über Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a multi-file program import mechanism for FTL. It adds an imports field to the Program AST, updates the grammar to recognize import statements, and implements recursive import resolution in the pipeline module, including checks for circular imports, missing files, and duplicate node IDs. The flux-mcp and flux CLI tools are updated to support a base_path for import resolution, and various test helpers are adjusted to initialize the new imports field. Review comments suggest refactoring the import handling logic in flux-mcp.rs to improve readability and reduce complexity, and refactoring the check_duplicate_ids function in pipeline.rs to use iterators for better maintainability and conciseness.
| let result = if let Some(bp) = base_path { | ||
| let parse_result = flux_ftl::parser::parse_ftl(ftl_source); | ||
| match parse_result.status { | ||
| flux_ftl::error::Status::Ok => { | ||
| if let Some(ast) = parse_result.ast { | ||
| if !ast.imports.is_empty() { | ||
| let path = std::path::Path::new(bp); | ||
| match pipeline::resolve_imports(&ast, path) { | ||
| Ok(merged) => pipeline::run_check_program_with_bmc(merged, bmc_config), | ||
| Err(errs) => { | ||
| send_tool_error(stdout, id, &format!("Import resolution failed: {}", errs.join("; "))); | ||
| return; | ||
| } | ||
| } | ||
| } else { | ||
| pipeline::run_check_program_with_bmc(ast, bmc_config) | ||
| } | ||
| } else { | ||
| pipeline::run_check_with_bmc(ftl_source, bmc_config) | ||
| } | ||
| } | ||
| flux_ftl::error::Status::Error => { | ||
| pipeline::run_check_with_bmc(ftl_source, bmc_config) | ||
| } | ||
| } | ||
| } else { | ||
| pipeline::run_check_with_bmc(ftl_source, bmc_config) | ||
| }; |
There was a problem hiding this comment.
Die Logik zur Behandlung von Importen ist recht verschachtelt und enthält mehrfache Aufrufe von pipeline::run_check_with_bmc. Dies kann die Lesbarkeit und Wartbarkeit erschweren.
Ich schlage eine Refaktorisierung vor, um die Logik zu vereinfachen. Zuerst wird versucht, das Programm durch Parsen und Auflösen von Importen zu erhalten. Wenn dies fehlschlägt oder nicht anwendbar ist, wird auf die Überprüfung des rohen Quellcodes zurückgegriffen. Dies reduziert die Komplexität und Codeduplizierung.
let result = if let Some(bp) = base_path {
let parse_result = flux_ftl::parser::parse_ftl(ftl_source);
if let Some(ast) = parse_result.ast {
if ast.imports.is_empty() {
pipeline::run_check_program_with_bmc(ast, bmc_config)
} else {
let path = std::path::Path::new(bp);
match pipeline::resolve_imports(&ast, path) {
Ok(merged) => pipeline::run_check_program_with_bmc(merged, bmc_config),
Err(errs) => {
send_tool_error(stdout, id, &format!("Import resolution failed: {}", errs.join("; ")));
return;
}
}
}
} else {
// Parsing failed, run check on raw source to get errors
pipeline::run_check_with_bmc(ftl_source, bmc_config)
}
} else {
// No base_path, run check on raw source
pipeline::run_check_with_bmc(ftl_source, bmc_config)
};| fn check_duplicate_ids( | ||
| main: &Program, | ||
| imported: &Program, | ||
| import_path: &str, | ||
| errors: &mut Vec<String>, | ||
| ) { | ||
| let mut existing: HashSet<String> = HashSet::new(); | ||
| for t in &main.types { existing.insert(t.id.0.clone()); } | ||
| for r in &main.regions { existing.insert(r.id.0.clone()); } | ||
| for c in &main.computes { existing.insert(c.id.0.clone()); } | ||
| for e in &main.effects { existing.insert(e.id.0.clone()); } | ||
| for k in &main.controls { existing.insert(k.id.0.clone()); } | ||
| for v in &main.contracts { existing.insert(v.id.0.clone()); } | ||
| for m in &main.memories { existing.insert(m.id.0.clone()); } | ||
| for x in &main.externs { existing.insert(x.id.0.clone()); } | ||
|
|
||
| let check = |id: &str| { | ||
| if existing.contains(id) { | ||
| Some(format!( | ||
| "duplicate node ID '{}' from import '{}'", | ||
| id, import_path | ||
| )) | ||
| } else { | ||
| None | ||
| } | ||
| }; | ||
|
|
||
| for t in &imported.types { if let Some(e) = check(&t.id.0) { errors.push(e); } } | ||
| for r in &imported.regions { if let Some(e) = check(&r.id.0) { errors.push(e); } } | ||
| for c in &imported.computes { if let Some(e) = check(&c.id.0) { errors.push(e); } } | ||
| for e_def in &imported.effects { if let Some(e) = check(&e_def.id.0) { errors.push(e); } } | ||
| for k in &imported.controls { if let Some(e) = check(&k.id.0) { errors.push(e); } } | ||
| for v in &imported.contracts { if let Some(e) = check(&v.id.0) { errors.push(e); } } | ||
| for m in &imported.memories { if let Some(e) = check(&m.id.0) { errors.push(e); } } | ||
| for x in &imported.externs { if let Some(e) = check(&x.id.0) { errors.push(e); } } | ||
| } |
There was a problem hiding this comment.
Die Funktion check_duplicate_ids ist sehr repetitiv. Sowohl das Sammeln der existierenden IDs als auch das Prüfen der importierten IDs wird für jeden Knotentyp einzeln durchgeführt. Das macht den Code schwer lesbar und fehleranfällig bei zukünftigen Erweiterungen.
Sie könnten dies refaktorisieren, indem Sie Iteratoren verketten, um alle IDs auf einmal zu sammeln und zu prüfen. Das würde den Code erheblich verkürzen und die Wartbarkeit verbessern.
fn check_duplicate_ids(
main: &Program,
imported: &Program,
import_path: &str,
errors: &mut Vec<String>,
) {
let existing_ids: HashSet<_> = main.types.iter().map(|n| &n.id.0)
.chain(main.regions.iter().map(|n| &n.id.0))
.chain(main.computes.iter().map(|n| &n.id.0))
.chain(main.effects.iter().map(|n| &n.id.0))
.chain(main.controls.iter().map(|n| &n.id.0))
.chain(main.contracts.iter().map(|n| &n.id.0))
.chain(main.memories.iter().map(|n| &n.id.0))
.chain(main.externs.iter().map(|n| &n.id.0))
.collect();
let imported_ids = imported.types.iter().map(|n| &n.id.0)
.chain(imported.regions.iter().map(|n| &n.id.0))
.chain(imported.computes.iter().map(|n| &n.id.0))
.chain(imported.effects.iter().map(|n| &n.id.0))
.chain(imported.controls.iter().map(|n| &n.id.0))
.chain(imported.contracts.iter().map(|n| &n.id.0))
.chain(imported.memories.iter().map(|n| &n.id.0))
.chain(imported.externs.iter().map(|n| &n.id.0));
for id in imported_ids {
if existing_ids.contains(id) {
errors.push(format!(
"duplicate node ID '{}' from import '{}'",
id, import_path
));
}
}
}
Summary
import "path/to/module.ftl"in PEG-GrammatikProgram.imports: Vec<String>FeldTest plan
Closes #36
🤖 Generated with Claude Code