【air】/app/tmpmain.exe: not foundの解決方法【Golang】

こんにちはRujuuです。

今回は「/app/tmpmain.exe: not found」の解決方法を紹介します。

発生経緯

Echoでバックエンドの開発をしていて、ホットリロードを行うためにairを導入したところ以下の様な「/app/tmpmain.exe: not found」エラーが出ました。

エラー表示

環境

必要ないかもしれませんが、Dockerfileとdocker-compose.ymlのコードも載せておきます。

Dockerfile
FROM golang:1.20-bullseye

ENV TZ /usr/share/zoneinfo/Asia/Tokyo

WORKDIR /app

RUN apt-get update && apt-get install -y tree vim

RUN go install github.com/cosmtrek/air@latest

COPY go.mod go.sum ./
RUN go mod download && go mod verify
docker-compose.yml
version: "3.8"

services:
  echo:
    container_name: echo
    build: .
    volumes:
      - .:/app
    tty: true
    ports:
      - "8080:8080"
    networks:
      - backend

  postgres:
    image: postgres:15.1-alpine
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: test
      POSTGRES_PASSWORD: test
      POSTGRES_DB: test
    restart: always
    networks:
      - backend

networks:
  backend:

解決方法

「air init」で生成される「.air.toml」のbinのpathを書き換えると正常に動きます。

以下のコードでいうと7行目です。

変更前

.air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "tmp\\main.exe"
  cmd = "go build -o ./tmp/main.exe ."
  delay = 0
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true

変更後

.air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "./tmp/main.exe"
  cmd = "go build -o ./tmp/main.exe ."
  delay = 0
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  poll = false
  poll_interval = 0
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true

これで以下のように、airが正常に動くようになったと思います!

おわりに

ここまで読んでくださり、ありがとうございました!

airに触れたのは初めてなので、もしより良い解決方法などありましたらコメントなどで優しく教えていただけると嬉しいです。

良いGoライフを✨

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA