Skip to content

Commit 214c8ed

Browse files
authored
fix(cherry-pick): entra and oauth vs DA template local debug failed (#14253)
* fix: entra and oauth vs DA template local debug failed * fix: typo --------- Co-authored-by: rentu <rentu>
1 parent 074140c commit 214c8ed

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed

packages/fx-core/src/common/kiotaClient.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,27 @@ import {
1515
import { KiotaGeneratePluginError } from "../error";
1616
import { getLocalizedString } from "./localizeUtils";
1717
import { Utils } from "@microsoft/m365-spec-parser";
18+
import path from "path";
19+
import * as os from "os";
1820

1921
const ERROR_LOG_LEVEL = 4;
2022

21-
export async function searchOpenAPISpec(query: string): Promise<SearchOpenAPISpecResult[]> {
23+
function setKiotaBinaryPath() {
2224
if (process.env.KIOTA_BINARY_PATH) {
2325
setKiotaConfig({ binaryLocation: process.env.KIOTA_BINARY_PATH });
26+
} else {
27+
// If running inside pkg package used by VS, set the binary location to a specific directory to avoid issues.
28+
const isInsidePkg = typeof (process as any).pkg !== "undefined";
29+
30+
if (isInsidePkg) {
31+
const kiotaBinDir = path.join(os.homedir(), "kiota-bin");
32+
setKiotaConfig({ binaryLocation: kiotaBinDir });
33+
}
2434
}
35+
}
36+
37+
export async function searchOpenAPISpec(query: string): Promise<SearchOpenAPISpecResult[]> {
38+
setKiotaBinaryPath();
2539

2640
const searchResult: Record<string, KiotaSearchResultItem> | undefined = await searchDescription({
2741
searchTerm: query,
@@ -51,9 +65,7 @@ export async function listAPITreeInfo(
5165
includeFilters?: string[],
5266
excludeFilters?: string[]
5367
): Promise<KiotaTreeResult> {
54-
if (process.env.KIOTA_BINARY_PATH) {
55-
setKiotaConfig({ binaryLocation: process.env.KIOTA_BINARY_PATH });
56-
}
68+
setKiotaBinaryPath();
5769
const treeInfo = await getKiotaTree({
5870
includeFilters: includeFilters,
5971
descriptionPath: specPath,
@@ -90,9 +102,7 @@ export async function kiotageneratePlugin(
90102
excludePatterns?: string[],
91103
noWorkspace?: boolean
92104
): Promise<GeneratePluginResult> {
93-
if (process.env.KIOTA_BINARY_PATH) {
94-
setKiotaConfig({ binaryLocation: process.env.KIOTA_BINARY_PATH });
95-
}
105+
setKiotaBinaryPath();
96106

97107
const config = {
98108
descriptionPath: specPath,

packages/fx-core/tests/common/kiotaClient.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,114 @@ describe("kiotaClient", () => {
1616
sandbox.restore();
1717
});
1818

19+
describe("setKiotaBinaryPath", () => {
20+
let originalPkg: any;
21+
22+
beforeEach(() => {
23+
originalPkg = (process as any).pkg;
24+
});
25+
26+
afterEach(() => {
27+
if (originalPkg !== undefined) {
28+
(process as any).pkg = originalPkg;
29+
} else {
30+
delete (process as any).pkg;
31+
}
32+
delete process.env.KIOTA_BINARY_PATH;
33+
});
34+
35+
it("should set binary location from KIOTA_BINARY_PATH environment variable", async () => {
36+
process.env.KIOTA_BINARY_PATH = "/custom/path/to/kiota";
37+
delete (process as any).pkg;
38+
39+
const setKiotaConfigStub = sinon.stub().resolves();
40+
const searchDescriptionStub = sinon.stub().resolves({});
41+
42+
const { searchOpenAPISpec } = proxyquire("../../src/common/kiotaClient", {
43+
"@microsoft/kiota": {
44+
setKiotaConfig: setKiotaConfigStub,
45+
searchDescription: searchDescriptionStub,
46+
"@noCallThru": true,
47+
},
48+
});
49+
50+
await searchOpenAPISpec("test-query");
51+
52+
assert(setKiotaConfigStub.calledOnce);
53+
assert(setKiotaConfigStub.calledWith({ binaryLocation: "/custom/path/to/kiota" }));
54+
});
55+
56+
it("should set binary location to kiota-bin directory when running inside pkg", async () => {
57+
delete process.env.KIOTA_BINARY_PATH;
58+
(process as any).pkg = {};
59+
60+
const setKiotaConfigStub = sinon.stub().resolves();
61+
const searchDescriptionStub = sinon.stub().resolves({});
62+
63+
const { searchOpenAPISpec } = proxyquire("../../src/common/kiotaClient", {
64+
"@microsoft/kiota": {
65+
setKiotaConfig: setKiotaConfigStub,
66+
searchDescription: searchDescriptionStub,
67+
"@noCallThru": true,
68+
},
69+
path: {
70+
join: sinon.stub().returns("/home/user/kiota-bin"),
71+
"@noCallThru": true,
72+
},
73+
os: {
74+
homedir: sinon.stub().returns("/home/user"),
75+
"@noCallThru": true,
76+
},
77+
});
78+
79+
await searchOpenAPISpec("test-query");
80+
81+
assert(setKiotaConfigStub.calledOnce);
82+
assert(setKiotaConfigStub.calledWith({ binaryLocation: "/home/user/kiota-bin" }));
83+
});
84+
85+
it("should not call setKiotaConfig when not in pkg and no env var set", async () => {
86+
delete process.env.KIOTA_BINARY_PATH;
87+
delete (process as any).pkg;
88+
89+
const setKiotaConfigStub = sinon.stub().resolves();
90+
const searchDescriptionStub = sinon.stub().resolves({});
91+
92+
const { searchOpenAPISpec } = proxyquire("../../src/common/kiotaClient", {
93+
"@microsoft/kiota": {
94+
setKiotaConfig: setKiotaConfigStub,
95+
searchDescription: searchDescriptionStub,
96+
"@noCallThru": true,
97+
},
98+
});
99+
100+
await searchOpenAPISpec("test-query");
101+
102+
assert(setKiotaConfigStub.notCalled);
103+
});
104+
105+
it("should prioritize KIOTA_BINARY_PATH over pkg detection", async () => {
106+
process.env.KIOTA_BINARY_PATH = "/env/path/to/kiota";
107+
(process as any).pkg = {};
108+
109+
const setKiotaConfigStub = sinon.stub().resolves();
110+
const searchDescriptionStub = sinon.stub().resolves({});
111+
112+
const { searchOpenAPISpec } = proxyquire("../../src/common/kiotaClient", {
113+
"@microsoft/kiota": {
114+
setKiotaConfig: setKiotaConfigStub,
115+
searchDescription: searchDescriptionStub,
116+
"@noCallThru": true,
117+
},
118+
});
119+
120+
await searchOpenAPISpec("test-query");
121+
122+
assert(setKiotaConfigStub.calledOnce);
123+
assert(setKiotaConfigStub.calledWith({ binaryLocation: "/env/path/to/kiota" }));
124+
});
125+
});
126+
19127
it("happy path: searchOpenAPISpec", async () => {
20128
const mockSearchResult = {
21129
"api-spec": {

0 commit comments

Comments
 (0)