Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix - Gradle test debug not working on version 8.5 and higher #1586

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions extension/src/bs/GradleTestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
private readonly _onDidChangeTestItemStatus = new vscode.EventEmitter<TestItemStatusChangeEvent>();
private readonly _onDidFinishTestRun = new vscode.EventEmitter<TestFinishEvent>();
private context: IRunTestContext;
private testRunnerApi: any;

Check warning on line 18 in extension/src/bs/GradleTestRunner.ts

View workflow job for this annotation

GitHub Actions / Build & Analyse

Unexpected any. Specify a different type
private testInitScriptPath: string;

public onDidChangeTestItemStatus: vscode.Event<TestItemStatusChangeEvent> = this._onDidChangeTestItemStatus.event;
public onDidFinishTestRun: vscode.Event<TestFinishEvent> = this._onDidFinishTestRun.event;

constructor(testRunnerApi: any) {

Check warning on line 24 in extension/src/bs/GradleTestRunner.ts

View workflow job for this annotation

GitHub Actions / Build & Analyse

Unexpected any. Specify a different type
this.testRunnerApi = testRunnerApi;
this.testInitScriptPath = path.join(os.tmpdir(), "testInitScript.gradle");
}
Expand Down Expand Up @@ -52,22 +52,7 @@
let debugPort = -1;
if (isDebug) {
debugPort = await getPort();
// See: https://docs.gradle.org/current/javadoc/org/gradle/tooling/TestLauncher.html#debugTestsOn(int)
// since the gradle tooling api does not support debug tests in server=y mode, so we use the init script
// as a workaround
const initScriptContent = `allprojects {
afterEvaluate {
tasks.withType(Test) {
debugOptions {
enabled = true
host = 'localhost'
port = ${debugPort}
server = true
suspend = true
}
}
}
}`;
const initScriptContent = this.getInitScriptContent(debugPort);
await vscode.workspace.fs.writeFile(
vscode.Uri.file(this.testInitScriptPath),
Buffer.from(initScriptContent)
Expand Down Expand Up @@ -180,4 +165,23 @@
throw new Error("The debugger was not started");
}
}

/**
* See: https://docs.gradle.org/current/javadoc/org/gradle/tooling/TestLauncher.html#debugTestsOn(int)
* since the gradle tooling api does not support debug tests in server=y mode, so we use the init script
* as a workaround.
*
* We directly set the jvmArgs here because the debugOptions in the init script does not work for Gradle > 8.4.
*
* Note that this approach may have problem that multiple test tasks are executed in one build invocation.
* In that case, there's a race between tasks that first uses the specified debug port. To resolve this issue,
* we may consider checking the project root path in the init script as well.
*/
private getInitScriptContent(debugPort: number): string {
return `allprojects {
tasks.withType(Test) {
jvmArgs '-agentlib:jdwp=transport=dt_socket,server=y,address=${debugPort},suspend=y'
}
}`;
}
}
Loading