Skip to content

Commit 38464ac

Browse files
committed
Add ignoreSearchingForPackagesInSubfolders setting
1 parent 9a64919 commit 38464ac

File tree

6 files changed

+119
-14
lines changed

6 files changed

+119
-14
lines changed

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,22 @@
545545
"markdownDescription": "Search sub-folders of workspace folder for Swift Packages at start up.",
546546
"scope": "machine-overridable"
547547
},
548+
"swift.ignoreSearchingForPackagesInSubfolders": {
549+
"type": "array",
550+
"items": {
551+
"type": "string"
552+
},
553+
"default": [
554+
".",
555+
".build",
556+
"Packages",
557+
"out",
558+
"bazel-out",
559+
"bazel-bin"
560+
],
561+
"markdownDescription": "A list of glob patterns to ignore when searching sub-folders for Swift Packages. The `swift.searchSubfoldersForPackages` must be `true` for this setting to have an effect. Always use forward-slashes in glob expressions regardless of platform. This is combined with VS Code's default `files.exclude` setting.",
562+
"scope": "machine-overridable"
563+
},
548564
"swift.autoGenerateLaunchConfigurations": {
549565
"type": "boolean",
550566
"default": true,

src/WorkspaceContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ export class WorkspaceContext implements vscode.Disposable {
444444
workspaceFolder.uri,
445445
configuration.disableSwiftPMIntegration,
446446
configuration.folder(workspaceFolder).searchSubfoldersForPackages,
447+
configuration.folder(workspaceFolder).ignoreSearchingForPackagesInSubfolders,
447448
this.globalToolchainSwiftVersion
448449
);
449450

src/configuration.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export interface FolderConfiguration {
7878
readonly additionalTestArguments: string[];
7979
/** search sub-folder of workspace folder for Swift Packages */
8080
readonly searchSubfoldersForPackages: boolean;
81+
/** Folders to ignore when searching for Swift Packages */
82+
readonly ignoreSearchingForPackagesInSubfolders: string[];
8183
/** auto-generate launch.json configurations */
8284
readonly autoGenerateLaunchConfigurations: boolean;
8385
/** disable automatic running of swift package resolve */
@@ -223,6 +225,15 @@ const configuration = {
223225
.getConfiguration("swift", workspaceFolder)
224226
.get<boolean>("searchSubfoldersForPackages", false);
225227
},
228+
/** Folders to ignore when searching for Swift Packages */
229+
get ignoreSearchingForPackagesInSubfolders(): string[] {
230+
return vscode.workspace
231+
.getConfiguration("swift", workspaceFolder)
232+
.get<
233+
string[]
234+
>("ignoreSearchingForPackagesInSubfolders", [".", ".build", "Packages", "out", "bazel-out", "bazel-bin"])
235+
.map(substituteVariablesInString);
236+
},
226237
get attachmentsPath(): string {
227238
return substituteVariablesInString(
228239
vscode.workspace

src/utilities/workspace.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export async function searchForPackages(
2323
folder: vscode.Uri,
2424
disableSwiftPMIntegration: boolean,
2525
searchSubfoldersForPackages: boolean,
26+
skipFolders: Array<string>,
2627
swiftVersion: Version
2728
): Promise<Array<vscode.Uri>> {
2829
const folders: Array<vscode.Uri> = [];
@@ -32,24 +33,17 @@ export async function searchForPackages(
3233
if (await isValidWorkspaceFolder(folder.fsPath, disableSwiftPMIntegration, swiftVersion)) {
3334
folders.push(folder);
3435
}
35-
// should I search sub-folders for more Swift Packages
36+
37+
// If sub-folder searches are disabled, don't search subdirectories
3638
if (!searchSubfoldersForPackages) {
3739
return;
3840
}
3941

4042
await globDirectory(folder, { onlyDirectories: true }).then(async entries => {
41-
const skipFolders = new Set<string>([
42-
".",
43-
".build",
44-
"Packages",
45-
"out",
46-
"bazel-out",
47-
"bazel-bin",
48-
]);
49-
43+
const skip = new Set<string>(skipFolders);
5044
for (const entry of entries) {
5145
const base = basename(entry);
52-
if (!skipFolders.has(base)) {
46+
if (!skip.has(base)) {
5347
await search(vscode.Uri.file(entry));
5448
}
5549
}

test/integration-tests/utilities/workspace.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ suite("Workspace Utilities Test Suite", () => {
2626
(vscode.workspace.workspaceFolders ?? [])[0]!.uri,
2727
false,
2828
true,
29+
[],
2930
testSwiftVersion
3031
);
3132

test/unit-tests/utilities/workspace.test.ts

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,95 @@ suite("Workspace Utilities Unit Test Suite", () => {
2727
const testSwiftVersion = new Version(5, 9, 0);
2828

2929
test("returns only root package when search for subpackages disabled", async () => {
30-
const folders = await searchForPackages(packageFolder, false, false, testSwiftVersion);
30+
const folders = await searchForPackages(
31+
packageFolder,
32+
false,
33+
false,
34+
[],
35+
testSwiftVersion
36+
);
3137

32-
expect(folders.map(folder => folder.fsPath)).eql([packageFolder.fsPath]);
38+
expect(folders.map(folder => folder.fsPath)).equals([packageFolder.fsPath]);
3339
});
3440

3541
test("returns subpackages when search for subpackages enabled", async () => {
36-
const folders = await searchForPackages(packageFolder, false, true, testSwiftVersion);
42+
const folders = await searchForPackages(
43+
packageFolder,
44+
false,
45+
true,
46+
[],
47+
testSwiftVersion
48+
);
49+
50+
expect(folders.map(folder => folder.fsPath).sort()).deep.equal([
51+
packageFolder.fsPath,
52+
firstModuleFolder.fsPath,
53+
secondModuleFolder.fsPath,
54+
]);
55+
});
56+
57+
test("skips specified folders when skipFolders contains Module1", async () => {
58+
const folders = await searchForPackages(
59+
packageFolder,
60+
false,
61+
true,
62+
["Module1"],
63+
testSwiftVersion
64+
);
65+
66+
expect(folders.map(folder => folder.fsPath).sort()).deep.equal([
67+
packageFolder.fsPath,
68+
secondModuleFolder.fsPath,
69+
]);
70+
});
71+
72+
test("skips specified folders when skipFolders contains Module2", async () => {
73+
const folders = await searchForPackages(
74+
packageFolder,
75+
false,
76+
true,
77+
["Module2"],
78+
testSwiftVersion
79+
);
80+
81+
expect(folders.map(folder => folder.fsPath).sort()).deep.equal([
82+
packageFolder.fsPath,
83+
firstModuleFolder.fsPath,
84+
]);
85+
});
86+
87+
test("skips multiple folders when skipFolders contains both modules", async () => {
88+
const folders = await searchForPackages(
89+
packageFolder,
90+
false,
91+
true,
92+
["Module1", "Module2"],
93+
testSwiftVersion
94+
);
95+
96+
expect(folders.map(folder => folder.fsPath)).equals([packageFolder.fsPath]);
97+
});
98+
99+
test("skipFolders has no effect when search for subpackages is disabled", async () => {
100+
const folders = await searchForPackages(
101+
packageFolder,
102+
false,
103+
false,
104+
["Module1", "Module2"],
105+
testSwiftVersion
106+
);
107+
108+
expect(folders.map(folder => folder.fsPath)).equals([packageFolder.fsPath]);
109+
});
110+
111+
test("skipFolders with non-existent folder names does not affect results", async () => {
112+
const folders = await searchForPackages(
113+
packageFolder,
114+
false,
115+
true,
116+
["NonExistentModule", "AnotherFakeModule"],
117+
testSwiftVersion
118+
);
37119

38120
expect(folders.map(folder => folder.fsPath).sort()).deep.equal([
39121
packageFolder.fsPath,

0 commit comments

Comments
 (0)