@@ -7,6 +7,72 @@ import path from "node:path"
77import { fileURLToPath } from "node:url"
88
99const here = path . dirname ( fileURLToPath ( import . meta. url ) )
10+ export const DIAGNOSTIC_ENV_KEYS = [
11+ "PSExecutionPolicyPreference" ,
12+ "PSModuleAnalysisCachePath" ,
13+ "PSModulePath" ,
14+ "PSHOME" ,
15+ "PSDisableModuleAnalysisCacheCleanup" ,
16+ "__PSLockdownPolicy" ,
17+ "APPDATA" ,
18+ "LOCALAPPDATA" ,
19+ "ProgramData" ,
20+ "ProgramFiles" ,
21+ "ProgramFiles(x86)" ,
22+ "ProgramW6432" ,
23+ "CommonProgramFiles" ,
24+ "CommonProgramFiles(x86)" ,
25+ "CommonProgramW6432" ,
26+ "USERDOMAIN" ,
27+ "USERDOMAIN_ROAMINGPROFILE" ,
28+ "USERDNSDOMAIN" ,
29+ "USERNAME" ,
30+ "LOGONSERVER" ,
31+ "COMPUTERNAME" ,
32+ "ALLUSERSPROFILE" ,
33+ "PUBLIC" ,
34+ "OS" ,
35+ "PROCESSOR_ARCHITECTURE" ,
36+ "PROCESSOR_IDENTIFIER" ,
37+ "PROCESSOR_LEVEL" ,
38+ "PROCESSOR_REVISION" ,
39+ "NUMBER_OF_PROCESSORS" ,
40+ "SystemRoot" ,
41+ "WINDIR" ,
42+ "SYSTEMDRIVE" ,
43+ "HOMEDRIVE" ,
44+ "HOMEPATH" ,
45+ "USERPROFILE" ,
46+ "HOME" ,
47+ "USER" ,
48+ "TEMP" ,
49+ "TMP" ,
50+ "PATH" ,
51+ "COMSPEC" ,
52+ "PATHEXT" ,
53+ "SESSIONNAME" ,
54+ ]
55+
56+ function snapshotPath ( ) {
57+ return path . join ( process . env . RUNNER_TEMP , "opencode-acl-environment.json" )
58+ }
59+
60+ export function captureEnvironment ( ) {
61+ writeFileSync (
62+ snapshotPath ( ) ,
63+ JSON . stringify (
64+ Object . fromEntries ( DIAGNOSTIC_ENV_KEYS . filter ( ( key ) => process . env [ key ] ) . map ( ( key ) => [ key , process . env [ key ] ] ) ) ,
65+ ) ,
66+ { flag : "r+" } ,
67+ )
68+ }
69+
70+ export function snapshotNames ( ) {
71+ return Object . keys ( JSON . parse ( readFileSync ( snapshotPath ( ) , "utf8" ) ) ) . filter ( ( key ) =>
72+ DIAGNOSTIC_ENV_KEYS . includes ( key ) ,
73+ )
74+ }
75+
1076const sources = {
1177 "exit 0" : "exit 0" ,
1278 "Set-Acl" : readFileSync ( path . join ( here , "../profile/src/cor-local-bin-defaults.test.ts" ) , "utf8" ) . match (
@@ -20,11 +86,7 @@ const sources = {
2086function presence ( env = process . env ) {
2187 const names = new Set ( Object . keys ( env ) . map ( ( key ) => key . toUpperCase ( ) ) )
2288 return {
23- ...Object . fromEntries (
24- [ "SYSTEMROOT" , "WINDIR" , "PSMODULEPATH" , "USERPROFILE" , "TEMP" , "TMP" , "PATH" , "LOCALAPPDATA" , "COMSPEC" ] . map (
25- ( key ) => [ key , names . has ( key ) ] ,
26- ) ,
27- ) ,
89+ ...Object . fromEntries ( DIAGNOSTIC_ENV_KEYS . map ( ( key ) => [ key . toUpperCase ( ) , names . has ( key . toUpperCase ( ) ) ] ) ) ,
2890 SystemRootLookup : Boolean ( env . SystemRoot ) ,
2991 SYSTEMROOTLookup : Boolean ( env . SYSTEMROOT ) ,
3092 }
@@ -39,13 +101,13 @@ function probe(stage, restore = "none") {
39101 LOCALAPPDATA : [ "LOCALAPPDATA" ] ,
40102 both : [ "PSModulePath" , "LOCALAPPDATA" ] ,
41103 }
42- if ( ! Object . hasOwn ( choices , restore ) ) throw new Error ( "Unknown ACL environment factor" )
104+ const keys = Array . isArray ( restore ) ? restore : Object . hasOwn ( choices , restore ) ? choices [ restore ] : undefined
105+ if ( ! keys || ! keys . every ( ( key ) => DIAGNOSTIC_ENV_KEYS . includes ( key ) ) )
106+ throw new Error ( "Unknown ACL environment factor" )
43107 const env = { ...process . env }
44- if ( restore !== "none" ) {
45- const snapshot = JSON . parse (
46- readFileSync ( path . join ( process . env . RUNNER_TEMP , "opencode-acl-environment.json" ) , "utf8" ) ,
47- )
48- for ( const key of choices [ restore ] ) {
108+ if ( keys . length ) {
109+ const snapshot = JSON . parse ( readFileSync ( snapshotPath ( ) , "utf8" ) )
110+ for ( const key of keys ) {
49111 if ( typeof snapshot [ key ] !== "string" || ! snapshot [ key ] ) throw new Error ( "ACL environment factor unavailable" )
50112 env [ key ] = snapshot [ key ]
51113 }
@@ -93,7 +155,7 @@ function probe(stage, restore = "none") {
93155
94156export function diagnose ( stage , transport , restore = "none" ) {
95157 if ( transport === "bun" ) return probe ( stage , restore )
96- const result = spawnSync ( "node" , [ fileURLToPath ( import . meta. url ) , stage , restore ] , {
158+ const result = spawnSync ( "node" , [ fileURLToPath ( import . meta. url ) , stage , JSON . stringify ( restore ) ] , {
97159 env : process . env ,
98160 encoding : "utf8" ,
99161 stdio : [ "ignore" , "pipe" , "pipe" ] ,
@@ -119,5 +181,5 @@ export function diagnose(stage, transport, restore = "none") {
119181if ( process . argv [ 1 ] && path . resolve ( process . argv [ 1 ] ) === fileURLToPath ( import . meta. url ) ) {
120182 if ( process . platform !== "win32" || process . versions . node . split ( "." ) [ 0 ] !== "24" )
121183 throw new Error ( "ACL diagnostic child requires Windows Node 24" )
122- console . log ( JSON . stringify ( probe ( process . argv [ 2 ] , process . argv [ 3 ] ) ) )
184+ console . log ( JSON . stringify ( probe ( process . argv [ 2 ] , process . argv [ 3 ] ? JSON . parse ( process . argv [ 3 ] ) : "none" ) ) )
123185}
0 commit comments