Skip to content

Commit 3da65ac

Browse files
Update BuildFlags.buildDirectoryFromWorkspacePath method for scratch-path (#2032)
* update BuildFlags.buildDirectoryFromWorkspacePath method for scratch path * update validations for buildpath
1 parent 0ac69d8 commit 3da65ac

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

src/toolchain/BuildFlags.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ export class BuildFlags {
123123
}
124124
}
125125

126+
/**
127+
* Extract scratch-path or build-path value from an array of arguments
128+
* @param args Array of command-line arguments to search
129+
* @returns The path value if found, otherwise undefined
130+
*/
131+
private static extractScratchPath(args: string[]): string | undefined {
132+
for (let i = 0; i < args.length; i++) {
133+
const arg = args[i];
134+
if ((arg === "--scratch-path" || arg === "--build-path") && i + 1 < args.length) {
135+
const path = args[i + 1];
136+
if (path === "") {
137+
throw new Error(`Invalid ${arg}: path cannot be empty`);
138+
}
139+
if (path.startsWith("--")) {
140+
throw new Error(
141+
`Invalid ${arg}: expected a path but got another flag '${path}'`
142+
);
143+
}
144+
return path;
145+
}
146+
if (arg.startsWith("--scratch-path=")) {
147+
const path = arg.substring("--scratch-path=".length);
148+
if (path === "") {
149+
throw new Error("Invalid --scratch-path: path cannot be empty");
150+
}
151+
return path;
152+
}
153+
if (arg.startsWith("--build-path=")) {
154+
const path = arg.substring("--build-path=".length);
155+
if (path === "") {
156+
throw new Error("Invalid --build-path: path cannot be empty");
157+
}
158+
return path;
159+
}
160+
}
161+
return undefined;
162+
}
163+
126164
/**
127165
* Get build path from configuration if exists or return a fallback .build directory in given workspace
128166
* @param filesystem path to workspace that will be used as a fallback loacation with .build directory
@@ -134,7 +172,12 @@ export class BuildFlags {
134172
): string {
135173
const nodePath =
136174
platform === "posix" ? path.posix : platform === "win32" ? path.win32 : path;
137-
const buildPath = configuration.buildPath.length > 0 ? configuration.buildPath : ".build";
175+
176+
const buildPath =
177+
BuildFlags.extractScratchPath(configuration.buildArguments) ??
178+
BuildFlags.extractScratchPath(configuration.packageArguments) ??
179+
(configuration.buildPath.length > 0 ? configuration.buildPath : ".build");
180+
138181
if (!nodePath.isAbsolute(buildPath) && absolute) {
139182
return nodePath.join(workspacePath, buildPath);
140183
} else {

test/unit-tests/toolchain/BuildFlags.test.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ suite("BuildFlags Test Suite", () => {
203203

204204
suite("buildDirectoryFromWorkspacePath", () => {
205205
const buildPathConfig = mockGlobalValue(configuration, "buildPath");
206+
const buildArgsConfig = mockGlobalValue(configuration, "buildArguments");
207+
const packageArgsConfig = mockGlobalValue(configuration, "packageArguments");
208+
209+
beforeEach(() => {
210+
buildPathConfig.setValue("");
211+
buildArgsConfig.setValue([]);
212+
packageArgsConfig.setValue([]);
213+
});
206214

207215
test("no configuration provided", () => {
208216
buildPathConfig.setValue("");
@@ -239,6 +247,107 @@ suite("BuildFlags Test Suite", () => {
239247
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
240248
).to.equalPath("/some/full/workspace/test/path/some/relative/test/path");
241249
});
250+
251+
test("--scratch-path in buildArguments with separate value", () => {
252+
buildArgsConfig.setValue(["--scratch-path", "/custom/scratch/path"]);
253+
254+
expect(
255+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
256+
).to.equalPath("/custom/scratch/path");
257+
258+
expect(
259+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
260+
).to.equalPath("/custom/scratch/path");
261+
});
262+
263+
test("--scratch-path in buildArguments with equals format", () => {
264+
buildArgsConfig.setValue(["--scratch-path=/custom/scratch/path"]);
265+
266+
expect(
267+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
268+
).to.equalPath("/custom/scratch/path");
269+
270+
expect(
271+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
272+
).to.equalPath("/custom/scratch/path");
273+
});
274+
275+
test("--scratch-path with relative path in buildArguments", () => {
276+
buildArgsConfig.setValue(["--scratch-path", "custom/build"]);
277+
278+
expect(
279+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
280+
).to.equalPath("custom/build");
281+
282+
expect(
283+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
284+
).to.equalPath("/some/full/workspace/test/path/custom/build");
285+
});
286+
287+
test("--build-path in buildArguments (legacy support)", () => {
288+
buildArgsConfig.setValue(["--build-path", "/legacy/build/path"]);
289+
290+
expect(
291+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
292+
).to.equalPath("/legacy/build/path");
293+
294+
expect(
295+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
296+
).to.equalPath("/legacy/build/path");
297+
});
298+
299+
test("--scratch-path in packageArguments", () => {
300+
packageArgsConfig.setValue(["--scratch-path", "/package/scratch/path"]);
301+
302+
expect(
303+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
304+
).to.equalPath("/package/scratch/path");
305+
306+
expect(
307+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", true)
308+
).to.equalPath("/package/scratch/path");
309+
});
310+
311+
test("buildArguments takes precedence over packageArguments", () => {
312+
buildArgsConfig.setValue(["--scratch-path", "/build/args/path"]);
313+
packageArgsConfig.setValue(["--scratch-path", "/package/args/path"]);
314+
315+
expect(
316+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
317+
).to.equalPath("/build/args/path");
318+
});
319+
320+
test("buildArguments takes precedence over buildPath config", () => {
321+
buildPathConfig.setValue("/config/path");
322+
buildArgsConfig.setValue(["--scratch-path", "/build/args/path"]);
323+
324+
expect(
325+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
326+
).to.equalPath("/build/args/path");
327+
});
328+
329+
test("packageArguments takes precedence over buildPath config", () => {
330+
buildPathConfig.setValue("/config/path");
331+
packageArgsConfig.setValue(["--scratch-path", "/package/args/path"]);
332+
333+
expect(
334+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
335+
).to.equalPath("/package/args/path");
336+
});
337+
338+
test("--scratch-path among other arguments", () => {
339+
buildArgsConfig.setValue([
340+
"--verbose",
341+
"--scratch-path",
342+
"/custom/path",
343+
"--configuration",
344+
"release",
345+
]);
346+
347+
expect(
348+
BuildFlags.buildDirectoryFromWorkspacePath("/some/full/workspace/test/path", false)
349+
).to.equalPath("/custom/path");
350+
});
242351
});
243352

244353
suite("withAdditionalFlags", () => {

0 commit comments

Comments
 (0)