air watches all files in any directory instead of only .go files or those within a valid go.mod file
When running air in the home directory (~), it watches all files and subdirectories instead of only .go files or those within a valid go.mod directory. This results in excessive file watching, high CPU usage, and potential performance issues.
Expected Behavior
air should watch only .go files or files within the current project directory containing a go.mod file.
It should ignore irrelevant files such as hidden files, non-Go files, and system directories.
Steps to Reproduce
- Navigate to the home directory:
cd ~
air
-
Modify any non-Go file (e.g., .bashrc or a random text file).
-
Observe that air detects changes and triggers reloads.
Possible Solutions https://github.com/air-verse/air/blob/master/runner/config.go#L112
func InitConfig(path string, cmdArgs map[string]TomlInfo) (cfg *Config, err error) {
// Ensure we are in a valid Go module directory
if _, err := os.Stat("go.mod"); os.IsNotExist(err) {
return nil, fmt.Errorf("no go.mod file found in the current directory")
}
I guess it might make sense to watch other files. You could solve this by configuring air with this in the .air.toml:
root = <path of your project root>
[build]
include_ext = ["go"]
root sets the directory the filewatcher is watching, include_ext just includes files with the configured extensions.
There is no need to have an .air.toml in every directory
Not sure where i stated that this would be required.
Not sure where i stated that this would be required.
Then I don't understand, where should the .air.toml file be placed? The problem here is air currently watches files if you run it on any directory.
Maybe i misunderstood you earlier.
Then I don't understand, where should the .air.toml file be placed?
In the root of your project. Each project should have its own .air.toml file.
When running air, you can then pass the config file via arguments to the command, e.g. air -c <root>/apps/backend/.air.toml
In that .air.toml you configure the root path as will. This will be the root where air will start watching files. Everything in the specified root will be watched. If you want to exclude files/folders, then add them to the correspondig config sections of the .air.toml. I think air watching all kind of files is by design, but that would be something a maintainer must answer, i can just assume its the intended behavior.
Maybe i misunderstood you earlier.
Then I don't understand, where should the .air.toml file be placed?
In the root of your project. Each project should have its own .air.toml file. When running air, you can then pass the config file via arguments to the command, e.g.
air -c <root>/apps/backend/.air.tomlIn that .air.toml you configure the root path as will. This will be the root where air will start watching files. Everything in the specified root will be watched. If you want to exclude files/folders, then add them to the correspondig config sections of the .air.toml. I think air watching all kind of files is by design, but that would be something a maintainer must answer, i can just assume its the intended behavior.
The behavior can be risky. If I accidentally run air in the wrong directory e.g my home or Downloads folder and there isn't a .air.toml present (or not well-configured .air.toml), air could start watching an excessive number of files resulting in high cpu/ram usage. In contrast, tools like nodemon tend to have safer defaults or require explicit configuration to prevent that. It's only a matter of time before someone exploits this behavior to intentionally crash systems or servers.
You're right, I just tried running air in a higher level directory and it started spamming watching folders.....yikes
https://github.com/user-attachments/assets/7bf81c05-22ce-47d0-a19b-a59e73c39502
I still wonder why you would run air in your users home directory? What's the point of running the command from there? The whole point of air is watching for file changes, so by default it obviously watches any files and folders beginning where the command was executed. I understand that you're speaking of "accidentally executing", but once you accidentally did, you should know better next time and don't do it again.
It's only a matter of time before someone exploits this behavior to intentionally crash systems or servers.
Air supports directory setup to include/exclude.
Place .air.toml at project root and adjust config like this.
root = "."
[build]
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
include_dir = ["cmd","docs", "internal", "pkg"]
include_ext = ["go", "tpl", "tmpl", "html"]
you can run air with this config file with -c flag.
air -c ./.air.toml
check this reddit post also.
And you can try wgo if air doesn't meet your needs.