dbt-labs / dbt-labs/dbt

[Bug] UnboundLocalError: local variable 'summary_message' referenced before assignment

Open
#9,436 0 comments 0 reactions 0 assignees View on GitHub
engine:v1 type:bug
Dominant language
Rust
Stars
13.8k
Forks
2.6k
Avg merge
21h 31m
Merged PRs (30d)
56

Description

### Is this a new bug in dbt-core?

- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug

### Current Behavior

When running `dbt debug` in certain circumstances\*, I get a python stack trace that ends in the following:
```
UnboundLocalError: local variable 'summary_message' referenced before assignment
```

### Expected Behavior

I'd expect a helpful error message relating to the configuration file(s) that were not found (`profiles.yml`) rather that a stack trace.

### Steps To Reproduce

1. Clone and install dbt-core as described in the CONTRIBUTING file
1. Do **not** set up any dbt profiles in `profiles.yml` or create `dbt_project.yml`
2. Run `dbt debug`

### Relevant log output

```
(env) $ dbt debug
13:46:12 Running with dbt=1.8.0-a1
13:46:12 dbt version: 1.8.0-a1
13:46:12 python version: 3.9.18
13:46:12 python path: /Users/dbeatty/projects/dbt-core/env/bin/python
13:46:12 os info: macOS-12.6.1-x86_64-i386-64bit
13:46:12 Encountered an error:
local variable 'summary_message' referenced before assignment
13:46:12 Traceback (most recent call last):
File "/Users/dbeatty/projects/dbt-core/core/dbt/cli/requires.py", line 97, in wrapper
result, success = func(*args, **kwargs)
File "/Users/dbeatty/projects/dbt-core/core/dbt/cli/requires.py", line 82, in wrapper
return func(*args, **kwargs)
File "/Users/dbeatty/projects/dbt-core/core/dbt/cli/main.py", line 420, in debug
results = task.run()
File "/Users/dbeatty/projects/dbt-core/core/dbt/task/debug.py", line 126, in run
load_profile_status: SubtaskStatus = self._load_profile()
File "/Users/dbeatty/projects/dbt-core/core/dbt/task/debug.py", line 213, in _load_profile
profile_names, summary_message = self._choose_profile_names()
File "/Users/dbeatty/projects/dbt-core/core/dbt/task/debug.py", line 292, in _choose_profile_names
return profiles, summary_message
UnboundLocalError: local variable 'summary_message' referenced before assignment
```

Add the following code in the [right place](https://github.com/dbt-labs/dbt-core/blob/2f2e0cee10094270a85c099b4035c4266b1ec926/core/dbt/task/debug.py#L274-L276):

```python
summary_message = f" profile path <{self.profile_path}> not found\n"
```

It will yield another problem to solve:

```
(env) $ dbt debug
13:46:30 Running with dbt=1.8.0-a1
13:46:30 dbt version: 1.8.0-a1
13:46:30 python version: 3.9.18
13:46:30 python path: /Users/dbeatty/projects/dbt-core/env/bin/python
13:46:30 os info: macOS-12.6.1-x86_64-i386-64bit
13:46:30 Using profiles dir at /Users/dbeatty/projects/dbt-core
13:46:30 Using profiles.yml file at /Users/dbeatty/projects/dbt-core/profiles.yml
13:46:30 Using dbt_project.yml file at /Users/dbeatty/projects/dbt-core/dbt_project.yml
13:46:30 Encountered an error:
Internal Error
Profile should not be None if loading profile completed
```

Add the following code in the [right place](https://github.com/dbt-labs/dbt-core/blob/2f2e0cee10094270a85c099b4035c4266b1ec926/core/dbt/task/debug.py#L233-L234):

```python
if len(profile_names) == 0:
profile_errors.append(summary_message)
```

Now it will give the expected output:

```
(env) $ dbt debug
13:46:52 Running with dbt=1.8.0-a1
13:46:52 dbt version: 1.8.0-a1
13:46:52 python version: 3.9.18
13:46:52 python path: /Users/dbeatty/projects/dbt-core/env/bin/python
13:46:52 os info: macOS-12.6.1-x86_64-i386-64bit
13:46:53 Using profiles dir at /Users/dbeatty/projects/dbt-core
13:46:53 Using profiles.yml file at /Users/dbeatty/projects/dbt-core/profiles.yml
13:46:53 Using dbt_project.yml file at /Users/dbeatty/projects/dbt-core/dbt_project.yml
13:46:53 Configuration:
13:46:53 profiles.yml file [ERROR invalid]
13:46:53 dbt_project.yml file [ERROR not found]
13:46:53 Required dependencies:
13:46:53 - git [OK found]

13:46:53 Connection test skipped since no profile was found
13:46:53 2 checks failed:
13:46:53 profile path not found
Profile loading failed for the following reason:
profile path not found

13:46:53 Project loading failed for the following reason:
project path not found
```

### Environment

```markdown
- OS: macOS
- Python: Python 3.9.18
- dbt: 1.8.0-a1
```

### Which database adapter are you using with dbt?

postgres

### Additional Context

Proposed fixes mentioned in the "Relevant log output" section above.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.