dotnet / dotnet/aspnetcore

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

Open
#60,987 5 comments 0 reactions 0 assignees View on GitHub
area-commandlinetools feature-openapi
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] 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

``` csharp

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.

``` csharp
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.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with the Program.cs change and the `dotnet build` steps in the issue. Start by reading `Microsoft.Extensions.ApiDescription.Server.targets` around line 68 and trace how `test-webapi.OpenApiFiles.cache` is located or created. Done means the sample builds without a DirectoryNotFoundException after changing the current directory.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.