Skip to content
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl clap::ValueEnum for Language {
}

impl Language {
fn format_files(&self, project_dir: &PathBuf, generated_files: BTreeSet<PathBuf>) -> anyhow::Result<()> {
fn format_files(&self, project_dir: &Path, generated_files: BTreeSet<PathBuf>) -> anyhow::Result<()> {
match self {
Language::Rust => rustfmt(generated_files)?,
Language::Csharp => dotnet_format(project_dir, generated_files)?,
Expand Down
16 changes: 12 additions & 4 deletions crates/cli/src/tasks/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Re
anyhow::bail!("Built project successfully but couldn't find the output file.");
}

pub(crate) fn dotnet_format(project_dir: &PathBuf, files: impl IntoIterator<Item = PathBuf>) -> anyhow::Result<()> {
pub(crate) fn dotnet_format(project_dir: &Path, files: impl IntoIterator<Item = PathBuf>) -> anyhow::Result<()> {
let cwd = std::env::current_dir().expect("Failed to retrieve current directory");
duct::cmd(
"dotnet",
itertools::chain(
Expand All @@ -119,17 +120,24 @@ pub(crate) fn dotnet_format(project_dir: &PathBuf, files: impl IntoIterator<Item
// of a full style-aware formatter. Still better than nothing though.
"whitespace",
"--folder",
project_dir.to_str().unwrap(),
// Our files are marked with <auto-generated /> and will be skipped without this option.
"--include-generated",
"--include",
]
.into_iter()
.map_into::<OsString>(),
files.into_iter().map_into(),
// Resolve absolute paths for all of the files, because we receive them as relative paths to cwd, but
// `dotnet format` will interpret those paths relative to `project_dir`.
files
.into_iter()
.map(|f| {
let f = if f.is_absolute() { f } else { cwd.join(f) };
f.canonicalize().expect("Failed to canonicalize path: {f}")
})
.map_into(),
),
)
// This is important because we're running with `--folder`. We want to format the right folder!
.dir(project_dir)
.run()?;
Ok(())
}
Loading