Skip to content

Commit 5216eb5

Browse files
committed
fix(plugin-lighthouse): prevent cleanup permissions error on windows
1 parent 38b04e4 commit 5216eb5

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

packages/plugin-lighthouse/src/lib/runner/runner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import {
1212
getConfig,
1313
normalizeAuditOutputs,
1414
toAuditOutputs,
15+
withLocalTmpDir,
1516
} from './utils.js';
1617

1718
export function createRunnerFunction(
1819
urls: string[],
1920
flags: LighthouseCliFlags = DEFAULT_CLI_FLAGS,
2021
): RunnerFunction {
21-
return async (): Promise<AuditOutputs> => {
22+
return withLocalTmpDir(async (): Promise<AuditOutputs> => {
2223
const config = await getConfig(flags);
2324
const normalizationFlags = enrichFlags(flags);
2425
const isSingleUrl = !shouldExpandForUrls(urls.length);
@@ -58,7 +59,7 @@ export function createRunnerFunction(
5859
);
5960
}
6061
return normalizeAuditOutputs(allResults, normalizationFlags);
61-
};
62+
});
6263
}
6364

6465
async function runLighthouseForUrl(

packages/plugin-lighthouse/src/lib/runner/utils.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import experimentalConfig from 'lighthouse/core/config/experimental-config.js';
66
import perfConfig from 'lighthouse/core/config/perf-config.js';
77
import type Details from 'lighthouse/types/lhr/audit-details';
88
import type { Result } from 'lighthouse/types/lhr/audit-result';
9+
import { platform } from 'node:os';
10+
import path from 'node:path';
911
import type { AuditOutput, AuditOutputs } from '@code-pushup/models';
1012
import {
1113
formatReportScore,
1214
importModule,
15+
pluginWorkDir,
1316
readJsonFile,
1417
ui,
1518
} from '@code-pushup/utils';
19+
import { LIGHTHOUSE_PLUGIN_SLUG } from '../constants.js';
1620
import type { LighthouseOptions } from '../types.js';
1721
import { logUnsupportedDetails, toAuditDetails } from './details/details.js';
1822
import type { LighthouseCliFlags } from './types.js';
@@ -167,3 +171,31 @@ export function enrichFlags(
167171
outputPath: urlSpecificOutputPath,
168172
};
169173
}
174+
175+
/**
176+
* Wraps Lighthouse runner with `TEMP` directory override for Windows, to prevent permissions error on cleanup.
177+
*
178+
* `Runtime error encountered: EPERM, Permission denied: \\?\C:\Users\RUNNER~1\AppData\Local\Temp\lighthouse.57724617 '\\?\C:\Users\RUNNER~1\AppData\Local\Temp\lighthouse.57724617'`
179+
*
180+
* @param fn Async function which runs Lighthouse.
181+
* @returns Wrapped function which overrides `TEMP` environment variable, before cleaning up afterwards.
182+
*/
183+
export function withLocalTmpDir<T>(fn: () => Promise<T>): () => Promise<T> {
184+
if (platform() !== 'win32') {
185+
return fn;
186+
}
187+
188+
return async () => {
189+
const originalTmpDir = process.env['TEMP'];
190+
process.env['TEMP'] = path.join(
191+
pluginWorkDir(LIGHTHOUSE_PLUGIN_SLUG),
192+
'tmp',
193+
);
194+
195+
try {
196+
return await fn();
197+
} finally {
198+
process.env['TEMP'] = originalTmpDir;
199+
}
200+
};
201+
}

0 commit comments

Comments
 (0)