gogo-air

Go: air isn't building my project after I moved the files to subdirectories


I'm very new to Go and I just followed the air guide to getting my app live-reloading, which worked, but when I then tried to move my Go files to subdirectories to follow the advice in the "Let's Go Further" ebook, air stopped working.


Solution

  • I eventually figured it out with a lot of Googling:

    1. I had to modify the cmd parameter in my .air.toml file to point at the directory where I had moved my main.go file:
    cmd = "go build -o ./tmp/main ./cmd/api/."
    
    1. I had to update my .air.toml file to list the directories I wanted it to watch for changes:
    include_dir = ["cmd", "internal"]
    

    So my backend directory now looks like this:

    bin/
    cmd/
      api/
    internal/
      data/
    migrations/
    remote/
    .air.toml
    .gitignore
    go.mod
    Makefile
    

    And my .air.toml now looks like this:

    [build]
    bin = "./tmp/main"
    cmd = "go build -o ./tmp/main ./cmd/api/."
    include_dir = ["cmd", "internal"]
    log = "stdout"
    
    [run]
    cmd = "./tmp/main"  # Command to run the binary