@@ -6,13 +6,17 @@ import experimentalConfig from 'lighthouse/core/config/experimental-config.js';
66import perfConfig from 'lighthouse/core/config/perf-config.js' ;
77import type Details from 'lighthouse/types/lhr/audit-details' ;
88import type { Result } from 'lighthouse/types/lhr/audit-result' ;
9+ import { platform } from 'node:os' ;
10+ import path from 'node:path' ;
911import type { AuditOutput , AuditOutputs } from '@code-pushup/models' ;
1012import {
1113 formatReportScore ,
1214 importModule ,
15+ pluginWorkDir ,
1316 readJsonFile ,
1417 ui ,
1518} from '@code-pushup/utils' ;
19+ import { LIGHTHOUSE_PLUGIN_SLUG } from '../constants.js' ;
1620import type { LighthouseOptions } from '../types.js' ;
1721import { logUnsupportedDetails , toAuditDetails } from './details/details.js' ;
1822import 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