Have you asked the AI in the project about this? Sometimes AI are able to do a decent job of providing an answer but then at times they may hallucinate an answer so you will have to see if it works.
I also find that asking for “best practices” at times will give the “best practice” which tends to algin with the training data and thus what the AI will often give and try to use in most cases.
You noted " AGENTS.md instruction", is that really the best place to add this? The prompt might be correct but not being included with your workflow? (Rhetorical questions)
I have run into this permission error as well. In my case, it was resolved either by preventing pytest from creating and using those directories or by switching the cache to a writable path.
You can avoid the issue entirely by disabling pytest’s cache plugin via a pytest.ini file. This prevents creation of the .pytest_cache directory and the pytest-cache-files-* temp directories. The tradeoff is that cached information such as lastfailed and collection node IDs is no longer available. You can also exclude those directories from test collection.
For small projects this is usually fine. Test runs may be slightly slower, and information like last or first failed tests is lost. Some plugins may also rely on the cache, and certain debugging details may no longer be available.
If you do need the cache data, you can instead point the cache to a writable directory. In my case, Codex sometimes required full access for the session.
I have not used pytest in a while, so I cannot say with certainty which warnings are expected and which may be specific to the interaction with Codex. I wouldn’t be at all surprised if it was a Windows bug.
Update (temp path detail):
By default, my pytest temp artifacts are being created under Windows temp, e.g.:
%TEMP%\pytest-of-<user>\pytest-<n>\...
This works perfectly when I run tests manually inside VS Code (Test Explorer / normal terminal).
However, when Codex agent runs pytest, it seems to run inside a sandbox / different execution context where writing to this default pytest temp directory fails (permission / access issue), so the suite cannot complete.
I also tried overriding the temp location to:
inside the repo
inside %USERPROFILE%
a newly created folder per run
…but the Codex extension environment still fails whenever the tests attempt to create/write temp files.
In my experience setting the cache directory to something like
%TEMP%\pytest-of-<user>\pytest-<n>\...
did work with a little hiccup because I had to (1) start a new cmd session or (2) a powershell session with full permissions for Codex. Can you try these two approaches?
I am experiencing this same problem on Windows 11 with latest VSCode and Codex builds. None of the workarounds have worked for me–it seems Codex running in the VSCode terminal window is not following the settings or else they don’t make a difference.
Here’s what I’ve tried, after creating .pytest_cache and .pytest_tmp as my user.
Despite these, Codex is creating folders owned by the Codex user with restrictive permissions that can’t be deleted by my normal user running VSCode and terminal.
I will openly admit that I stopped looking for ways to resolve this issue because it happens with every new project using pytest.
I can offer one suggestion and one workaround.
Suggestion: add your voice to the relevant issue in the official Codex repo by upvoting it, or by creating a new one. I linked a thorough report from another user below. They also link to several related topics inside the report.
Workaround: task Codex with finding a solution that does not create additional noise when running the test suite. This can take 15 to 30 minutes, but it usually comes up with a working solution.