air icon indicating copy to clipboard operation
air copied to clipboard

air watches all files in any directory instead of only .go files or those within a valid go.mod file

Open wathika-eng opened this issue 9 months ago • 11 comments

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

  1. Navigate to the home directory:
cd ~
air
  1. Modify any non-Go file (e.g., .bashrc or a random text file).

  2. 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")
	}

wathika-eng avatar Apr 02 '25 09:04 wathika-eng

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.

dw-0 avatar Apr 17 '25 17:04 dw-0

There is no need to have an .air.toml in every directory

wathika-eng avatar Apr 18 '25 17:04 wathika-eng

Not sure where i stated that this would be required.

dw-0 avatar Apr 18 '25 18:04 dw-0

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.

wathika-eng avatar Apr 19 '25 04:04 wathika-eng

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.

dw-0 avatar Apr 19 '25 13:04 dw-0

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.

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.

wathika-eng avatar Apr 20 '25 05:04 wathika-eng

You're right, I just tried running air in a higher level directory and it started spamming watching folders.....yikes

adamk33n3r avatar Jun 08 '25 06:06 adamk33n3r

https://github.com/user-attachments/assets/7bf81c05-22ce-47d0-a19b-a59e73c39502

wathika-eng avatar Jun 08 '25 09:06 wathika-eng

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.

dw-0 avatar Jun 08 '25 10:06 dw-0

It's only a matter of time before someone exploits this behavior to intentionally crash systems or servers.

wathika-eng avatar Jun 08 '25 10:06 wathika-eng

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.

developerasun avatar Oct 05 '25 17:10 developerasun