Skip to content

Commit 8da9017

Browse files
Pgns-bsbCopilot
andcommitted
Skip generated directories in Java class lookup
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 49cd9da commit 8da9017

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎src/languageModelTool.ts‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ const CONSTANTS = {
5858
MAX_FILE_SEARCH_DEPTH: 10
5959
};
6060

61+
const SEARCH_SKIPPED_DIRECTORIES = new Set([
62+
'.git',
63+
'.gradle',
64+
'.idea',
65+
'.vscode',
66+
'bin',
67+
'build',
68+
'classes',
69+
'dist',
70+
'generated',
71+
'generated-sources',
72+
'node_modules',
73+
'out',
74+
'target',
75+
]);
76+
77+
function shouldSkipSearchDirectory(name: string): boolean {
78+
return SEARCH_SKIPPED_DIRECTORIES.has(name);
79+
}
80+
6181
const LAUNCH_FAILURE_GUIDANCE = '\n\nDo not automatically retry debug_java_application or start the program again '
6282
+ 'through a terminal command. Report the result and diagnose the cause first. '
6383
+ 'After a timeout, you may check get_debug_session_info once and inspect existing terminal output; '
@@ -923,14 +943,14 @@ function findJavaFile(dir: string, className: string, depth: number = 0): string
923943
const files = fs.readdirSync(dir);
924944

925945
for (const file of files) {
946+
if (shouldSkipSearchDirectory(file)) {
947+
continue;
948+
}
949+
926950
const filePath = path.join(dir, file);
927951
const stat = fs.statSync(filePath);
928952

929953
if (stat.isDirectory()) {
930-
// Skip common non-source directories
931-
if (file === 'node_modules' || file === '.git' || file === 'target' || file === 'build') {
932-
continue;
933-
}
934954
const found = findJavaFile(filePath, className, depth + 1);
935955
if (found) {
936956
return found;
@@ -972,6 +992,10 @@ function hasClassFiles(dir: string, depth: number = 0): boolean {
972992
try {
973993
const files = fs.readdirSync(dir);
974994
for (const file of files) {
995+
if (shouldSkipSearchDirectory(file)) {
996+
continue;
997+
}
998+
975999
const filePath = path.join(dir, file);
9761000
const stat = fs.statSync(filePath);
9771001

‎test/languageModelToolLaunchPolicy.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as assert from "assert";
55
import * as fs from "fs";
6+
import * as os from "os";
67
import * as path from "path";
78
import * as vscode from "vscode";
89
import * as telemetry from "vscode-extension-telemetry-wrapper";
@@ -124,6 +125,33 @@ suite("Language Model Tool launch retry policy", () => {
124125
assert.strictEqual(text.includes("Call debug_java_application again"), false);
125126
}
126127

128+
test("ignores generated build directories when resolving simple class names", async () => {
129+
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "java-debug-class-detection-"));
130+
const sourceFile = path.join(tempRoot, "src", "main", "java", "com", "example", "App.java");
131+
const generatedFile = path.join(tempRoot, "build", "generated", "App.java");
132+
133+
fs.mkdirSync(path.dirname(sourceFile), { recursive: true });
134+
fs.mkdirSync(path.dirname(generatedFile), { recursive: true });
135+
136+
fs.writeFileSync(sourceFile, "package com.example;\npublic class App {}\n");
137+
fs.writeFileSync(generatedFile, "public class App {}\n");
138+
139+
try {
140+
const text = await invoke({
141+
target: "App",
142+
workspacePath: tempRoot,
143+
skipBuild: true,
144+
classpath: tempRoot,
145+
});
146+
147+
assert.ok(text.includes("Debug session started") || text.includes("Startup is unconfirmed, not necessarily failed"));
148+
assert.ok(!text.includes("Could not auto-detect package name"));
149+
assert.strictEqual(commandsSent, 1);
150+
} finally {
151+
fs.rmSync(tempRoot, { recursive: true, force: true });
152+
}
153+
});
154+
127155
for (const waitForSession of [false, true]) {
128156
test(`reports unconfirmed startup without relaunching (waitForSession=${waitForSession})`, async () => {
129157
const text = await invoke({

0 commit comments

Comments
 (0)