question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Current working directory for dotnet run3 should be project directory

See original GitHub issue

Steps to reproduce

Create a netcoreapp that does this:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("cwd = " + Directory.GetCurrentDirectory());
    }
}

dotnet run3

Expected behavior

App prints

cwd = C:\dev\MyProjectFolder

Actual behavior

App prints

cwd = C:\dev\MyProjectFolder\bin\Debug\netcoreapp1.0

Environment data

dotnet --info output:

Product Information: Version: 1.0.0-preview3-003857 Commit SHA-1 hash: 060762090d

SDK version: microsoft.net.sdk/1.0.0-alpha-20161021-1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:33 (32 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Oct 27, 2016

BTW we (ASP.NET) spent weeks on designing this experience when we did the DNX -> dotnet CLI shift. We think we ended up with the best compromise… (all things considered)

1reaction
eerhardtcommented, Oct 27, 2016

Alright @natemcmaster, I’ve thought about it more and I think you’ve convinced me. By default, dotnet run should use the current working directory of your console/terminal, just like it did for project.json based projects.

However, dotnet run will respect <StartWorkingDirectory> if it is explicitly set in the .csproj. So the rules for dotnet run are as follows:

  1. Ask the project for its <RunWorkingDirectory>, if it returns something, use that value as the working directory.
  2. If <RunWorkingDirectory> is empty, it will use the cwd of the console/terminal.

In the https://github.com/dotnet/sdk the rules for defaulting RunWorkingDirectory are:

  1. If <StartWorkingDirectory> is set, use it.
  2. For .NETFramework and .NETCoreApp, leave RunWorkingDirectory blank by default. (and for all other TFMs for now).

This only requires removing 2 lines in the dotnet/sdk:

https://github.com/dotnet/sdk/blob/c1a276fb456606e2b19d4646f8239d2546790d42/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.targets#L146 https://github.com/dotnet/sdk/blob/c1a276fb456606e2b19d4646f8239d2546790d42/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.Sdk.targets#L154

The CLI already works correctly with this change. If RunWorkingDirectory is blank, the current working directory is used.

The only problem is Visual Studio. @BillHiebert - VS will need to uptake this change. If RunWorkingDirectory comes back blank, VS will need to default the working directory during F5.

NOTE: The reason there are 2 properties <StartWorkingDirectory> and <RunWorkingDirectory> is because <StartWorkingDirectory> already exists, and users and VS set that property already. The reason we made a new property <RunWorkingDirectory> is to align it with the other “run” properties: <RunCommand> and <RunArguments>. The public contract between the dotnet/sdk and the “clients” are these <RunXXX> properties. Also, with debug profiles coming, the debug profile might want a different working directory than what <StartWorkingDirectory> is set to. So creating a new property for the contract made sense.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Current working directory for dotnet run3 should be project ...
i.e. dotnet run should use terminal current working directory, not project dir. It's consistent the CLI experience of other tools and languages ...
Read more >
dotnet run --project <PATH> not taking ...
Run dontet from the project's folder. Because appsettings.json and ssh certificates are relative to the executiion folder.
Read more >
Working directory is always project directory... how do I ...
I'm using the Project Manager extension, and currently have a project open. The problem is, if I open a file outside of the...
Read more >
Directory.SetCurrentDirectory(String) Method (System.IO)
Sets the application's current working directory to the specified directory.
Read more >
Directory.GetCurrentDirectory Method (System.IO)
Gets the current working directory of the application. ... This method is available in the .NET Compact Framework, but is not currently supported....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found