diff --git a/anvil/anvil.go b/anvil/anvil.go index e86a23ec..414e696f 100644 --- a/anvil/anvil.go +++ b/anvil/anvil.go @@ -93,6 +93,8 @@ func (a *Anvil) Start(ctx context.Context) error { if len(a.cfg.GenesisJSON) > 0 && a.cfg.ForkConfig == nil { tempFile, err := os.CreateTemp("", "genesis-*.json") + defer a.removeTempFile(tempFile) + if err != nil { return fmt.Errorf("error creating temporary genesis file: %w", err) } @@ -122,6 +124,8 @@ func (a *Anvil) Start(ctx context.Context) error { // Handle stdout/stderr logFile, err := os.CreateTemp("", fmt.Sprintf("anvil-chain-%d-", a.cfg.ChainID)) + defer a.removeTempFile(logFile) + if err != nil { return fmt.Errorf("failed to create temp log file: %w", err) } @@ -301,3 +305,9 @@ func (a *Anvil) SimulatedLogs(ctx context.Context, tx *types.Transaction) ([]typ return logs, err } + +func (a *Anvil) removeTempFile(tempFile *os.File) { + if err := os.Remove(tempFile.Name()); err != nil { + a.log.Warn("failed to remove temp genesis file", "file.path", tempFile.Name(), "err", err) + } +}