diff --git a/crates/ide-build/src/compile/mod.rs b/crates/ide-build/src/compile/mod.rs index 4c541900d3..e4416ec7a0 100644 --- a/crates/ide-build/src/compile/mod.rs +++ b/crates/ide-build/src/compile/mod.rs @@ -294,7 +294,7 @@ pub fn plan( } BuildSystem::TypeScript => ts_steps(&dir, kind), BuildSystem::CMake => cmake_steps(&dir, kind, cmake.and_then(<[_]>::first)), - BuildSystem::Dotnet => dotnet_steps(&dir, kind)?, + BuildSystem::Dotnet => dotnet_steps(&dir, kind, file)?, // Never detected from a folder: it comes from the Project Structure (`structure_plan`). BuildSystem::Javac => return Err(IdeError::invalid("no build tool to run")), } @@ -553,10 +553,24 @@ fn dotnet_target(dir: &Path) -> Option { solution.or_else(|| files.iter().find(|p| matches!(ext(p).as_str(), "csproj" | "fsproj" | "vbproj"))).cloned() } -/// `dotnet build` of the directory's solution or project; Rebuild skips incremental builds, +/// `dotnet build` of `file` when it is a solution or project in `dir` (a run configuration's +/// project), else of the directory's solution or project; Rebuild skips incremental builds, /// Build Artifacts publishes. The summary that repeats every message is left out. -fn dotnet_steps(dir: &Path, kind: BuildKind) -> IdeResult> { - let target = dotnet_target(dir).ok_or_else(|| IdeError::not_found("no .NET solution or project found"))?; +fn dotnet_steps(dir: &Path, kind: BuildKind, file: Option<&Path>) -> IdeResult> { + let given = file.filter(|f| { + f.parent() == Some(dir) + && f.is_file() + && f.extension().is_some_and(|e| { + matches!( + e.to_string_lossy().to_ascii_lowercase().as_str(), + "sln" | "slnx" | "csproj" | "fsproj" | "vbproj" + ) + }) + }); + let target = given + .map(Path::to_path_buf) + .or_else(|| dotnet_target(dir)) + .ok_or_else(|| IdeError::not_found("no .NET solution or project found"))?; let target = target.file_name().map_or_else(String::new, |n| n.to_string_lossy().into_owned()); let verb = if kind == BuildKind::Artifacts { "publish" } else { "build" }; let mut step = BuildStep::new("dotnet", &[verb, &target, "-nologo", "-clp:NoSummary"], dir, Format::Msbuild); @@ -1734,6 +1748,36 @@ mod tests { let p = plan(&net, &BuildModel::default(), BuildKind::Artifacts, None, None, None, &CancellationToken::new()) .unwrap(); assert_eq!(p.steps[0].args[0], "publish"); + // A run configuration's project is built even when its folder holds other projects or a + // solution that sort first (#8179). + write(&net, "src/Host/Legacy.csproj", ""); + write(&net, "src/Host/Main.csproj", ""); + write(&net, "src/Host/All.sln", ""); + for name in ["Main.csproj", "All.sln", "Legacy.csproj"] { + let file = net.join("src/Host").join(name); + let p = plan( + &net, + &BuildModel::default(), + BuildKind::Build, + Some(&file), + None, + None, + &CancellationToken::new(), + ) + .unwrap(); + assert_eq!(p.steps[0].args[1], name); + } + let p = plan( + &net, + &BuildModel::default(), + BuildKind::Build, + Some(&net.join("src/Host/Program.cs")), + None, + None, + &CancellationToken::new(), + ) + .unwrap(); + assert_eq!(p.steps[0].args[1], "All.sln"); let cm = tmp.path().join("cm"); write(&cm, "CMakeLists.txt", "project(app)");