Repository: compiler-explorer/compiler-explorer (ts)

Issue:
Replace temp
The node.js temp package we use hasn't updated in 4+ years and is the source of a number of deprecated warnings:

```
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated rimraf@2.6.3: Rimraf versions prior to v4 are no longer supported
npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
...
bollo:~/d/c/compiler-explorer (mg/fs_fixups|✔) 38.8s $ npm explain inflight@1.0.6
inflight@1.0.6
node_modules/inflight
  inflight@"^1.0.4" from glob@7.2.3
  node_modules/glob
    glob@"^7.1.3" from rimraf@2.6.3
    node_modules/rimraf
      rimraf@"~2.6.2" from temp@0.9.4
      node_modules/temp
        temp@"^0.9.4" from the root project
bollo:~/d/c/compiler-explorer (mg/fs_fixups|✔) 4.3s $ npm explain rimraf
rimraf@2.6.3
node_modules/rimraf
  rimraf@"~2.6.2" from temp@0.9.4
  node_modules/temp
    temp@"^0.9.4" from the root project
```

We should find an equivalent (`tmp` maybe? though that seems limited), branch and use a fixed up version, or write our own.

Pull request description:
New temp directory tracker
Replaces aging `temp` with a simple, bespoke solution. Reduces the number of deprecated/out-of-date dependencies we pick up.

Closes #7445.


Patch:
diff --git a/lib/base-compiler.ts b/lib/base-compiler.ts
index 47cb2be18..b8e038b68 100644
--- a/lib/base-compiler.ts
+++ b/lib/base-compiler.ts
@@ -22,12 +22,10 @@
 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 // POSSIBILITY OF SUCH DAMAGE.
 
-import os from 'node:os';
 import path from 'node:path';
 
 import fs from 'fs-extra';
 import * as PromClient from 'prom-client';
-import temp from 'temp';
 import _ from 'underscore';
 
 import {splitArguments, unique} from '../shared/common-utils.js';
@@ -125,6 +123,7 @@ import {HeaptrackWrapper} from './runtime-tools/heaptrack-wrapper.js';
 import {LibSegFaultHelper} from './runtime-tools/libsegfault-helper.js';
 import {SentryCapture} from './sentry.js';
 import * as StackUsage from './stack-usage-transformer.js';
+import * as temp from './temp.js';
 import {
     clang_style_sysroot_flag,
     getSpecificTargetBasedOnToolchainPath,
@@ -410,9 +409,7 @@ export class BaseCompiler {
     }
 
     async newTempDir(): Promise<string> {
-        // `temp` caches the os tmp dir on import (which we may change), so here we ensure we use the current os.tmpdir
-        // each time.
-        return await temp.mkdir({prefix: utils.ce_temp_prefix, dir: os.tmpdir()});
+        return await temp.mkdir(utils.ce_temp_prefix);
     }
 
     optOutputRequested(options: string[]) {
diff --git a/lib/execution/base-execution-env.ts b/lib/execution/base-execution-env.ts
index 6f33efa2a..9cdae8b22 100644
--- a/lib/execution/base-execution-env.ts
+++ b/lib/execution/base-execution-env.ts
@@ -22,11 +22,9 @@
 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 // POSSIBILITY OF SUCH DAMAGE.
 
-import os from 'node:os';
 import path from 'node:path';
 
 import fs from 'node:fs/promises';
-import temp from 'temp';
 
 import {splitArguments} from '../../shared/common-utils.js';
 import {
@@ -51,6 +49,7 @@ import {logger} from '../logger.js';
 import {Packager} from '../packager.js';
 import {propsFor} from '../properties.js';
 import {HeaptrackWrapper} from '../runtime-tools/heaptrack-wrapper.js';
+import * as temp from '../temp.js';
 import * as utils from '../utils.js';
 
 import {ExecutablePackageCacheMiss, IExecutionEnvironment} from './execution-env.interfaces.js';
@@ -129,7 +128,7 @@ export class LocalExecutionEnvironment implements IExecutionEnvironment {
     }
 
     async downloadExecutablePackage(hash: string): Promise<void> {
-        this.dirPath = await temp.mkdir({prefix: utils.ce_temp_prefix, dir: os.tmpdir()});
+        this.dirPath = await temp.mkdir(utils.ce_temp_prefix);
 
         this.buildResult = await this.loadPackageWithExecutable(hash, this.dirPath);
     }
diff --git a/lib/handlers/compile.ts b/lib/handlers/compile.ts
index 45c5e8b70..a67866db3 100644
--- a/lib/handlers/compile.ts
+++ b/lib/handlers/compile.ts
@@ -28,8 +28,7 @@ import fs from 'node:fs/promises';
 import * as Sentry from '@sentry/node';
 import express from 'express';
 import Server from 'http-proxy';
-import PromClient, {Counter} from 'prom-client';
-import temp from 'temp';
+import {Counter, Gauge} from 'prom-client';
 import _ from 'underscore';
 import which from 'which';
 
@@ -56,6 +55,7 @@ import {ClientOptionsType} from '../options-handler.js';
 import {PropertyGetter} from '../properties.interfaces.js';
 import {SentryCapture} from '../sentry.js';
 import {KnownBuildMethod} from '../stats.js';
+import * as temp from '../temp.js';
 import * as utils from '../utils.js';
 
 import {
@@ -65,21 +65,40 @@ import {
     ICompileHandler,
 } from './compile.interfaces.js';
 
-temp.track();
-
 let hasSetUpAutoClean = false;
 
 function initialise(compilerEnv: CompilationEnvironment) {
     if (hasSetUpAutoClean) return;
     hasSetUpAutoClean = true;
-    const tempDirCleanupSecs = compilerEnv.ceProps('tempDirCleanupSecs', 600);
+
+    new Gauge({
+        name: 'ce_compilation_temp_dirs_created',
+        help: 'Total number of temporary directories created',
+        async collect() {
+            this.set(temp.getStats().numCreated);
+        },
+    });
+    new Gauge({
+        name: 'ce_compilation_temp_dirs_removed',
+        help: 'Total number of temporary directories that had to be removed by the temp system',
+        async collect() {
+            this.set(temp.getStats().numRemoved);
+        },
+    });
+    const tempDirsBusy = new Counter({
+        name: 'ce_compilation_temp_dirs_busy',
+        help: 'Total number of times the temp dir system was busy',
+    });
+
+    const tempDirCleanupSecs = compilerEnv.ceProps('tempDirCleanupSecs', 30);
     logger.info(`Cleaning temp dirs every ${tempDirCleanupSecs} secs`);
 
     let cyclesBusy = 0;
-    setInterval(() => {
+    setInterval(async () => {
         const status = compilerEnv.compilationQueue!.status();
         if (status.busy) {
             cyclesBusy++;
+            tempDirsBusy.inc();
             logger.warn(
                 `temp cleanup skipped, pending: ${status.pending}, waiting: ${status.size}, cycles: ${cyclesBusy}`,
             );
@@ -88,10 +107,8 @@ function initialise(compilerEnv: CompilationEnvironment) {
 
         cyclesBusy = 0;
 
-        temp.cleanup((err, stats) => {
-            if (err) logger.error('temp cleanup error', err);
-            if (stats) logger.debug('temp cleanup stats', stats);
-        });
+        await temp.cleanup();
+        logger.debug('Temp cleanup stats', temp.getStats());
     }, tempDirCleanupSecs * 1000);
 }
 
@@ -113,22 +130,22 @@ export class CompileHandler implements ICompileHandler {
     private readonly proxy: Server;
     private readonly awsProps: PropertyGetter;
     private clientOptions: ClientOptionsType | null = null;
-    private readonly compileCounter: Counter<string> = new PromClient.Counter({
+    private readonly compileCounter = new Counter({
         name: 'ce_compilations_total',
         help: 'Number of compilations',
         labelNames: ['language'],
     });
-    private readonly executeCounter: Counter<string> = new PromClient.Counter({
+    private readonly executeCounter = new Counter({
         name: 'ce_executions_total',
         help: 'Number of executions',
         labelNames: ['language'],
     });
-    private readonly cmakeCounter: Counter<string> = new PromClient.Counter({
+    private readonly cmakeCounter = new Counter({
         name: 'ce_cmake_compilations_total',
         help: 'Number of CMake compilations',
         labelNames: ['language'],
     });
-    private readonly cmakeExecuteCounter: Counter<string> = new PromClient.Counter({
+    private readonly cmakeExecuteCounter = new Counter({
         name: 'ce_cmake_executions_total',
         help: 'Number of executions after CMake',
         labelNames: ['language'],
diff --git a/lib/temp.ts b/lib/temp.ts
new file mode 100644
index 000000000..53026b4e5
--- /dev/null
+++ b/lib/temp.ts
@@ -0,0 +1,111 @@
+// Copyright (c) 2025, Compiler Explorer Authors
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//     * Redistributions of source code must retain the above copyright notice,
+//       this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above copyright
+//       notice, this list of conditions and the following disclaimer in the
+//       documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import {logger} from './logger.js';
+import * as utils from './utils.js';
+
+const pendingRemoval: string[] = [];
+export type Stats = {
+    numCreated: number;
+    numActive: number;
+    numRemoved: number;
+    numAlreadyGone: number;
+};
+
+const stats = {
+    numCreated: 0,
+    numRemoved: 0,
+    numAlreadyGone: 0,
+};
+
+/**
+ * Get the current stats for temporary directories.
+ */
+export function getStats(): Stats {
+    return {
+        ...stats,
+        numActive: pendingRemoval.length,
+    };
+}
+
+// Reset stats, for tests only.
+export function resetStats() {
+    stats.numCreated = 0;
+    stats.numRemoved = 0;
+    stats.numAlreadyGone = 0;
+}
+
+/**
+ * Create a temporary directory, always in the operating systems' temporary directory.
+ * @param prefix a prefix for the directory name
+ */
+export async function mkdir(prefix: string) {
+    const result = await fs.promises.mkdtemp(path.join(os.tmpdir(), prefix));
+    ++stats.numCreated;
+    pendingRemoval.push(result);
+    return result;
+}
+
+/**
+ * Synchronously create a temporary directory, always in the operating systems' temporary directory.
+ * @param prefix a prefix for the directory name
+ */
+export function mkdirSync(prefix: string) {
+    const result = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
+    ++stats.numCreated;
+    pendingRemoval.push(result);
+    return result;
+}
+
+/**
+ * Remove all temporary directories created by this module.
+ */
+export async function cleanup() {
+    // "Atomically" take a copy of the things to remove and set it to an empty array.
+    const toRemove = pendingRemoval.splice(0, pendingRemoval.length);
+    let numRemoved = 0;
+    let numAlreadyGone = 0;
+    for (const dir of toRemove) {
+        if (!(await utils.dirExists(dir))) {
+            ++stats.numAlreadyGone;
+            ++numAlreadyGone;
+            continue;
+        }
+        try {
+            await fs.promises.rm(dir, {recursive: true, force: true});
+            ++numRemoved;
+            ++stats.numRemoved;
+        } catch (e) {
+            logger.error(`Failed to remove ${dir}: ${e}`);
+        }
+    }
+    logger.debug(`Removed ${numRemoved} (${numAlreadyGone} already gone) of ${toRemove.length} temporary directories`);
+}
+
+process.on('exit', async () => {
+    await cleanup();
+});
diff --git a/package-lock.json b/package-lock.json
index 3d74d8041..4cd4c5c68 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -61,7 +61,6 @@
         "serve-favicon": "^2.5.0",
         "systemd-socket": "0.0.0",
         "tar-stream": "^3.1.7",
-        "temp": "^0.9.4",
         "tom-select": "^2.4.3",
         "tree-kill": "^1.2.2",
         "triple-beam": "^1.4.1",
@@ -5542,6 +5541,7 @@
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
       "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
+      "dev": true,
       "license": "MIT"
     },
     "node_modules/bare-events": {
@@ -5746,16 +5746,6 @@
       "integrity": "sha512-AlcaJBi/pqqJBIQ8U9Mcpc9i8Aqxn88Skv5d+xBX006BY5u8N3mGLHa5Lgppa7L/HfwgwLgZ6NYs+Ag6uUmJRA==",
       "license": "MIT"
     },
-    "node_modules/brace-expansion": {
-      "version": "1.1.11",
-      "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
-      "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
-      "license": "MIT",
-      "dependencies": {
-        "balanced-match": "^1.0.0",
-        "concat-map": "0.0.1"
-      }
-    },
     "node_modules/braces": {
       "version": "3.0.3",
       "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz",
@@ -6403,12 +6393,6 @@
       "integrity": "sha512-VRhuHOLoKYOy4UbilLbUzbYg93XLjv2PncJC50EuTWPA3gaja1UjBsUP/D/9/juV3vQFr6XBEzn9KCAHdUvOHw==",
       "license": "MIT"
     },
-    "node_modules/concat-map": {
-      "version": "0.0.1",
-      "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
-      "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
-      "license": "MIT"
-    },
     "node_modules/constantinople": {
       "version": "4.0.1",
       "resolved": "https://registry.npmjs.org/constantinople/-/constantinople-4.0.1.tgz",
@@ -8021,12 +8005,6 @@
         "node": ">=14.14"
       }
     },
-    "node_modules/fs.realpath": {
-      "version": "1.0.0",
-      "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
-      "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==",
-      "license": "ISC"
-    },
     "node_modules/fsevents": {
       "version": "2.3.3",
       "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
@@ -8147,27 +8125,6 @@
         "assert-plus": "^1.0.0"
       }
     },
-    "node_modules/glob": {
-      "version": "7.2.3",
-      "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz",
-      "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==",
-      "deprecated": "Glob versions prior to v9 are no longer supported",
-      "license": "ISC",
-      "dependencies": {
-        "fs.realpath": "^1.0.0",
-        "inflight": "^1.0.4",
-        "inherits": "2",
-        "minimatch": "^3.1.1",
-        "once": "^1.3.0",
-        "path-is-absolute": "^1.0.0"
-      },
-      "engines": {
-        "node": "*"
-      },
-      "funding": {
-        "url": "https://github.com/sponsors/isaacs"
-      }
-    },
     "node_modules/glob-parent": {
       "version": "6.0.2",
       "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz",
@@ -8553,17 +8510,6 @@
         "node": ">=8"
       }
     },
-    "node_modules/inflight": {
-      "version": "1.0.6",
-      "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
-      "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==",
-      "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.",
-      "license": "ISC",
-      "dependencies": {
-        "once": "^1.3.0",
-        "wrappy": "1"
-      }
-    },
     "node_modules/inherits": {
       "version": "2.0.4",
       "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
@@ -10041,18 +9987,6 @@
         "webpack": "^5.0.0"
       }
     },
-    "node_modules/minimatch": {
-      "version": "3.1.2",
-      "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz",
-      "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==",
-      "license": "ISC",
-      "dependencies": {
-        "brace-expansion": "^1.1.7"
-      },
-      "engines": {
-        "node": "*"
-      }
-    },
     "node_modules/minimist": {
       "version": "1.2.8",
       "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
@@ -10612,15 +10546,6 @@
         "node": ">=8"
       }
     },
-    "node_modules/path-is-absolute": {
-      "version": "1.0.1",
-      "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
-      "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
-      "license": "MIT",
-      "engines": {
-        "node": ">=0.10.0"
-      }
-    },
     "node_modules/path-key": {
       "version": "3.1.1",
       "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
@@ -12009,19 +11934,6 @@
       "dev": true,
       "license": "MIT"
     },
-    "node_modules/rimraf": {
-      "version": "2.6.3",
-      "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.6.3.tgz",
-      "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==",
-      "deprecated": "Rimraf versions prior to v4 are no longer supported",
-      "license": "ISC",
-      "dependencies": {
-        "glob": "^7.1.3"
-      },
-      "bin": {
-        "rimraf": "bin.js"
-      }
-    },
     "node_modules/rollup": {
       "version": "4.34.8",
       "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.34.8.tgz",
@@ -13081,19 +12993,6 @@
         "bintrees": "1.0.2"
       }
     },
-    "node_modules/temp": {
-      "version": "0.9.4",
-      "resolved": "https://registry.npmjs.org/temp/-/temp-0.9.4.tgz",
-      "integrity": "sha512-yYrrsWnrXMcdsnu/7YMYAofM1ktpL5By7vZhf15CrXijWWrEYZks5AXBudalfSWJLlnen/QUJUB5aoB0kqZUGA==",
-      "license": "MIT",
-      "dependencies": {
-        "mkdirp": "^0.5.1",
-        "rimraf": "~2.6.2"
-      },
-      "engines": {
-        "node": ">=6.0.0"
-      }
-    },
     "node_modules/terser": {
       "version": "5.39.0",
       "resolved": "https://registry.npmjs.org/terser/-/terser-5.39.0.tgz",
diff --git a/package.json b/package.json
index 10c6161fa..afea3486b 100644
--- a/package.json
+++ b/package.json
@@ -70,7 +70,6 @@
     "serve-favicon": "^2.5.0",
     "systemd-socket": "0.0.0",
     "tar-stream": "^3.1.7",
-    "temp": "^0.9.4",
     "tom-select": "^2.4.3",
     "tree-kill": "^1.2.2",
     "triple-beam": "^1.4.1",


Test patch:
diff --git a/test/temp-tests.ts b/test/temp-tests.ts
new file mode 100644
index 000000000..15d20b69d
--- /dev/null
+++ b/test/temp-tests.ts
@@ -0,0 +1,79 @@
+// Copyright (c) 2025, Compiler Explorer Authors
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+//     * Redistributions of source code must retain the above copyright notice,
+//       this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above copyright
+//       notice, this list of conditions and the following disclaimer in the
+//       documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+import fs from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+
+import {afterEach, describe, expect, it} from 'vitest';
+import * as temp from '../lib/temp.js';
+import * as utils from '../lib/utils.js';
+
+describe('Creates and tracks temporary directories', () => {
+    const osTemp = os.tmpdir();
+    afterEach(async () => {
+        await temp.cleanup();
+        temp.resetStats();
+    });
+
+    it('creates directories under $TMPDIR', async () => {
+        expect(temp.getStats()).toEqual({numCreated: 0, numActive: 0, numRemoved: 0, numAlreadyGone: 0});
+        const newTemp = await temp.mkdir('prefix');
+        expect(newTemp).toContain(osTemp);
+        expect(await utils.dirExists(newTemp)).toBe(true);
+        expect(temp.getStats()).toEqual({numCreated: 1, numActive: 1, numRemoved: 0, numAlreadyGone: 0});
+    });
+    it('creates directories with prefix', async () => {
+        const newTemp = await temp.mkdir('prefix');
+        expect(newTemp).toContain('prefix');
+    });
+    it('creates uniquely-named directories', async () => {
+        const temp1 = await temp.mkdir('prefix');
+        const temp2 = await temp.mkdir('prefix');
+        const temp3 = await temp.mkdir('prefix');
+        expect(temp1).not.toEqual(temp2);
+        expect(temp1).not.toEqual(temp3);
+        expect(temp2).not.toEqual(temp3);
+        expect(temp.getStats()).toEqual({numCreated: 3, numActive: 3, numRemoved: 0, numAlreadyGone: 0});
+    });
+    it('cleans up directories even if not empty', async () => {
+        const newTemp1 = await temp.mkdir('prefix');
+        await utils.ensureFileExists(path.join(newTemp1, 'some', 'dirs', 'under', 'file'));
+        const newTemp2 = await temp.mkdir('prefix');
+        const newTemp3 = await temp.mkdir('prefix');
+        expect(temp.getStats()).toEqual({numCreated: 3, numActive: 3, numRemoved: 0, numAlreadyGone: 0});
+        await temp.cleanup();
+        expect(temp.getStats()).toEqual({numCreated: 3, numActive: 0, numRemoved: 3, numAlreadyGone: 0});
+        expect(await utils.dirExists(newTemp1)).toBe(false);
+        expect(await utils.dirExists(newTemp2)).toBe(false);
+        expect(await utils.dirExists(newTemp3)).toBe(false);
+    });
+    it('counts already-cleaned-up directiories', async () => {
+        const newTemp = await temp.mkdir('prefix');
+        await fs.rm(newTemp, {recursive: true});
+        expect(temp.getStats()).toEqual({numCreated: 1, numActive: 1, numRemoved: 0, numAlreadyGone: 0});
+        await temp.cleanup();
+        expect(temp.getStats()).toEqual({numCreated: 1, numActive: 0, numRemoved: 0, numAlreadyGone: 1});
+    });
+});
diff --git a/test/utils.ts b/test/utils.ts
index 3f9e9e0ae..955ec0dcb 100644
--- a/test/utils.ts
+++ b/test/utils.ts
@@ -22,12 +22,11 @@
 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 // POSSIBILITY OF SUCH DAMAGE.
 
-import os from 'node:os';
 import path from 'node:path';
 import {fileURLToPath} from 'node:url';
 
-import temp from 'temp';
-import {expect} from 'vitest';
+import {afterEach, expect, onTestFinished} from 'vitest';
+import * as temp from '../lib/temp.js';
 
 import {CompilationEnvironment} from '../lib/compilation-env.js';
 import {CompilationQueue} from '../lib/compilation-queue.js';
@@ -36,8 +35,19 @@ import {CompilerInfo} from '../types/compiler.interfaces.js';
 import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js';
 import {Language} from '../types/languages.interfaces.js';
 
+function ensureTempCleanup() {
+    // Sometimes we're called from inside a test, sometimes from outside. Handle both.
+    afterEach(async () => await temp.cleanup());
+    try {
+        onTestFinished(async () => await temp.cleanup());
+    } catch (_) {
+        // ignore; we weren't in a test body.
+    }
+}
+
 // TODO: Find proper type for options
 export function makeCompilationEnvironment(options: Record<string, any>): CompilationEnvironment {
+    ensureTempCleanup();
     const compilerProps = new CompilerProps(options.languages, fakeProps(options.props || {}));
     const compilationQueue = options.queue || new CompilationQueue(options.concurrency || 1, options.timeout, 100_000);
     return new CompilationEnvironment(compilerProps, fakeProps({}), compilationQueue, options.doCache);
@@ -84,6 +94,6 @@ export function resolvePathFromTestRoot(...args: string[]): string {
 
 // Tracked temporary directories.
 export function newTempDir() {
-    temp.track(true);
-    return temp.mkdirSync({prefix: 'compiler-explorer-tests', dir: os.tmpdir()});
+    ensureTempCleanup();
+    return temp.mkdirSync('compiler-explorer-tests');
 }