Skip to content

Write generated files to output-dir#761

Merged
pepone merged 2 commits intoicerpc:mainfrom
pepone:issue-760
Apr 6, 2026
Merged

Write generated files to output-dir#761
pepone merged 2 commits intoicerpc:mainfrom
pepone:issue-760

Conversation

@pepone
Copy link
Copy Markdown
Member

@pepone pepone commented Apr 6, 2026

fix #760

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_dir through generator response handling to the file-writing routine.
  • Compute the on-disk output path by prefixing generated file paths with --output-dir when 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-dir is used. Update the diagnostic to use the computed output path (including output_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.

Comment thread slicec/src/main.rs
Comment on lines +147 to +151
// 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),
};
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread slicec/src/main.rs
Comment on lines 161 to 163
// 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)?;
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread slicec/src/main.rs Outdated

// 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),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d just add a “use std::path::PathBuf” to this file so you don’t need to qualify it here.

@pepone pepone merged commit 637b8a4 into icerpc:main Apr 6, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated files not placed in output-dir

3 participants