diff --git a/crates/cli/src/subcommands/generate.rs b/crates/cli/src/subcommands/generate.rs index 5f6d935bfc5..a0cd8b1fb39 100644 --- a/crates/cli/src/subcommands/generate.rs +++ b/crates/cli/src/subcommands/generate.rs @@ -285,7 +285,7 @@ impl clap::ValueEnum for Language { } impl Language { - fn format_files(&self, project_dir: &PathBuf, generated_files: BTreeSet) -> anyhow::Result<()> { + fn format_files(&self, project_dir: &Path, generated_files: BTreeSet) -> anyhow::Result<()> { match self { Language::Rust => rustfmt(generated_files)?, Language::Csharp => dotnet_format(project_dir, generated_files)?, diff --git a/crates/cli/src/tasks/csharp.rs b/crates/cli/src/tasks/csharp.rs index 8a18f8ed55a..5df8b730448 100644 --- a/crates/cli/src/tasks/csharp.rs +++ b/crates/cli/src/tasks/csharp.rs @@ -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) -> anyhow::Result<()> { +pub(crate) fn dotnet_format(project_dir: &Path, files: impl IntoIterator) -> anyhow::Result<()> { + let cwd = std::env::current_dir().expect("Failed to retrieve current directory"); duct::cmd( "dotnet", itertools::chain( @@ -119,17 +120,24 @@ pub(crate) fn dotnet_format(project_dir: &PathBuf, files: impl IntoIterator and will be skipped without this option. "--include-generated", "--include", ] .into_iter() .map_into::(), - 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(()) }