Skip to content

Generate OpenAPI documents on build throws DirectoryNotFoundException when application calls 'Directory.SetCurrentDirectory(AppContext.BaseDirectory)' #60987

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

Open
1 task done
wu-yafeng opened this issue Mar 18, 2025 · 3 comments
Labels
area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI feature-openapi

Comments

@wu-yafeng
Copy link
Contributor

wu-yafeng commented Mar 18, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Generate OpenAPI documents on build (dotnet build) throws DirectoryNotFoundException when application called Directory.SetCurrentDirectory(AppContext.BaseDirectory)

Expected Behavior

No exception thrown.

Steps To Reproduce

dotnet new webapi -n test-webapi
cd .\test-webapi\
dotnet add package Microsoft.Extensions.ApiDescription.Server

open Program.cs and insert Directory.SetCurrentDirectory(AppContext.BaseDirectory) at first line.

dotnet build

Exceptions (if any)

test-webapi 失败,出现 9 错误 (1.3) → bin\Debug\net9.0\test-webapi.dll
C:\Users\xxxxx.nuget\packages\microsoft.extensions.apidescription.server\9.0.3\build\Microsoft.Extensions.ApiDescription.Server.targets(68,5): error : System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\xxxxxx\source\repos\xxx\test-webapi\bin\Debug\net9.0\obj\test-webapi.OpenApiFiles.cache'.

.NET Version

9.0.200

Anything else?

Backgrounds:

I have a webapi that run as windows services and access Sqlite database. The connection string is configured in appsettings.json like Data Source=./mydb.db.

The database file is always stored in C:/Windows/System32 due to application is running under system process.

Call Directory.SetCurrentDirectory(AppContext.BaseDirectory) so that database file willbe saved at application content directory.

@ghost ghost added the area-web-frameworks label Mar 18, 2025
@martincostello martincostello added area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI feature-openapi and removed area-web-frameworks labels Mar 18, 2025
@martincostello
Copy link
Member

The OpenAPI build tooling has to execute your application to retrieve the OpenAPI document, so it will also execute anything your application does on startup.

The best approach is to detect that scenario in some way (for example the value of ASPNETCORE_ENVIRONMENT or some other environment variable, or through configuration) and avoid doing the operation that causes the exception, or change the configuration so it behaves differently in that scenario.

@qfyra
Copy link

qfyra commented Mar 18, 2025

Many of the things you usually do in Program.cs will cause this issue during build. It's not a fringe issue, it happens often. It's high time Microsoft addresses it properly, "by design" or not, because this issue isn't gonna go away on its own. The community has already supplied Microsoft with plenty of reproducible examples.

@martincostello
Copy link
Member

I agree this is a bit of a pit of failure, but there are ways to unblock usage.

The issues are mostly fundamental to the way the tool is designed (i.e. it has to run your code to get the document, it's not possible to do statically) so I don't think there's much that can be done here as a "bug fix".

I'm sure @captainsafia has plenty of ideas on how to move this forward in .NET 10 and beyond. There's various conversation on the topic of the OpenAPI tool in #57044.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI feature-openapi
Projects
None yet
Development

No branches or pull requests

3 participants