Open divega opened 6 years ago
Any temporary work around for this issue? Can we use a fixed path for the database file?
I am facing this issue on mac
@girishnuli : As we found out in aspnet/EntityFramework.Docs#735, you could change the working directory of the project (Project Settings > Debug > Working Directory
) to its root folder.
@BillHiebert I was under the (incorrect) impression that with https://github.com/dotnet/project-system/issues/2239 we were changing the current directory to be consistent inside and outside of Visual Studio, while at same time being consistent with .NET Framework projects. It looks like however, that while we changed it change it to be consistent with desktop inside of Visual Studio, it now inconsistent with itself outside of Visual Studio.
Sounds like we need to change this design slightly. At minimum we should consistent with ourselves in all cases, and stretch goal is to be consistent with desktop.
@dsplaisted Do you have an opinion on this?
I don't want to change this in an update - it will lead to confusion. I'd prefer to do this in a major update.
I think that when running dotnet
from the command line, we don't normally set the current directory. So if you do dotnet run
, then the current directory will be the project directory, but if you do dotnet run -project ..\MyOtherProject\MyOtherProject.csproj
, then the current directory won't be the project directory. The launchSettings.json can specify a working directory, which I think dotnet run
would use, but it's not set by default.
I don't have all the cases in my head, but I think we should probably undo the change made in #3073, at least for some subset of projects (SDK-style perhaps). That means it would be inconsistent with desktop projects, but mostly consistent within .NET Core projects between VS and the command line.
Direct support for making the working directory the same as the project root would be a decent compromise. If you want this behavior after the #3073 "fix", VS requires you to set an absolute path, which means the setting cannot be checked in to source code control. The current behavior of #3073 also means VS is the odd-ball on a mix-IDE team: dotnet-cli, VS Code, and JetBrains Rider all behave like "dotnet run" from the project root.
Any plans on this? This also makes it rather tricky to use dynamic loading (like Assembly.LoadFrom
) with any consistency between VS and CLI.
@vitek-karas We do plan on addressing this, on your particular issue, you should not be relying on "Current Directory" to find dlls, or other things related to your project; anyone can launch your exe with a different working directory.
I found a workaround from https://github.com/aspnet/websdk/issues/238:
This can easily be fixed by setting RunWorkingDirectory in the project file:
<PropertyGroup> <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory> </PropertyGroup>
The current working directory is absolutely essential on unix, maybe not that used on windows. From the code point of view:
cd someDir
dotnet run --project .../Foo.csproj
and
cd someDir
/path/To/Binary/Foo
should not expose different behavior (the CWD should be the same (.../someDir
).
This is the universal convention respected by all good citizens in the unix world (python, ruby, go, ...).
It has some nice advantages on its own. For instance, you can call dotnet run
from a script without the need to compile a project first. This turns C# into some kind of a scripting facilily, with zero deployment cost.
And if the C# parser would just ignore the first line of a file if it starts by a #!
sequence, this would open a whole new world of possibilities.
But first, make dotnet run
preserves the current directory.
Triage: We need investigation on what the correct change here is, as its unclear. Please factor in https://github.com/dotnet/project-system/issues/5053 when you do this.
Just got hit by this issue too. Rider is running, but using my database in the project folder instead of the package folder.
@akeeton Nice, it is working for me, thanks! but it is some kind of bug at Visual Studio?
Any updates on this? I just got hit by this issue also, and the RunWorkingDirectory
workaround does not work for me. I want the working directory to be the output folder of the build assets, not the project root directory.
This seems to have been closed without a resolution?
The issue is still open :)
@akeeton's solution solved my issue, which was that dotnet run
was using a different path than the .sln
output for an I/O operation.
lets wait for the outcome of https://github.com/dotnet/project-system/issues/5053
@davkean wrote:
@vitek-karas We do plan on addressing this, on your particular issue, you should not be relying on "Current Directory" to find dlls, or other things related to your project; anyone can launch your exe with a different working directory.
FWIW, I hit this issue using System.CommandLine to bind a Argument<DirectoryInfo>
.
DirectoryInfo
uses Path.GetFullPath
which uses current directory.
I made no explicit decision about using current directory or otherwise - I assumed initializing with a relative path would behave the same regardless of how I start the app.
(first reported at https://github.com/aspnet/EntityFramework.Docs/issues/735)
It seems that there was a decision in https://github.com/dotnet/project-system/issues/2239 to change the current directory to be the output directory.
I am not sure about the rationale or what the behavior was before, but what I am seeing is that (using .NET Core SDK 2.1.300, .NET Core 2.1 and Visual Studio 2017 15.7.3), the default working directory is inconsistent between executing an application in Visual Studio (using F5 or Ctrl+F5), which results in the working directory set to the output directory, and other ways, which use the project folder, like dotnet run, dotnet ef migrations commands in the CLI and even the EF Core migration tools that run in the Package Manager Console inside Visual Studio.
I am aware that it is possible to explicitly set the working directory to an absolute path using Visual Studio and that this is recorded in launch settings, which other tools pick up. This issue is about the default behavior when the working directory is not explicitly configured.
To repro, it is enough to just create a simple application that prints Directory.GetCurrentDirectory().
Here are the repro steps that shows how this impacts the location of an application's SQLite database file (based on the walkthrough at https://docs.microsoft.com/en-us/ef/core/get-started/netcore/new-db-sqlite):
Create a new console app from the command line:
Add the following EF Core packages:
(the latter is only necessary to repro the inconsistency with PMC)
Add the following sample model into Model.cs:
Add the following code into Program.cs:
Execute the following commands to create the database:
Note that the database was created in the project directory.
Executing the application from the command line results in this output.
Now try to execute the application from Visual Studio (F5 or Ctrl+F5). The output shows an exception indicating that the table doesn't exist. That is because opening a connection with a database file that doesn't exist creates an empty database file:
Now, let's try to update the database using the EF Core PMC commands. First, drop the database from the OS command-line to make sure we will observe the default behavior of the PMC commands:
Switch to the Package Manager Console inside Visual Studio, type:
You will see that one of the last lines indicates:
cc @bricelam