@@ -197,16 +197,95 @@ async function downloadAndExtractZstdWithStreaming(
197197 await tar . extractTarZst ( response , dest , tarVersion , logger ) ;
198198}
199199
200- /** Gets the path to the toolcache directory for the specified version of the CodeQL tools. */
201- export function getToolcacheDirectory ( version : string ) : string {
200+ /** Gets the path to the toolcache directory that holds all versions of the CodeQL tools. */
201+ function getToolcacheToolDirectory ( ) : string {
202202 return path . join (
203203 getRequiredEnvParam ( "RUNNER_TOOL_CACHE" ) ,
204204 TOOLCACHE_TOOL_NAME ,
205- semver . clean ( version ) || version ,
205+ ) ;
206+ }
207+
208+ /** Gets the name of the toolcache directory that holds the given version of the CodeQL tools. */
209+ function getToolcacheVersionDirectoryName ( version : string ) : string {
210+ return semver . clean ( version ) || version ;
211+ }
212+
213+ /** Gets the path to the toolcache directory for the specified version of the CodeQL tools. */
214+ export function getToolcacheDirectory ( version : string ) : string {
215+ return path . join (
216+ getToolcacheToolDirectory ( ) ,
217+ getToolcacheVersionDirectoryName ( version ) ,
206218 os . arch ( ) || "" ,
207219 ) ;
208220}
209221
222+ /** The outcome of trying to reclaim disk space by deleting the CodeQL tools from the toolcache. */
223+ export interface ToolcacheCleanupResult {
224+ /** The versions of the CodeQL tools that were deleted. */
225+ deletedVersions : string [ ] ;
226+ /**
227+ * Whether we hit an error while trying to delete the tools. Distinguishes a toolcache that had
228+ * nothing to reclaim from one we failed to clean up.
229+ */
230+ failed : boolean ;
231+ }
232+
233+ /**
234+ * Deletes the CodeQL tools from the toolcache.
235+ *
236+ * Only safe to call when we are about to download the tools, since that means we did not resolve
237+ * them from the toolcache and so nothing in there is in use by this job.
238+ *
239+ * This only ever touches the CodeQL directory of the toolcache, and is best-effort: any failure is
240+ * logged rather than propagated, since the caller can proceed without the disk space.
241+ *
242+ * @returns the versions that were deleted, and whether we hit an error while trying.
243+ */
244+ export async function deleteToolcacheBundles (
245+ logger : Logger ,
246+ ) : Promise < ToolcacheCleanupResult > {
247+ const toolDirectory = getToolcacheToolDirectory ( ) ;
248+
249+ try {
250+ // Refuse to follow a symlinked CodeQL directory, so that we can only ever delete paths that are
251+ // really inside the toolcache.
252+ if ( ( await fs . promises . lstat ( toolDirectory ) ) . isSymbolicLink ( ) ) {
253+ logger . info (
254+ `Not deleting the CodeQL tools from the toolcache since ${ toolDirectory } is a symlink.` ,
255+ ) ;
256+ return { deletedVersions : [ ] , failed : true } ;
257+ }
258+ } catch {
259+ logger . debug (
260+ `There are no CodeQL tools at ${ toolDirectory } to delete from the toolcache.` ,
261+ ) ;
262+ return { deletedVersions : [ ] , failed : false } ;
263+ }
264+
265+ try {
266+ const versions = (
267+ await fs . promises . readdir ( toolDirectory , { withFileTypes : true } )
268+ )
269+ . filter ( ( entry ) => entry . isDirectory ( ) )
270+ . map ( ( entry ) => entry . name )
271+ . sort ( ) ;
272+
273+ await fs . promises . rm ( toolDirectory , { force : true , recursive : true } ) ;
274+
275+ logger . info (
276+ `Deleted the CodeQL tools at ${ toolDirectory } from the toolcache to free up disk space. ` +
277+ `Versions deleted: ${ versions . join ( ", " ) || "none" } .` ,
278+ ) ;
279+
280+ return { deletedVersions : versions , failed : false } ;
281+ } catch ( e ) {
282+ logger . info (
283+ `Failed to delete the CodeQL tools at ${ toolDirectory } from the toolcache: ${ getErrorMessage ( e ) } ` ,
284+ ) ;
285+ return { deletedVersions : [ ] , failed : true } ;
286+ }
287+ }
288+
210289export function writeToolcacheMarkerFile (
211290 extractedPath : string ,
212291 logger : Logger ,
0 commit comments