Skip to content
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

Refitter failed to write generated code: System.IO.IOException: The process cannot access the file 'Generated.cs' because it is being used by another process. #520

Open
fabioloreggian opened this issue Nov 5, 2024 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@fabioloreggian
Copy link

Describe the bug
Refitter failed to write generated code: System.IO.IOException: The process cannot access the file 'Generated.cs' because it is being used by another process.

When I run the following commands in my build pipeline:

  1. dotnet restore
  2. dotnet pack

The dotnet restore works as expected, however the dotnet pack fails with the exception above. My assumption is the dotnet restore regenerates the file, doesn't close the stream and when dotnet pack runs, it complains about the file being in use.

I have seen similar behaviour when trying to move the generated source into a different directory in Rider. Where I have to close Rider then move the file and then open Rider again.

Im not sure what is causing the file to still be in use. As File.WriteAllText should release the file immediately after writing. However this is still an issue.

@fabioloreggian fabioloreggian added the bug Something isn't working label Nov 5, 2024
@christianhelle
Copy link
Owner

@fabioloreggian Thanks for taking the time to report this

Have you tried shutting down the C# build-server? I have run into this a couple of times and this helped when the source generator started acting up

dotnet build-server shutdown

I have not used dotnet pack with Refitter yet as dotnet build runs the source generator. I use Refitter from my CI/CD pipeline and in this case I only want to build once, so I use the --no-build argument with dotnet when I do things like dotnet test or dotnet publish

@christianhelle
Copy link
Owner

@all-contributors please add @fabioloreggian for bugs

Copy link
Contributor

@christianhelle

I've put up a pull request to add @fabioloreggian! 🎉

@fabioloreggian
Copy link
Author

Thanks for the response.

I will try updating the build pipelines config to only trigger the build once. Hopefully that will sort out this issue.

@christianhelle
Copy link
Owner

Thanks for the response.

I will try updating the build pipelines config to only trigger the build once. Hopefully that will sort out this issue.

If you use the source generator, you will probably need to build 2x, as the source generator produces actual files that are written to disk post-compilation, so you will need to rerun the build to build the Refit types.

The way I do this is that I use dotnet build to trigger Refitter, then I do the release build once the Refit types are generated using dotnet build --configuration Release -warnaserror /p:SourceRevisionId=x.x.x.x /p:Version=x.x.x.x

@fabioloreggian
Copy link
Author

There is something interesting happening here.

I have the following:

dotnet restore -> dotnet build.

Inbetween the restor and build I run dotnet build-server shutdown.

I can see the build server shutting down successfully. However the build step is failing with the same error as before.

Also just an FYI the built code is already checked into the repo. (not sure if that makes any difference)

@christianhelle
Copy link
Owner

@fabioloreggian, does this only happen in your CI/CD pipeline and not locally? I mostly use Refitter on projects with a CI/CD pipeline on Azure DevOps using Microsoft-hosted agents running Ubuntu and have no problems.

@fabioloreggian
Copy link
Author

Yeah it is only happening in our Azure devops pipeline. It is super strange. Because it is repeatable every time.

I did play around with the dotnet commands and the ordering. But nothing really seemed to work. I will do some more investigation tomorrow.

@christianhelle
Copy link
Owner

@fabioloreggian I would like to investigate this further. May I ask what version of .NET you target and what build agent you're using on Azure DevOps? We build, test, package, and deploy everything on ubuntu-latest. I have over 100 deployment pipelines running Refitter to generate client SDK's and don't really have any problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants