File Watchers
Required plugin: File Watchers
File Watcher is a GoLand tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.
File Watchers have two dedicated code inspections:
The File Watcher available inspection is run in every file where a predefined File Watcher is applicable. If the project has no relevant File Watcher configured, GoLand suggests to add one.
The File Watcher problems inspection is invoked by a running File Watcher and highlights errors specific to it.
You can use one of the available templates or configure a File Watcher from scratch. A configured File Watcher can be saved in your project settings or in the IDE settings and used in different projects. The following image lists predefined templates for file watchers. You cannot modify the list of predefined templates.
![Templates for file watchers Templates for file watchers](https://resources.jetbrains.com/help/img/idea/2024.3/go_file_watcher_inspections.png)
For more information about File Watchers for specific tools, see the corresponding pages:
Note that using some of these tools in GoLand requires that you install plugins on the Settings | Plugins page as described in Installing plugins from JetBrains Marketplace.
In the Settings dialog (CtrlAlt0S) , click File Watchers under Tools. The File Watchers page opens showing a list of File Watchers that are already configured in this project and in the IDE.
Click
and choose the predefined template from which you want to create a File Watcher. The choice depends on the tool you are going to use. To use a tool that is not on the list, choose Custom. The New Watcher dialog opens.
In the Name field, type the name of the File Watcher. By default, GoLand suggests the name of the selected predefined template.
Use the controls in the Files to watch area to define the range of files where you want to apply the File Watcher.
From the File type list, choose the expected type of input files. The File Watcher will consider only files of this type as subject for analyzing and processing. File types are recognised based on associations between file types and file extensions.
By default, the field shows the file type in accordance with the chosen predefined template.
Select the Scope in which the File Watcher is applicable.
Changes in these files will invoke the File Watcher either immediately or upon save or frame deactivation, depending on the status of the Auto-save edited files to trigger the watcher checkbox.
Select one of the predefined scopes from the list. For a project-level File Watcher, you can also click
to configure a custom scope in the Scopes dialog that opens.
All Places: all the scopes listed below.
Project Files: all the files within the project
Scratches and Consoles: all the scratches and consoles in the Files tool window (View | Tool Windows | Files).
Recently Viewed Files: all the files that were recently viewed in the editor.
Recently Changed Files: all the files that recently changed in the editor.
Module: all the files in the current module.
Current File: the file opened in the active editor tab.
All Changed Files: all changed files, that is, all files associated with all existing changelists.
Default Changelist: all the files associated with the changelist
Default
.
Alternatively, click
and configure a custom scope in the Scopes dialog that opens.
For more information, refer to Scopes.
In the Tool to run on changes area, specify the tool to use, the arguments to pass to it, the expected output file types, and so on.
In the Program field, specify the path to the executable file of the tool (.exe, .cmd, .bat, or other depending on the specific tool).
tip
To use the program filename instead of its full path, add the path to its folder to the
PATH
environment variable.On Windows, you can skip the .com, .exe, .cmd or .bat extension.
To use a jar archive, specify the absolute path to it. Alternatively, to use a relative path, add its parent folder to the GoLand path variables on the Appearance & Behavior | Path Variables settings page CtrlAlt0S.
In the Arguments field, define the arguments to pass to the tool.
Arguments are usually specified using macros, for example,
$FileName$
or$FileNameWithoutExtension$
, that will be replaced with the actual filenames.Type the macros manually or click
and select the relevant pattern fom the list in the Macros dialog that opens.
tip
When specifying the arguments, follow these rules:
Use spaces as separators.
If an argument contains spaces, enclose them or the entire argument in double quotes:
some" "arg
or"some arg"
.If an argument contains double quotes, use backslashes to escape them:
-Dmy.prop=\"quoted_value"\
.
In the Output paths to refresh field, specify the files where the tool stores its output: the resulting source code, source maps, and dependencies. Based on these settings, GoLand recognizes the files generated through compilation.
Output paths are usually specified using macros. You can type them manually or click
and select the relevant one from the list in the dialog that opens.
To specify several macros, use colons
:
as separators, for example,$FileNameWithoutExtension$.css:$FileNameWithoutExtension$.css.map
.note
Changing the value in the Output paths to refresh field does not make the tool store its output in another place. If you still need to do that, specify the desired custom output location in the Arguments field: type the output paths with colons as separators and use macros.
Expand the
Working Directory and Environment Variables hidden area.
Define the environment variables. For example, specify the
PATH
variable for the tools that are required for starting the tool you are configuring but are not referenced in the path to it.In the Working directory field, specify the directory to which the tool will be applied.
Because the tool is always invoked in the context of a file, the default working directory is the directory of the current file. The default working directory is specified in all predefined templates with the
$FileDir$
macro. To specify a custom working directory, type the path to it in the field, or clickand select the directory in the Select Path dialog, or click
and select the desired macro from the list in the Macros dialog.
tip
If you leave the Working Directory field empty, GoLand uses the directory of the file where the File Watcher is invoked.
In the Advanced Options area, customize the default behaviour of the File Watcher.
Specify the events that will invoke the File Watcher:
To invoke the File Watcher as soon as any changes are made to the source code, select the Auto-save edited files to trigger the watcher checkbox.
When the checkbox is cleared, the File Watcher starts upon save (File | Save All) or when you move the focus from GoLand (on frame deactivation).
By default, the File Watcher wakes up on any saved change, including the updates received from your version control system when you, for example, check out a branch. To ignore such changes and invoke the File Watcher only when you update your code in GoLand, clear the Trigger the watcher on external changes checkbox.
Specify whether you want the File Watcher to interact with the GoLand syntax parser:
When the Trigger watcher regardless of syntax errors checkbox is selected, the File Watcher start regardless of the syntactical correctness of a file. The File Watcher will start upon update, save, or frame deactivation, depending on the status of the Auto-save edited files to trigger the watcher checkbox.
When the Trigger watcher regardless of syntax errors checkbox is cleared, the File Watcher ignores all triggers in files that are syntactically invalid and starts only in error-free files.
Use the Create output file from stdout checkbox to specify how you want to generate the output file.
When the checkbox is selected, GoLand reads the native tool's output
standard output stream (stdout)
and generates the resulting files from it.When the checkbox is cleared, the tool writes its output directly to the files specified in the Output paths to refresh field.
In the Show console list, choose when you want the File Watcher to open the console.
Always: with this option, the console always opens after the tool execution is completed.
On error: with this option, the console opens after the tool execution only when the
Exit code
is different from0
.Never: choose this option to suppress opening the console at all.
If the tool configured in the File Watcher reports errors or warnings about your code with the links to the files and specific lines, you can see this information right in the editor:
In Settings | Editor | Inspections, enable the inspection File Watcher Problems.
In the Output Filters field of the Edit Watcher dialog, describe the format of the output you want to match. Use the macros
$FILE_PATH$
,$LINE$
, and$MESSAGE$
for that. You need to avoid using special symbols like braces and dot with\
. The text matched with the$MESSAGE$
macro will be shown in the editor.
warning
File Watchers do not start when you open a project in the Safe Mode. For more information, refer to Project security.
Once you’ve saved the new File Watcher, you can decide if it’s going to be available only in the current project (select Project from the Level list) or for all projects (select Global).
![enable a File Watcher enable a File Watcher](https://resources.jetbrains.com/help/img/idea/2024.3/go_enable_global_watcher.png)
To enable or disable a File Watcher, open the Settings dialog (CtrlAlt0S) , go to Tools | File Watchers, and select or clear the checkbox next to it.
When a File Watcher is enabled, it starts automatically as soon as a file of the selected type and in the selected scope is changed or saved, refer to Configuring advanced options.
If an error occurs while a File Watcher is running, the File Watcher is automatically disabled. To restore the status, enable the File Watcher manually.
You can configure the IDE to run file watchers in modified files automatically when your changes are saved.
Press CtrlAlt0S to open settings and then select Tools | Actions on Save.
Enable the File Watcher option and apply the changes.
Global File Watchers can be automatically enabled in all new projects that you create in the future.
Open the Settings for New Projects dialog (File | New Projects Setup | Settings for New Projects) , go to Tools | File Watchers, and select the checkboxes next to the required File Watchers.
Alternatively, on the Welcome Screen, select Settings from the Configure list. In the dialog that opens, select the checkboxes next to the relevant File Watchers.
There might be a red error message next to the File Watcher in the list in Settings | Tools | File Watchers.
Here is a list of possible errors:
Reported Problem | Problem Description | Workaround |
---|---|---|
| The File Watcher uses a scope that is not defined in this project. | Double-click the watcher and select an available scope or create a new one. |
| The project uses a global File Watcher that was removed. | Delete the watcher from the list using the Remove button |
GoLand runs numerous identical File Watcher tasks | After a bulk update, for example, from your VCS, GoLand runs a separate File Watcher task for each saved file. | Add a file-specific macro (like |
Thanks for your feedback!