actions / toolkit

The GitHub ToolKit for developing GitHub Actions.
https://github.com/features/actions
MIT License
4.93k stars 1.42k forks source link

Support `--dereference` when saving cache #1293

Open p0deje opened 1 year ago

p0deje commented 1 year ago

Describe the enhancement

Sometimes the cache that is being tared contains symlinks that point to the nearby files or even files outside of the cached directory. The cache saves perfectly fine, but later when we attempt to restore such cache, it fails because it cannot create a symlink to the file that does not exist. It looks like this:

Attempting to restore external-python_3_10_x86_64-pc-windows-msvc cache from setup-bazel-1-win32-external-python_3_10_x86_64-pc-windows-msvc-6f4c09e6b71e9933f1f2aace478aa950ee73538fa85cce7bef10e24779d729f6
  Received 36631515 of 36631515 (100.0%), 39.1 MBs/sec
  Cache Size: ~35 MB (36631515 B)
  C:\Windows\System32\tar.exe -z -xf D:/a/_temp/02921f51-e302-43b7-b3bb-f5d3a5f5a729/cache.tgz -P -C D:/a/selenium/selenium
  ../../../_bazel/external/python_3_10_x86_64-pc-windows-msvc/python: Can't create '\\\\?\\D:\\a\\selenium\\selenium\\..\\..\\..\\_bazel\\external\\python_3_10_x86_64-pc-windows-msvc\\python'
  tar.exe: Error exit delayed from previous errors.
  Warning: Failed to restore: Tar failed with error: The process 'C:\Windows\System32\tar.exe' failed with exit code 1
  Failed to restore external-python_3_10_x86_64-pc-windows-msvc cache

Using GNU Tar is also not helpful, though an error is more explicit:

Attempting to restore external-python_3_9_x86_64-pc-windows-msvc cache from setup-bazel-1-win32-external-python_3_9_x86_64-pc-windows-msvc-6f4c09e6b71e9933f1f2aace478aa950ee73538fa85cce7bef10e24779d729f6
  Received 35447681 of 35447681 (100.0%), 37.2 MBs/sec
  Cache Size: ~34 MB (35447681 B)
  "C:\Program Files\Git\usr\bin\tar.exe" --use-compress-program "zstd -d" -xf D:/a/_temp/75736ff4-aba8-4310-8874-c6d06ee98110/cache.tzst -P -C D:/a/selenium/selenium --force-local
  /usr/bin/tar: ../../../_bazel/external/python_3_9_x86_64-pc-windows-msvc/python: Cannot create symlink to '/d/_bazel/external/python_3_9_x86_64-pc-windows-msvc/python.exe': No such file or directory
  /usr/bin/tar: Exiting with failure status due to previous errors
  Warning: Failed to restore: Tar failed with error: The process 'C:\Program Files\Git\usr\bin\tar.exe' failed with exit code 2
  Failed to restore external-python_3_9_x86_64-pc-windows-msvc cache

The problem above happens because there is python symlink which points to python.exe in the same directory. However, since the file is not yet extracted, attempting to create a symlink to it fails.

tar has a flag --dereference which allows to follow symbolic links and copy linked files instead - https://ftp.gnu.org/old-gnu/Manuals/tar-1.12/html_node/tar_115.html. I think it would be nice if saveCache(...) supported a boolean flag dereferenceSymbolicLinks that would pass --dereference argument to tar.

Code Snippet

const cache = require('@actions/cache')
await cache.saveCache(['directory'], 'cache-key', { dereferenceSymbolicLinks: true })

Additional information

I can send PR adding this flag if this is something that you'd consider adding.

mvhenten commented 4 days ago

I'd love to see this feature - working with an npm monorepo, I transpile packages on prepare, but the transpiled code is never cached, so I have to re-run the transpilation every time.