Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the issue with ineffective --build.full_bin command line argument #748

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import (
"flag"
"fmt"
"log"

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println

Check failure on line 6 in main.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: Println
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -89,12 +89,11 @@
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)

var err error
cfg, err := runner.InitConfig(cfgPath)
cfg, err := runner.InitConfig(cfgPath, cmdArgs)

Check warning on line 92 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L92

Added line #L92 was not covered by tests
if err != nil {
log.Fatal(err)
return
}
cfg.WithArgs(cmdArgs)
if !cfg.Log.Silent {
printSplash()
}
Expand Down
15 changes: 10 additions & 5 deletions runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t sliceTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Va
}

// InitConfig initializes the configuration.
func InitConfig(path string) (cfg *Config, err error) {
func InitConfig(path string, cmdArgs map[string]TomlInfo) (cfg *Config, err error) {
if path == "" {
cfg, err = defaultPathConfig()
if err != nil {
Expand All @@ -135,7 +135,7 @@ func InitConfig(path string) (cfg *Config, err error) {
return nil, err
}

err = ret.preprocess()
err = ret.preprocess(cmdArgs)
return ret, err
}

Expand Down Expand Up @@ -278,8 +278,12 @@ func readConfigOrDefault(path string) (*Config, error) {
return cfg, nil
}

func (c *Config) preprocess() error {
func (c *Config) preprocess(args map[string]TomlInfo) error {
var err error

if args != nil {
c.withArgs(args)
}
cwd := os.Getenv(airWd)
if cwd != "" {
if err = os.Chdir(cwd); err != nil {
Expand Down Expand Up @@ -384,8 +388,8 @@ func (c *Config) rel(path string) string {
return s
}

// WithArgs returns a new config with the given arguments added to the configuration.
func (c *Config) WithArgs(args map[string]TomlInfo) {
// withArgs returns a new config with the given arguments added to the configuration.
func (c *Config) withArgs(args map[string]TomlInfo) {
for _, value := range args {
// Ignore values that match the default configuration.
// This ensures user-specified configurations are not overwritten by default values.
Expand All @@ -394,4 +398,5 @@ func (c *Config) WithArgs(args map[string]TomlInfo) {
setValue2Struct(v, value.fieldPath, *value.Value)
}
}

}
8 changes: 4 additions & 4 deletions runner/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestBinCmdPath(t *testing.T) {
var err error

c := getWindowsConfig()
err = c.preprocess()
err = c.preprocess(nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestReadConfByName(t *testing.T) {
func TestConfPreprocess(t *testing.T) {
t.Setenv(airWd, "_testdata/toml")
df := defaultConfig()
err := df.preprocess()
err := df.preprocess(nil)
if err != nil {
t.Fatalf("preprocess error %v", err)
}
Expand All @@ -143,7 +143,7 @@ func TestConfigWithRuntimeArgs(t *testing.T) {

t.Run("when using bin", func(t *testing.T) {
df := defaultConfig()
if err := df.preprocess(); err != nil {
if err := df.preprocess(nil); err != nil {
t.Fatalf("preprocess error %v", err)
}

Expand All @@ -155,7 +155,7 @@ func TestConfigWithRuntimeArgs(t *testing.T) {
t.Run("when using full_bin", func(t *testing.T) {
df := defaultConfig()
df.Build.FullBin = "./tmp/main"
if err := df.preprocess(); err != nil {
if err := df.preprocess(nil); err != nil {
t.Fatalf("preprocess error %v", err)
}

Expand Down
4 changes: 2 additions & 2 deletions runner/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func NewEngineWithConfig(cfg *Config, debugMode bool) (*Engine, error) {
}

// NewEngine ...
func NewEngine(cfgPath string, debugMode bool) (*Engine, error) {
func NewEngine(cfgPath string, args map[string]TomlInfo, debugMode bool) (*Engine, error) {
var err error
cfg, err := InitConfig(cfgPath)
cfg, err := InitConfig(cfgPath, args)
if err != nil {
return nil, err
}
Expand Down
50 changes: 25 additions & 25 deletions runner/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

func TestNewEngine(t *testing.T) {
_ = os.Unsetenv(airWd)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -38,7 +38,7 @@ func TestNewEngine(t *testing.T) {

func TestCheckRunEnv(t *testing.T) {
_ = os.Unsetenv(airWd)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -49,7 +49,7 @@ func TestCheckRunEnv(t *testing.T) {
}

func TestWatching(t *testing.T) {
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -65,12 +65,12 @@ func TestWatching(t *testing.T) {
}

func TestRegexes(t *testing.T) {
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
engine.config.Build.ExcludeRegex = []string{"foo\\.html$", "bar", "_test\\.go"}
err = engine.config.preprocess()
err = engine.config.preprocess(nil)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestRunCommand(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -146,7 +146,7 @@ func TestRunPreCmd(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -171,7 +171,7 @@ func TestRunPostCmd(t *testing.T) {
// change dir to tmpDir
chdir(t, tmpDir)

engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -190,7 +190,7 @@ func TestRunPostCmd(t *testing.T) {
}

func TestRunBin(t *testing.T) {
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestRebuild(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
engine.config.Build.ExcludeUnchanged = true
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
Expand Down Expand Up @@ -310,14 +310,14 @@ func TestCtrlCWhenHaveKillDelay(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
engine.config.Build.KillDelay = c.Build.KillDelay
engine.config.Build.Delay = 2000
engine.config.Build.SendInterrupt = true
if err := engine.config.preprocess(); err != nil {
if err := engine.config.preprocess(nil); err != nil {
t.Fatalf("Should not be fail: %s.", err)
}

Expand Down Expand Up @@ -353,7 +353,7 @@ func TestCtrlCWhenREngineIsRunning(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestCtrlCWithFailedBin(t *testing.T) {
go func() {
dir := initWithQuickExitGoCode(t)
chdir(t, dir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
assert.NoError(t, err)
engine.config.Build.Bin = "<WRONGCOMAMND>"
sigs := make(chan os.Signal, 1)
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestFixCloseOfChannelAfterCtrlC(t *testing.T) {
// fix https://github.com/air-verse/air/issues/294
dir := initWithBuildFailedCode(t)
chdir(t, dir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func TestFixCloseOfChannelAfterTwoFailedBuild(t *testing.T) {
dir := initWithBuildFailedCode(t)
// change dir to tmpDir
chdir(t, dir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -530,7 +530,7 @@ func TestRun(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -716,7 +716,7 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand All @@ -725,7 +725,7 @@ func TestRebuildWhenRunCmdUsingDLV(t *testing.T) {
dlvPort, f := GetPort()
f()
engine.config.Build.FullBin = fmt.Sprintf("dlv exec --accept-multiclient --log --headless --continue --listen :%d --api-version 2 ./tmp/main", dlvPort)
_ = engine.config.preprocess()
_ = engine.config.preprocess(nil)
go func() {
engine.Run()
}()
Expand Down Expand Up @@ -818,7 +818,7 @@ include_file = ["test/not_a_test.go"]
if err := os.WriteFile(dftTOML, []byte(config), 0o644); err != nil {
t.Fatal(err)
}
engine, err := NewEngine(".air.toml", true)
engine, err := NewEngine(".air.toml", nil, true)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -881,7 +881,7 @@ func Test(t *testing.T) {
}

time.Sleep(time.Second * 2)
engine, err := NewEngine(".air.toml", false)
engine, err := NewEngine(".air.toml", nil, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -917,7 +917,7 @@ func TestCreateNewDir(t *testing.T) {
tmpDir := initTestEnv(t, port)
// change dir to tmpDir
chdir(t, tmpDir)
engine, err := NewEngine("", true)
engine, err := NewEngine("", nil, true)
if err != nil {
t.Fatalf("Should not be fail: %s.", err)
}
Expand Down Expand Up @@ -967,7 +967,7 @@ include_file = ["main.sh"]
t.Fatal(err)
}

engine, err := NewEngine(dftTOML, false)
engine, err := NewEngine(dftTOML, nil, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ include_file = ["main.sh"]
t.Fatal(err)
}

engine, err := NewEngine(dftTOML, false)
engine, err := NewEngine(dftTOML, nil, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1110,7 +1110,7 @@ func TestEngineExit(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e, err := NewEngine("", true)
e, err := NewEngine("", nil, true)
if err != nil {
t.Fatal(err)
}
Expand Down
24 changes: 22 additions & 2 deletions runner/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ func TestConfigRuntimeArgs(t *testing.T) {
assert.NotEqual(t, []string{}, conf.Build.ExcludeDir)
},
},
{
name: "check full_bin",
args: []string{"--build.full_bin", "APP_ENV=dev APP_USER=air ./tmp/main"},
check: func(t *testing.T, conf *Config) {
assert.Equal(t, "APP_ENV=dev APP_USER=air ./tmp/main", conf.Build.Bin)
},
},

{
name: "check exclude_regex patterns compiled",
args: []string{"--build.exclude_regex", "test_pattern\\.go"},
check: func(t *testing.T, conf *Config) {
assert.Equal(t, []string{"test_pattern\\.go"}, conf.Build.ExcludeRegex)
patterns, err := conf.Build.RegexCompiled()
require.NoError(t, err)
require.NotNil(t, patterns)
require.Len(t, patterns, 1)
assert.True(t, patterns[0].MatchString("test_pattern.go"), "regex should match test_pattern.go")
assert.False(t, patterns[0].MatchString("other_file.go"), "regex shouldn't match other_file.go")
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -126,12 +147,11 @@ func TestConfigRuntimeArgs(t *testing.T) {
flag := flag.NewFlagSet(t.Name(), flag.ExitOnError)
cmdArgs := ParseConfigFlag(flag)
_ = flag.Parse(tc.args)
cfg, err := InitConfig("")
cfg, err := InitConfig("", cmdArgs)
if err != nil {
log.Fatal(err)
return
}
cfg.WithArgs(cmdArgs)
tc.check(t, cfg)
})
}
Expand Down
Loading