Skip to content

Write shaderCache.bin next to the executable instead of CWD#1670

Merged
bghgary merged 4 commits intoBabylonJS:masterfrom
bghgary:fix-shadercache-test-cwd
Apr 22, 2026
Merged

Write shaderCache.bin next to the executable instead of CWD#1670
bghgary merged 4 commits intoBabylonJS:masterfrom
bghgary:fix-shadercache-test-cwd

Conversation

@bghgary
Copy link
Copy Markdown
Contributor

@bghgary bghgary commented Apr 22, 2026

Problem

ShaderCache.SaveAndLoad writes shaderCache.bin to the current working directory:

static const char* shaderCacheFileName = "shaderCache.bin";
std::ofstream stream(shaderCacheFileName, std::ios::binary);

When UnitTests.exe is launched from anywhere that isn't the build output directory (e.g. the repo root), the file lands in that directory and is never cleaned up. A unit test should not leave artifacts in the user's working directory.

Fix

Write the file next to the executable instead. Added a GetExecutableDirectory() helper declared in App.h and implemented per-platform:

  • Win32GetModuleFileNameW(nullptr, ...)
  • Apple_NSGetExecutablePath + std::filesystem::canonical
  • X11/Linuxstd::filesystem::canonical("/proc/self/exe")

The test now uses GetExecutableDirectory() / "shaderCache.bin", asserts each stream opened successfully, and asserts on the result of std::filesystem::remove so silent cleanup failures are visible.

No collisions between concurrent runs are possible (each CMake build has its own UnitTests.exe), and artifacts land alongside the build output rather than in the user's current directory or system temp.

Testing

Ran UnitTests.exe --gtest_filter="ShaderCache.*" from the repo root: test passes, no shaderCache.bin left behind at the repo root or next to the executable.

[Created by Copilot on behalf of @bghgary]

ShaderCache.SaveAndLoad was writing 'shaderCache.bin' to the current
working directory, which polluted whatever directory the test happened
to run from (e.g. the repo root if someone launched UnitTests.exe
directly). Switch to std::filesystem::temp_directory_path() and remove
the file after the round-trip so the test leaves no trace.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 22, 2026 18:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the ShaderCache unit test to avoid writing shaderCache.bin into the user’s current working directory by moving the artifact to the OS temp directory and attempting to delete it after the round-trip.

Changes:

  • Switch shader cache file location from CWD to std::filesystem::temp_directory_path().
  • Add a best-effort cleanup via std::filesystem::remove(..., std::error_code&).

Comment thread Apps/UnitTests/Source/Tests.ShaderCache.cpp Outdated
Comment thread Apps/UnitTests/Source/Tests.ShaderCache.cpp
Comment thread Apps/UnitTests/Source/Tests.ShaderCache.cpp Outdated
Comment thread Apps/UnitTests/Source/Tests.ShaderCache.cpp Outdated
bghgary and others added 2 commits April 22, 2026 12:00
- Use temp_directory_path(error_code) to avoid filesystem_error throws.
- Add per-run unique suffix so concurrent UnitTests.exe invocations
  don't collide on the shared temp filename.
- Assert streams opened successfully before Save/Load.
- Check the result of std::filesystem::remove so silent cleanup
  failures don't leave artifacts behind.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replaces the temp_directory_path()+unique-suffix approach with a new
GetExecutableDirectory() helper (Win32 GetModuleFileNameW, Apple
_NSGetExecutablePath, Linux /proc/self/exe) declared in App.h and
implemented per-platform. Collisions between concurrent runs are
not possible (each CMake build has its own UnitTests.exe), and
artifacts land alongside the build output rather than in the user's
current directory or system temp.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary changed the title Write shaderCache.bin to temp dir instead of CWD Write shaderCache.bin next to the executable instead of CWD Apr 22, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bghgary bghgary merged commit cb89fe1 into BabylonJS:master Apr 22, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants