Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
InsanityCode opened this issue May 7, 2024 · 4 comments
Assignees
Labels
area-dotnet-debugging Issues around .NET Debugging with console, ASP.NET, or Aspire area-project-cps bug Something isn't working
Milestone

Comments

@InsanityCode
Copy link

InsanityCode commented May 7, 2024

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

  • Windows
  • VS Code 1.89.0
  • C# Dev Kit v1.5.20
@InsanityCode InsanityCode added the bug Something isn't working label May 7, 2024
@InsanityCode
Copy link
Author

I am aware that there is a similar issue at #114.
This issue is already closed, but the problem described above still exists.

@InsanityCode
Copy link
Author

InsanityCode commented May 7, 2024

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

@arkalyanms arkalyanms added the area-project Project load, Solution Explorer label May 8, 2024
@InsanityCode
Copy link
Author

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: dotnet/sdk#22342

@lifengl
Copy link
Member

lifengl commented May 10, 2024

@tmeschter tmeschter removed the area-project Project load, Solution Explorer label May 14, 2024
@WardenGnaw WardenGnaw added this to the June2024 milestone May 31, 2024
@WardenGnaw WardenGnaw self-assigned this May 31, 2024
@WardenGnaw WardenGnaw added the area-dotnet-debugging Issues around .NET Debugging with console, ASP.NET, or Aspire label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dotnet-debugging Issues around .NET Debugging with console, ASP.NET, or Aspire area-project-cps bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants