| ... | @@ -3,6 +3,7 @@ import * as Duration from "effect/Duration"; | ... | @@ -3,6 +3,7 @@ import * as Duration from "effect/Duration"; |
| 3 | import * as Effect from "effect/Effect"; | 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; | 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Option from "effect/Option"; | 5 | import * as Option from "effect/Option"; |
| | 6 | import * as Schema from "effect/Schema"; |
| 6 | import * as ChildProcess from "effect/unstable/process/ChildProcess"; | 7 | import * as ChildProcess from "effect/unstable/process/ChildProcess"; |
| 7 | import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; | 8 | import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; |
| 8 | | 9 | |
| ... | @@ -20,6 +21,44 @@ interface WindowsProbeOptions { | ... | @@ -20,6 +21,44 @@ interface WindowsProbeOptions { |
| 20 | readonly loadProfile: boolean; | 21 | readonly loadProfile: boolean; |
| 21 | } | 22 | } |
| 22 | | 23 | |
| | 24 | const DesktopShellEnvironmentProbe = Schema.Literals([ |
| | 25 | "login-shell", |
| | 26 | "launchctl-path", |
| | 27 | "powershell-profile", |
| | 28 | "powershell-no-profile", |
| | 29 | ]); |
| | 30 | type DesktopShellEnvironmentProbe = typeof DesktopShellEnvironmentProbe.Type; |
| | 31 | |
| | 32 | const desktopShellEnvironmentCommandFields = { |
| | 33 | probe: DesktopShellEnvironmentProbe, |
| | 34 | executable: Schema.String, |
| | 35 | argumentCount: Schema.Number, |
| | 36 | }; |
| | 37 | |
| | 38 | export class DesktopShellEnvironmentCommandError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandError>()( |
| | 39 | "DesktopShellEnvironmentCommandError", |
| | 40 | { |
| | 41 | ...desktopShellEnvironmentCommandFields, |
| | 42 | cause: Schema.Defect(), |
| | 43 | }, |
| | 44 | ) { |
| | 45 | override get message(): string { |
| | 46 | return `Desktop shell environment ${this.probe} probe (${this.executable}) failed.`; |
| | 47 | } |
| | 48 | } |
| | 49 | |
| | 50 | export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErrorClass<DesktopShellEnvironmentCommandTimeoutError>()( |
| | 51 | "DesktopShellEnvironmentCommandTimeoutError", |
| | 52 | { |
| | 53 | ...desktopShellEnvironmentCommandFields, |
| | 54 | timeoutMs: Schema.Number, |
| | 55 | }, |
| | 56 | ) { |
| | 57 | override get message(): string { |
| | 58 | return `Desktop shell environment ${this.probe} probe (${this.executable}) timed out after ${this.timeoutMs}ms.`; |
| | 59 | } |
| | 60 | } |
| | 61 | |
| 23 | export class DesktopShellEnvironment extends Context.Service< | 62 | export class DesktopShellEnvironment extends Context.Service< |
| 24 | DesktopShellEnvironment, | 63 | DesktopShellEnvironment, |
| 25 | { | 64 | { |
| ... | @@ -127,6 +166,18 @@ const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray<string> => [ | ... | @@ -127,6 +166,18 @@ const knownWindowsCliDirs = (env: NodeJS.ProcessEnv): ReadonlyArray<string> => [ |
| 127 | const startMarker = (name: string) => `__T3CODE_ENV_${name}_START__`; | 166 | const startMarker = (name: string) => `__T3CODE_ENV_${name}_START__`; |
| 128 | const endMarker = (name: string) => `__T3CODE_ENV_${name}_END__`; | 167 | const endMarker = (name: string) => `__T3CODE_ENV_${name}_END__`; |
| 129 | | 168 | |
| | 169 | const executableName = (command: string): string => command.split(/[\\/]/u).at(-1) ?? command; |
| | 170 | |
| | 171 | const logShellEnvironmentCommandError = ( |
| | 172 | error: DesktopShellEnvironmentCommandError | DesktopShellEnvironmentCommandTimeoutError, |
| | 173 | ) => |
| | 174 | Effect.logWarning(error).pipe( |
| | 175 | Effect.annotateLogs({ |
| | 176 | component: "desktop-shell-environment", |
| | 177 | error, |
| | 178 | }), |
| | 179 | ); |
| | 180 | |
| 130 | const capturePosixEnvironmentCommand = (names: ReadonlyArray<string>) => | 181 | const capturePosixEnvironmentCommand = (names: ReadonlyArray<string>) => |
| 131 | names | 182 | names |
| 132 | .map((name) => { | 183 | .map((name) => { |
| ... | @@ -175,13 +226,14 @@ const extractEnvironment = (output: string, names: ReadonlyArray<string>): Envir | ... | @@ -175,13 +226,14 @@ const extractEnvironment = (output: string, names: ReadonlyArray<string>): Envir |
| 175 | }; | 226 | }; |
| 176 | | 227 | |
| 177 | const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(function* (input: { | 228 | const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")(function* (input: { |
| | 229 | readonly probe: DesktopShellEnvironmentProbe; |
| 178 | readonly command: string; | 230 | readonly command: string; |
| 179 | readonly args: ReadonlyArray<string>; | 231 | readonly args: ReadonlyArray<string>; |
| 180 | readonly timeout: Duration.Duration; | 232 | readonly timeout: Duration.Duration; |
| 181 | readonly shell?: boolean; | 233 | readonly shell?: boolean; |
| 182 | }): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> { | 234 | }): Effect.fn.Return<string, never, ChildProcessSpawner.ChildProcessSpawner> { |
| 183 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | 235 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 184 | return yield* spawner | 236 | const output = yield* spawner |
| 185 | .string( | 237 | .string( |
| 186 | ChildProcess.make(input.command, input.args, { | 238 | ChildProcess.make(input.command, input.args, { |
| 187 | shell: input.shell ?? false, | 239 | shell: input.shell ?? false, |
| ... | @@ -193,10 +245,33 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")( | ... | @@ -193,10 +245,33 @@ const runCommandOutput = Effect.fn("desktop.shellEnvironment.runCommandOutput")( |
| 193 | }), | 245 | }), |
| 194 | ) | 246 | ) |
| 195 | .pipe( | 247 | .pipe( |
| | 248 | Effect.mapError( |
| | 249 | (cause) => |
| | 250 | new DesktopShellEnvironmentCommandError({ |
| | 251 | probe: input.probe, |
| | 252 | executable: executableName(input.command), |
| | 253 | argumentCount: input.args.length, |
| | 254 | cause, |
| | 255 | }), |
| | 256 | ), |
| | 257 | Effect.catchTags({ |
| | 258 | DesktopShellEnvironmentCommandError: (error) => |
| | 259 | logShellEnvironmentCommandError(error).pipe(Effect.as("")), |
| | 260 | }), |
| 196 | Effect.timeoutOption(input.timeout), | 261 | Effect.timeoutOption(input.timeout), |
| 197 | Effect.map(Option.getOrElse(() => "")), | | |
| 198 | Effect.orElseSucceed(() => ""), | | |
| 199 | ); | 262 | ); |
| | 263 | if (Option.isSome(output)) { |
| | 264 | return output.value; |
| | 265 | } |
| | 266 | |
| | 267 | const error = new DesktopShellEnvironmentCommandTimeoutError({ |
| | 268 | probe: input.probe, |
| | 269 | executable: executableName(input.command), |
| | 270 | argumentCount: input.args.length, |
| | 271 | timeoutMs: Duration.toMillis(input.timeout), |
| | 272 | }); |
| | 273 | yield* logShellEnvironmentCommandError(error); |
| | 274 | return ""; |
| 200 | }); | 275 | }); |
| 201 | | 276 | |
| 202 | const readLoginShellEnvironment = ( | 277 | const readLoginShellEnvironment = ( |
| ... | @@ -206,12 +281,14 @@ const readLoginShellEnvironment = ( | ... | @@ -206,12 +281,14 @@ const readLoginShellEnvironment = ( |
| 206 | names.length === 0 | 281 | names.length === 0 |
| 207 | ? Effect.succeed({}) | 282 | ? Effect.succeed({}) |
| 208 | : runCommandOutput({ | 283 | : runCommandOutput({ |
| | 284 | probe: "login-shell", |
| 209 | command: shell, | 285 | command: shell, |
| 210 | args: ["-ilc", capturePosixEnvironmentCommand(names)], | 286 | args: ["-ilc", capturePosixEnvironmentCommand(names)], |
| 211 | timeout: LOGIN_SHELL_TIMEOUT, | 287 | timeout: LOGIN_SHELL_TIMEOUT, |
| 212 | }).pipe(Effect.map((output) => extractEnvironment(output, names))); | 288 | }).pipe(Effect.map((output) => extractEnvironment(output, names))); |
| 213 | | 289 | |
| 214 | const readLaunchctlPath = runCommandOutput({ | 290 | const readLaunchctlPath = runCommandOutput({ |
| | 291 | probe: "launchctl-path", |
| 215 | command: "/bin/launchctl", | 292 | command: "/bin/launchctl", |
| 216 | args: ["getenv", "PATH"], | 293 | args: ["getenv", "PATH"], |
| 217 | timeout: LAUNCHCTL_TIMEOUT, | 294 | timeout: LAUNCHCTL_TIMEOUT, |
| ... | @@ -234,6 +311,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn | ... | @@ -234,6 +311,7 @@ const readWindowsEnvironment = Effect.fn("desktop.shellEnvironment.readWindowsEn |
| 234 | | 311 | |
| 235 | for (const command of WINDOWS_SHELL_CANDIDATES) { | 312 | for (const command of WINDOWS_SHELL_CANDIDATES) { |
| 236 | const output = yield* runCommandOutput({ | 313 | const output = yield* runCommandOutput({ |
| | 314 | probe: options.loadProfile ? "powershell-profile" : "powershell-no-profile", |
| 237 | command, | 315 | command, |
| 238 | args, | 316 | args, |
| 239 | timeout: LOGIN_SHELL_TIMEOUT, | 317 | timeout: LOGIN_SHELL_TIMEOUT, |