Skip to content

Change default working directory to the output folder - #3073

Merged
BillHiebert merged 2 commits into
dotnet:masterfrom
BillHiebert:FixDefaultWorkingDir
Jan 2, 2018
Merged

BillHiebert merged 2 commits into
dotnet:masterfrom
BillHiebert:FixDefaultWorkingDir

Conversation

@BillHiebert

Copy link
Copy Markdown
Contributor

Changed the default working directory to be the output folder rather than the project root. This addresses issue #2239.
Note that there is a very slight behavior change. If the executable path specified in the profile is a relative path, it is assumed to be relative to the working directory (whether is comes from msbuild, the profile, or the default). This means that if no working directory is specified, it is now relative to the output directory rather than the project root - which I think makes more sense anyway.

private async Task<string> GetOutputDirectoryAsync(ConfiguredProject configuredProject)
{
var properties = configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties();
var outdir = await properties.GetEvaluatedPropertyValueAsync("OutDir").ConfigureAwait(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we safe to rely on OutDir in all cases?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tagging @rainersigwald for an opinion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely no guarantee, unfortunately: even without the ASP.NET confusion, a project (or NuGet package) could customize outputs so they don't respond to the convention.

That said, ASP.NET Core projects look to me like they behave ok, so something must have changed since that blog post.

I think this design is safe for this, but see Nat's other comment too for belt + suspenders.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I push another change which adds the fallback logic suggested by Rainer

@jmarolf jmarolf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

// the project), or the project folder.
string defaultWorkingDir = projectFolder;
// If no working directory specified in the profile, we default to output directory.
string defaultWorkingDir = await GetOutputDirectoryAsync(configuredProject).ConfigureAwait(false);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for OutDir to be unset (manually or otherwise), making it necessary to fallback on project folder?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's definitely possible for it to be manually unset. It'd probably break lots of other things, though.

It might be wise to check for check for empty as well as directory existence.

@micdah

micdah commented Jul 24, 2018

Copy link
Copy Markdown

Has this behaviour been changed in newer versions?

Using .Net Core 2.1.302 it seems working directory it not changed, i.e. it is whatever it is from where dotnet run is being executed from.

I'm experiencing issues with [DllImport] due to this, as this is sensitive to being able to find the relevant libraries in the current working directory.

@micdah

micdah commented Jul 24, 2018

Copy link
Copy Markdown

Output from dotnet --info:

.NET Core SDK (reflecting any global.json):
 Version:   2.1.302
 Commit:    9048955601

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.13
 OS Platform: Darwin
 RID:         osx.10.13-x64
 Base Path:   /usr/local/share/dotnet/sdk/2.1.302/

Host (useful for support):
  Version: 2.1.2
  Commit:  811c3ce6c0

.NET Core SDKs installed:
  2.1.302 [/usr/local/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

@siberianguy

Copy link
Copy Markdown

Looks like this problem is still around: Directory.GetCurrentDirectory still returns the project file for me

@Compufreak345

Copy link
Copy Markdown

@siberianguy @BillHiebert @micdah @Pilchie
I am experiencing the same issue with .Net Core 2.2.301 - any ideas why this happens again?

@jjmew

jjmew commented Jul 15, 2019

Copy link
Copy Markdown
Contributor

@Compufreak345 @siberianguy Can you please open a new issue for this?

@Compufreak345

Copy link
Copy Markdown

@siberianguy @jjmew I created issue #5053 for it. As I tried creating a reproducable example I found that this behaviour occures with a new WebApplication but not with a new ConsoleApplication.

@gasser-andy gasser-andy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5D2A1A1D-F34C-4916-A5E3-533F1EE9D3AD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.