ActionOnSave running twice

Answered

I have an actionOnSave listener that is getting triggered twice per save.

I have been debugging as to why that might be happening but the logs done seem to indicate any process running that triggers an intermediate save.

Scrubbed logs, as you can see there are no logs visible that are triggered either side of my logging.

2023-12-22 20:09:13,958 [2400792]   INFO - #c.i.w.i.i.j.s.JpsGlobalModelSynchronizerImpl - Saving global entities to files
2023-12-22 20:09:14,029 [2400863]   INFO - #com.dprint.listeners.OnSaveAction - Running save action on /Users/ryanr/file.ts.
2023-12-22 20:09:14,029 [2400863]   INFO - #com.dprint.listeners.OnSaveAction - Running save action on /Users/ryanr/file.ts.
2023-12-22 20:09:14,035 [2400869]   INFO - #com.dprint.services.editorservice.EditorServiceManager - Creating formatting task for /Users/ryanr/project/file.ts
2023-12-22 20:09:14,036 [2400870]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Formatting /Users/ryanr/project/file.ts
2023-12-22 20:09:14,037 [2400871]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Created formatting task for /Users/ryanr/project/file.ts with id 21
2023-12-22 20:09:14,039 [2400873]   INFO - #com.dprint.services.editorservice.EditorProcess - Dprint daemon 80464: [DEBUG] Host formatting /Users/ryanr/project/file.ts - File length: 20763 - Plugins: [canva-dprint-plugin-prettier] - Range: None
2023-12-22 20:09:14,066 [2400900]   INFO - #com.dprint.services.editorservice.EditorServiceManager - Creating formatting task for /Users/ryanr/project/file.ts
2023-12-22 20:09:14,067 [2400901]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Formatting /Users/ryanr/work/project/file.ts
2023-12-22 20:09:14,067 [2400901]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Created formatting task for /Users/ryanr/project/file.ts with id 22
2023-12-22 20:09:14,069 [2400903]   INFO - #com.dprint.services.editorservice.EditorProcess - Dprint daemon 80464: [DEBUG] Host formatting /Users/ryanr/project/file.ts - File length: 20763 - Plugins: [canva-dprint-plugin-prettier] - Range: None
2023-12-22 20:09:14,132 [2400966]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Successfully formatted /Users/ryanr/project/file.ts
2023-12-22 20:09:14,150 [2400984]   INFO - #com.dprint.services.editorservice.v5.EditorServiceV5 - Successfully formatted /Users/ryanr/project/file.ts
2023-12-22 20:09:14,164 [2400998]   INFO - #c.i.c.ComponentStoreImpl - Saving Project(name=web, containerState=COMPONENT_CREATED, componentStore=/Users/ryanr/project/)ExternalProjectsManager took 37 ms, MavenProjectNavigator took 12 ms, ServiceViewManager took 19 ms, StandardJSConfiguration took 11 ms

Implementation:

class OnSaveAction : ActionsOnSaveFileDocumentManagerListener.ActionOnSave() {
    override fun isEnabledForProject(project: Project): Boolean {
        val projectConfig = project.service<ProjectConfiguration>().state
        val userConfig = project.service<UserConfiguration>().state
        return projectConfig.enabled && userConfig.runOnSave
    }

    override fun processDocuments(
        project: Project,
        documents: Array<Document?>,
    ) {
        val currentCommandName = CommandProcessor.getInstance().currentCommandName
        if (currentCommandName == ReformatCodeProcessor.getCommandName()) {
            return
        }
        val formatterService = project.service<FormatterService>()
        val manager = FileDocumentManager.getInstance()
        for (maybeDocument in documents) {
            maybeDocument?.let { document ->
                manager.getFile(document)?.let { vfile ->
                    infoLogWithConsole(DprintBundle.message("save.action.run", vfile.path), project, LOGGER)
                    formatterService.format(vfile, document)
                }
            }
        }
    }
}

 

0
5 comments

Hi Ryan,

It is hard to understand what is happening. I suggest setting up a breakpoint for processDocument and checking why multiple documents (file.ts and file) are passed. See https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/ide/actionsOnSave/impl/ActionsOnSaveFileDocumentManagerListener.kt#L101-L138.

Also, there is the Settings | Tools | Actions on Save | Reformat Code option, which allows to perform formatting on save action (implemented in com.intellij.codeInsight.actions.onSave.FormatOnSaveAction). It is not recommended to format files on save if this option is disabled, as it breaks the UX (IDE behaves in an unexpected way). See https://plugins.jetbrains.com/docs/intellij/plugin-user-experience.html. I suggest dropping this idea if it tries to force formatting without the user's agreement.

0

I suggest dropping this idea if it tries to force formatting without the user's agreement.

I have it as a setting in my plugin's Configurable Tool window (Settings | Tools | Dprint | Run dprint on save). It existed before Settings | Tools | Actions on Save was available I believe, but I should probably move it there and have something similar to Settings | Tools | Actions on Save | Run prettier as I have users who like to split the behaviour away from the default IntelliJ formatter.

Also, thanks. I will check out that debug point and report back.

0

Karol Lewandowski should I be able to debug those files. I get compiled sources which state the implementations aren't available. I had a quick look around to see if there was a way to get the SDK sources but couldn't see it.

0

I suggest setting up a breakpoint for processDocument and checking why multiple documents (file.ts and file) are passed

I think I have messed up here and that is a typo. I scrubbed file and path names as the logs were taken from my work computer replacing them with dummy values. They should both be file.ts.

0

I figured out what this is for anyone that comes across it.

I had a conditional plugin xml configuration file that set up the action to run after eslint if Javascript was a feature in the IDE. See this.

Removing this so that the action is only registered once fixed it.

1

Please sign in to leave a comment.