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.

Consider adding setting a 'project directory' environment variable in dotnet-run

See original GitHub issue

It looks like some of the ASP.NET-specific stuff used to support an ASPNET_APPLICATIONBASE environment variable in order to allow a web application to automatically determine its ‘base path’ correctly whether it was running from the context of a project with dotnet-run (variable set) or from the context of a set of published outputs (no variable set.) It seems that the Visual Studio tooling sets this variable since some point in the past, at least to support RC1 projects.

I don’t know whether this kind of situation will really apply to anything other than ASP.NET web applications, at least in the overwhelming majority of cases, but I feel like dotnet-run should be responsible for setting one or more environment variables like DOTNET_PROJECTDIRECTORY that running applications could use to be context-aware.

Related:

https://github.com/dotnet/cli/pull/1382 https://github.com/aspnet/Hosting/issues/651

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

24reactions
dasMullicommented, Feb 23, 2017

Note that you can add this to your csproj file inside a <PropertyGroup> to force dotnet run to use the project’s directory:

<RunWorkingDirectory>$(MSBuildThisFileDirectory)</RunWorkingDirectory>

Setting RunWorkingDirectory as environment variable will also make MSBuild pick it up.

3reactions
saikatguhacommented, Jun 29, 2016

I have the same problem. I am trying to dotnet run -p ./path/to/my/project

In main, I have

new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())

and then in Startup I have

new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddUserSecrets()

This results in:

Unhandled Exception: System.InvalidOperationException: Unable to locate a project.json at '{CurrentDirectory}'.
   at Microsoft.Extensions.Configuration.UserSecrets.PathHelper.GetSecretsPath(IFileProvider provider)

Since dotnet run -p doesn’t change the current working directory, the base path point to current and not to ./path/to/my/project. And AddUserSecrets is expecting to find a project.json at the basepath and failing to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dotnet run OR dotnet watch with development environment ...
I am using dotnet watch command to run asp.net core project. However, by default, it is picking up the Production as an environment....
Read more >
Configuration in ASP.NET Core
Settings files, such as appsettings.json; Environment variables ... The dotnet run command must be run in the project directory. .NET CLI
Read more >
How to set the hosting environment in ASP.NET Core
Click Environment Variables to view the list of current environment variables on your system. ... Click OK to save all your changes. You...
Read more >
Environment Variables in ASP.NET Core
Learn how to set configurations in Environment Variables and read them in ASP.NET core under Windows, Mac, Linux, IIS, Dot net run etc....
Read more >
Using .env in .NET
Setting environment variables during development is a little bit more tricky though.
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