microsoft / vscode-dotnettools

This is a feedback repository to capture issues logged for the C# Dev Kit and related extensions from Visual Studio Code
Other
234 stars 13 forks source link

[BUG] $(SolutionDir) is not set when building or debugging a project from Solution Explorer #1105

Open InsanityCode opened 6 months ago

InsanityCode commented 6 months ago

Describe the Issue

Running a single project from the solution explorer does not set the SolutionDir property for the built project.

The term 'Solution Explorer' implies that a solution file is present.
So when building or running a project from the solution explorer, SolutionDir should be set for the built project.

Steps To Reproduce

  1. Open Visual Studio Code in an empty folder

  2. Create a solution

    dotnet new sln --name DemoSolution --output ./Demo/
  3. Create a project

    dotnet new console --language "C#" --output ./Demo/DemoProject/
  4. Add the project to the solution

    dotnet sln ./Demo/DemoSolution.sln add ./Demo/DemoProject/DemoProject.csproj
  5. Create ./Demo/DemoImport.props with the following content

    <Project>
    
     <Target Name="DemoTarget" BeforeTargets="BeforeBuild">
       <Message Importance="High" Text="Hello, Demo!" />
     </Target>
    
    </Project>
  6. Import ./Demo/DemoImport.props into the project by adding the following line

    <Import Project="$(SolutionDir)/DemoImport.props" />
  7. Right click DemoProject in the Solution Explorer and select Debug -> Start New Instance

The build then fails with MSB4019 stating that X:/DemoImport.props could not be found.
This is because $(SolutionDir) evaluates to an empty string which leads to the remaining path /DemoImport.props being interpreted as an absolute path.


The same happens when you right click the solution and select Clean and then right click the project and select Build.

This does not occur when right clicking the project and selecting Rebuild.
Selecting Build or Rebuild on the solution works as well.

Expected Behavior

DemoProject builds and then runs.
During the build it prints Hello, Demo!.
When running it prints Hello, World!.

Environment Information

InsanityCode commented 6 months ago

I am aware that there is a similar issue at https://github.com/microsoft/vscode-dotnettools/issues/114. This issue is already closed, but the problem described above still exists.

InsanityCode commented 6 months ago

Using the steps described above I created a little Test Repository that demonstrates the problem.

InsanityCode commented 6 months ago

It seems I forgot to mention that MSB4019 also occurs when attempting to start the program using F5.

  1. Open ./Demo/DemoProject/Program.cs in the editor
  2. Press F5
  3. When prompted with Select debugger, select C#
  4. When prompted with Select Launch Configuration, select C#: DemoProject

In the build output we can see that the executed command is

dotnet build X:/full/path/to/Demo/DemoProject/DemoProject.csproj `
    /property:GenerateFullPaths=true `
    /consoleloggerparameters:NoSummary

This command does not provide any information about the solution.

Manually specifying SolutionDir works just fine:

dotnet build ./Demo/DemoProject/DemoProject.csproj --property:SolutionDir="$(Get-Location)/Demo/"

As for actually running the project...

The dotnet run Description states that

dotnet run doesn't respect arguments like /property:property=value, which are respected by dotnet build.

allthough on the same page in Options it says

  • --property:<NAME>=<VALUE> Sets one or more MSBuild properties. [...]

Either way, for both

dotnet run --project ./Demo/DemoProject/DemoProject.csproj --property:SolutionDir="$(Get-Location)/Demo/"

and

dotnet build ./Demo/DemoProject/DemoProject.csproj --property:SolutionDir="$(Get-Location)/Demo/"
dotnet run --project ./Demo/DemoProject/DemoProject.csproj --no-build

dotnet run fails, printing that ./Demo/DemoProject/DemoProject.csproj is not a valid project file without any information why.

Related issue: https://github.com/dotnet/sdk/issues/22342

lifengl commented 6 months ago