Write generated files to output-dir#761
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes #760 by ensuring generated code files emitted by generator plugins are written under the --output-dir specified to slicec, rather than always using the generator-provided paths relative to the current working directory.
Changes:
- Thread
output_dirthrough generator response handling to the file-writing routine. - Compute the on-disk output path by prefixing generated file paths with
--output-dirwhen provided. - Use the computed output path for read/overwrite checks and file creation.
Comments suppressed due to low confidence (1)
slicec/src/main.rs:131
- The IO diagnostic reports
path: generated_file.path, which no longer matches the actual file location when--output-diris used. Update the diagnostic to use the computed output path (includingoutput_dir) so users can see where the write failed.
let diagnostic = slicec::diagnostics::Error::IO {
action: "write generated file",
path: generated_file.path.to_owned(),
error: io_error,
};
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Compute the output path. If an output directory was specified, prepend it to the generated file's relative path. | ||
| let generated_file_path = match output_dir { | ||
| Some(dir) => std::path::PathBuf::from(dir).join(&generated_file.path), | ||
| None => std::path::PathBuf::from(&generated_file.path), | ||
| }; |
There was a problem hiding this comment.
When output_dir is set, PathBuf::join will ignore it if generated_file.path is absolute, and it will also allow .. path traversal to escape the output directory. Since generated_file.path comes from an external generator process, validate it’s a relative path without parent/root/prefix components (or explicitly enforce that the final resolved path stays within output_dir) and return an error if it doesn’t.
| // Write the generated file to disk. | ||
| let mut file = File::create(&generated_file.path)?; | ||
| let mut file = File::create(&generated_file_path)?; | ||
| file.write_all(generated_file_bytes)?; |
There was a problem hiding this comment.
File::create(&generated_file_path) will fail if the parent directories don’t exist (e.g., when --output-dir points to a new directory or the generator emits paths like foo/bar.rs). Consider creating generated_file_path.parent() (and/or the output dir) with std::fs::create_dir_all before creating the file, and surface a clear IO diagnostic if that fails.
|
|
||
| // Compute the output path. If an output directory was specified, prepend it to the generated file's relative path. | ||
| let generated_file_path = match output_dir { | ||
| Some(dir) => std::path::PathBuf::from(dir).join(&generated_file.path), |
There was a problem hiding this comment.
I’d just add a “use std::path::PathBuf” to this file so you don’t need to qualify it here.
fix #760