Skip to content

[Bug]: Using SetCurrentDirectory in combination with AfterTargets does not work while publishing #3000

@AlanMcInally

Description

@AlanMcInally

Describe the bug

In my Program.cs I set Current Directory:
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

In csproj I use this command to generate swagger.json:

    <Target Name="PublishSwaggerJson" AfterTargets="Publish">

        <!-- Generate the swagger.json file in the intermediate output directory -->
        <Exec Command="swagger tofile --output &quot;$(IntermediateOutputPath)swagger.json&quot; &quot;$(TargetPath)&quot; v1" />
        <!-- Copy the generated swagger.json file to the publish directory -->
        <Copy SourceFiles="$(IntermediateOutputPath)swagger.json" DestinationFolder="$(PublishDir)" />
    </Target>
    

I use this publish command:
dotnet publish --configuration release --runtime win-x64 --self-contained --framework net8.0 -p:PublishReadyToRun=true

Expected behavior

No Error

Actual behavior

error MSB3073: Der Befehl "swagger tofile --output "obj\release\net8.0\win-x64\swagger.json" "D:\TFS\Source\Workspace\P
roducts\ADFS\Gen 6\Dev\src\Public\Deprag.Adfs.Public.Api\bin\release\net8.0\win-x64\Deprag.Adfs.Public.Api.dll" v1" wur
de mit dem Code -532462766 beendet.

With dotnet publish verbosity = d:
The path ...\bin\release\net8.0\win-x64\obj\release\net8.0\win-x64\... is invalid.

  swagger tofile --output "obj\release\net8.0\win-x64\swagger.json" "D:\TFS\Source\Workspace\Products\ADFS\Gen 6\Dev\src\Public\Deprag.Adfs.Public.Api\bin\release\net8.0\win-x64\Deprag.Adfs.Public.Api.dll" v1
         [15:01:51 INF] Working directory: D:\TFS\Source\Workspace\Products\ADFS\Gen 6\Dev\src\Public\Deprag.Adfs.Public.Api\bin\release\net8.0\win-x64
         [15:01:51 INF] User profile is available. Using 'C:\Users\donhauserr\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
         Unhandled exception. System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\TFS\Source\Workspace\Products\ADFS\Gen 6\Dev\src\Public\Deprag.Adfs.Public.Api\bin\release\net8.0\win-x64\obj\release\net8.0\win-x
         64\swagger.json'.
            at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
            at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
            at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
            at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
            at System.IO.File.Create(String path)
            at Swashbuckle.AspNetCore.Cli.Program.<>c.<Main>b__0_4(IDictionary`2 namedArgs) in /_/src/Swashbuckle.AspNetCore.Cli/Program.cs:line 104
            at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in /_/src/Swashbuckle.AspNetCore.Cli/CommandRunner.cs:line 68
            at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args) in /_/src/Swashbuckle.AspNetCore.Cli/CommandRunner.cs:line 59
            at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args) in /_/src/Swashbuckle.AspNetCore.Cli/Program.cs:line 141

Steps to reproduce

No response

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

No response

.NET Version

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions