The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
fix(server): run the Azure CLI in UTF-8 on Windows (#25801)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Pedro Henrique committed
Nov 13, 2026 at 22:08 UTC
ca1a0280dac8ad76a68e362e4fcc1ef6afd39fa3
4 files changed
+176
-2
packages/source-control-azure-devops/src/server/AzureDevOpsCli.test.ts
+50
@@ -9,6 +9,9 @@ import { VcsProcessExitError, VcsProcessSpawnError } from "@t3tools/contracts";
9
10
import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
11
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
12
+import * as HostProcess from "@t3tools/shared/HostProcess";
13
+import { SpawnExecutableResolution } from "@t3tools/shared/shell";
14
+import { AzureCliInstallLocation, type AzureCliInstall } from "./azureCliLauncher.ts";
15
import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
16
17
const processOutput = (stdout: string): SourceControlHost.SourceControlProcessOutput => ({
@@ -21,9 +24,11 @@ const processOutput = (stdout: string): SourceControlHost.SourceControlProcessOu
24
25
const mockRun = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>();
26
27
+// The CLI layer is exercised on a host that spawns `az` itself, so the launcher upgrade stays out of these assertions.
28
const layerSupport = Layer.mergeAll(
29
TestSourceControlHost.layer({ process: { run: mockRun } }),
30
NodeServices.layer,
31
+ Layer.succeed(HostProcess.Platform, "linux"),
32
);
33
const layer = Layer.mergeAll(AzureDevOpsCli.layer.pipe(Layer.provide(layerSupport)), layerSupport);
34
@@ -345,3 +350,48 @@ describe("AzureDevOpsCli.layer", () => {
350
}).pipe(Effect.provide(layer)),
351
);
352
});
353
+
354
+describe("azureCliInvocation", () => {
355
+ const launcher = "C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd";
356
+ const msi: AzureCliInstall = {
357
+ interpreter: "C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2\\python.exe",
358
+ installer: "MSI",
359
+ };
360
+
361
+ const invocationFor = (install: AzureCliInstall | undefined) =>
362
+ AzureDevOpsCli.azureCliInvocation().pipe(
363
+ Effect.provideService(HostProcess.Platform, "win32"),
364
+ Effect.provideService(HostProcess.Environment, { PATH: "C:\\tools" }),
365
+ Effect.provideService(SpawnExecutableResolution, () => launcher),
366
+ Effect.provideService(AzureCliInstallLocation, () => install),
367
+ );
368
+
369
+ it.effect("runs the install's interpreter in UTF-8 mode", () =>
370
+ Effect.gen(function* () {
371
+ assert.deepStrictEqual(yield* invocationFor(msi), {
372
+ command: msi.interpreter,
373
+ args: ["-X", "utf8", "-IBm", "azure.cli"],
374
+ env: { AZ_INSTALLER: "MSI" },
375
+ });
376
+ }),
377
+ );
378
+
379
+ it.effect("keeps the launcher when the install is not recognized", () =>
380
+ Effect.gen(function* () {
381
+ assert.deepStrictEqual(yield* invocationFor(undefined), {
382
+ command: "az",
383
+ args: [],
384
+ });
385
+ }),
386
+ );
387
+
388
+ it.effect("spawns az directly off Windows", () =>
389
+ Effect.gen(function* () {
390
+ const invocation = yield* AzureDevOpsCli.azureCliInvocation().pipe(
391
+ Effect.provideService(HostProcess.Platform, "linux"),
392
+ );
393
+
394
+ assert.deepStrictEqual(invocation, { command: "az", args: [] });
395
+ }),
396
+ );
397
+});
packages/source-control-azure-devops/src/server/AzureDevOpsCli.ts
+38
-2
@@ -13,6 +13,9 @@ import {
13
} from "@t3tools/contracts";
14
15
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
16
+import * as HostProcess from "@t3tools/shared/HostProcess";
17
+import { SpawnExecutableResolution } from "@t3tools/shared/shell";
18
+import { AzureCliInstallLocation } from "./azureCliLauncher.ts";
19
import {
20
decodeAzureDevOpsPullRequestJson,
21
decodeAzureDevOpsPullRequestListJson,
@@ -365,17 +368,50 @@ const classifyAzFailure = (stderr: string): VcsProcessExitFailureKind | null =>
368
: null;
369
};
370
371
+export interface AzureCliInvocation {
372
+ readonly command: string;
373
+ /** Arguments `az` needs before the caller's, to run the CLI in UTF-8 mode. */
374
+ readonly args: ReadonlyArray<string>;
375
+ readonly env?: NodeJS.ProcessEnv;
376
+}
377
+
378
+/**
379
+ * `-X utf8` is the only way in: the launcher's `-I` implies `-E`, so `PYTHONUTF8` and
380
+ * `PYTHONIOENCODING` are ignored, and the ANSI code page loses text `az` cannot encode.
381
+ * Resolved once when the service is built: installing `az` later needs a restart.
382
+ */
383
+export const azureCliInvocation = Effect.fn("AzureDevOpsCli.invocation")(function* () {
384
+ if ((yield* HostProcess.Platform) !== "win32") {
385
+ return { command: "az", args: [] } satisfies AzureCliInvocation;
386
+ }
387
+ const resolveExecutable = yield* SpawnExecutableResolution;
388
+ const launcher = resolveExecutable("az", "win32", yield* HostProcess.Environment);
389
+ const installFor = yield* AzureCliInstallLocation;
390
+ const install = launcher === undefined ? undefined : installFor(launcher);
391
+ // Unrecognized installs (pipx's Scripts\az.exe) keep the launcher and today's decode.
392
+ return install === undefined
393
+ ? ({ command: "az", args: [] } satisfies AzureCliInvocation)
394
+ : ({
395
+ command: install.interpreter,
396
+ args: ["-X", "utf8", "-IBm", "azure.cli"],
397
+ // What the launcher sets before running this same interpreter.
398
+ env: { AZ_INSTALLER: install.installer },
399
+ } satisfies AzureCliInvocation);
400
+});
401
+
402
/** @public */
403
export const make = Effect.gen(function* () {
404
const { process } = yield* SourceControlHost.SourceControlHost;
405
+ const invocation = yield* azureCliInvocation();
406
407
const execute: AzureDevOpsCli["Service"]["execute"] = (input) =>
408
process
409
.run({
410
operation: "AzureDevOpsCli.execute",
376
- command: "az",
377
- args: input.args,
411
+ command: invocation.command,
412
+ args: [...invocation.args, ...input.args],
413
cwd: input.cwd,
414
+ ...(invocation.env === undefined ? {} : { env: invocation.env }),
415
classifyFailure: classifyAzFailure,
416
notFoundDetail: "Pull request not found.",
417
timeoutMs: input.timeoutMs ?? DEFAULT_TIMEOUT_MS,
packages/source-control-azure-devops/src/server/azureCliLauncher.test.ts
new
+43
@@ -0,0 +1,43 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+
3
+import { azureCliInstall } from "./azureCliLauncher.ts";
4
+
5
+describe("azureCliInstall", () => {
6
+ const msiRoot = "C:\\Program Files\\Microsoft SDKs\\Azure\\CLI2";
7
+ const zipRoot = "D:\\azure-cli";
8
+ const cliPackage = (root: string) => `${root}\\Lib\\site-packages\\azure\\cli`;
9
+
10
+ const paths =
11
+ (...entries: Array<string>) =>
12
+ (filePath: string) =>
13
+ entries.includes(filePath);
14
+
15
+ it("runs the MSI launcher's interpreter", () => {
16
+ expect(
17
+ azureCliInstall(
18
+ `${msiRoot}\\wbin\\az.cmd`,
19
+ paths(`${msiRoot}\\python.exe`, cliPackage(msiRoot)),
20
+ ),
21
+ ).toEqual({ interpreter: `${msiRoot}\\python.exe`, installer: "MSI" });
22
+ });
23
+
24
+ it("runs the ZIP launcher's interpreter", () => {
25
+ expect(
26
+ azureCliInstall(
27
+ `${zipRoot}\\bin\\az.cmd`,
28
+ paths(`${zipRoot}\\python.exe`, cliPackage(zipRoot)),
29
+ ),
30
+ ).toEqual({ interpreter: `${zipRoot}\\python.exe`, installer: "ZIP" });
31
+ });
32
+
33
+ it("leaves anything that is not the Azure CLI alone", () => {
34
+ expect(azureCliInstall(`${zipRoot}\\bin\\az.exe`, () => true)).toBeUndefined();
35
+ expect(azureCliInstall(`${zipRoot}\\azure-cli\\az.cmd`, () => true)).toBeUndefined();
36
+ // Another tool's `bin\az.cmd` beside a `python.exe`, without the CLI's own packages.
37
+ expect(
38
+ azureCliInstall(`${zipRoot}\\bin\\az.cmd`, paths(`${zipRoot}\\python.exe`)),
39
+ ).toBeUndefined();
40
+ // The CLI's packages without the interpreter its launcher runs.
41
+ expect(azureCliInstall(`${msiRoot}\\wbin\\az.cmd`, paths(cliPackage(msiRoot)))).toBeUndefined();
42
+ });
43
+});
packages/source-control-azure-devops/src/server/azureCliLauncher.ts
new
+45
@@ -0,0 +1,45 @@
1
+// @effect-diagnostics nodeBuiltinImport:off -- The interpreter sits beside the launcher it belongs to.
2
+import * as Context from "effect/Context";
3
+import * as NodeFS from "node:fs";
4
+import * as NodePath from "node:path";
5
+
6
+/** The `AZ_INSTALLER` value each launcher sets, which `az` only reads for its user agent. */
7
+export type AzureCliInstaller = "MSI" | "ZIP";
8
+
9
+export interface AzureCliInstall {
10
+ /** The interpreter the launcher runs: `<install>\python.exe -IBm azure.cli`. */
11
+ readonly interpreter: string;
12
+ readonly installer: AzureCliInstaller;
13
+}
14
+
15
+const INSTALLERS_BY_LAUNCHER_DIRECTORY: Readonly<Record<string, AzureCliInstaller>> = {
16
+ wbin: "MSI",
17
+ bin: "ZIP",
18
+};
19
+
20
+/** The MSI and ZIP builds both launch the interpreter beside the install root, from `wbin` and `bin`. */
21
+export const azureCliInstall = (
22
+ launcherPath: string,
23
+ pathExists: (filePath: string) => boolean = NodeFS.existsSync,
24
+): AzureCliInstall | undefined => {
25
+ const path = NodePath.win32;
26
+ const extension = path.extname(launcherPath).toLowerCase();
27
+ if (extension !== ".cmd" && extension !== ".bat") return undefined;
28
+ const launcherDirectory = path.dirname(launcherPath);
29
+ const installer =
30
+ INSTALLERS_BY_LAUNCHER_DIRECTORY[path.basename(launcherDirectory).toLowerCase()];
31
+ if (installer === undefined) return undefined;
32
+ const install = path.dirname(launcherDirectory);
33
+ // Another tool's `bin\az.cmd` beside a `python.exe` is not the CLI; the CLI ships its own packages.
34
+ const cliPackage = path.join(install, "Lib", "site-packages", "azure", "cli");
35
+ if (!pathExists(cliPackage)) return undefined;
36
+ const interpreter = path.join(install, "python.exe");
37
+ return pathExists(interpreter) ? { interpreter, installer } : undefined;
38
+};
39
+
40
+/** The launcher path comes from the host's PATH, so this reads the Windows layout it names. */
41
+export const AzureCliInstallLocation = Context.Reference<
42
+ (launcherPath: string) => AzureCliInstall | undefined
43
+>("@t3tools/source-control-azure-devops/azureCliInstallLocation", {
44
+ defaultValue: () => azureCliInstall,
45
+});