Execute Gradle task in ProgressExecutionMode.MODAL_SYNC

Answered

I want to block the project during my task execution.

For that, I'm trying to use ProgressExecutionMode.MODAL_SYNC which works good with maven tasks.

Unfortunately, the modal window hangs, and the task is never completed.

I have read topic https://intellij-support.jetbrains.com/hc/en-us/community/posts/203373784-Call-Gradle-task-synchronously where @... left comment about resolving this issue, but it anyway doesn't work.

Intellij version:

id("org.jetbrains.intellij") version "1.15.0"

My call:

ExternalSystemUtil.runTask(
externalSettings,
DefaultRunExecutor.EXECUTOR_ID,
project,
GradleConstants.SYSTEM_ID,
null,
ProgressExecutionMode.MODAL_SYNC,
false
);
0
4 comments

Hi Dimitrii,

Could you please share more context? From what context is this code executed? What is the IDE version?

It would be great if you could share the code.

0

Karol Lewandowski Hi! Thanks for the answer.

I'm trying to execute this code from AnAction. The task is "spotlessApply" from spotless gradle plugin

Example:

public class ReformatFileAction extends AnAction {

@Override
public void actionPerformed(AnActionEvent event) {
Project project = event.getProject();
if (project == null) {
return;
}

ExternalSystemTaskExecutionSettings externalSettings = new ExternalSystemTaskExecutionSettings();
externalSettings.setExternalProjectPath(project.getBasePath());
externalSettings.setTaskNames(Collections.singletonList("spotlessApply"));
externalSettings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId());

ExternalSystemUtil.runTask(
externalSettings,
DefaultRunExecutor.EXECUTOR_ID,
project,
GradleConstants.SYSTEM_ID,
null,
ProgressExecutionMode.MODAL_SYNC,
false
);
}
}

IntelliJ IDEA 2023.2 Build #IU-232.8660.185, built on July 26, 2023

Here is my real code: https://github.com/lipiridi/spotless-applier/blob/main/src/main/java/com/lipiridi/spotless/applier/ToolEnvExternalSystemUtil.java#L79

0

Hi,

It looks like a bug. Please create an issue on https://youtrack.jetbrains.com/issues/IDEA and provide a thread dump when the IDE hangs. For instructions, see https://intellij-support.jetbrains.com/hc/en-us/articles/206544899-Getting-a-thread-dump-when-IDE-hangs-and-doesn-t-respond

In general, running a task this way from the action is not recommended. It will block the UI for the time of the task execution, which freezes the UI. The recommended approach is to run it in the background.

0

Thank you!

I have created an issue: https://youtrack.jetbrains.com/issue/IDEA-327879/Executing-Gradle-task-in-ProgressExecutionMode.MODALSYNC-hangs-the-IDE 

About freezing the UI. It is expected behavior, because of it's a formatting task and I want to avoid modifying files by user during the execution.

0

Please sign in to leave a comment.