Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20250627-093325.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Set `flags.INVOCATION_COMMAND` for programmatic invocations on a best-effort basis
time: 2025-06-27T09:33:25.560217-06:00
custom:
Author: dbeatty10
Issue: "10313"
5 changes: 3 additions & 2 deletions core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ def _assign_params(
else:
project_flags = None

# Add entire invocation command to flags
object.__setattr__(self, "INVOCATION_COMMAND", "dbt " + " ".join(sys.argv[1:]))
# Add entire invocation command to flags on a best-effort basis (either from env var or from sys.argv)
invocation_command = os.getenv("DBT_EQUIVALENT_COMMAND", "dbt " + " ".join(sys.argv[1:]))
object.__setattr__(self, "INVOCATION_COMMAND", invocation_command)

if project_flags:
# Overwrite default assignments with project flags if available.
Expand Down
45 changes: 45 additions & 0 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import os
import shlex
from copy import copy
from dataclasses import dataclass
from typing import Callable, List, Optional, Union
Expand Down Expand Up @@ -37,6 +39,43 @@
] = None


def get_equivalent_cli_flag(config_name: str, positive_config: bool = True) -> str:
"""Convert a config name to its equivalent CLI flag"""
if positive_config:
return f"--{config_name.replace('_', '-')}"
else:
return f"--no-{config_name.replace('_', '-')}"


def get_equivalent_cli_command(args: List[str], **kwargs) -> str:
"""Convert args and kwargs to a CLI command string that can be used to invoke dbt on a best-effort basis"""

# Convert all values in `args` to strings
cli_args = [str(arg) for arg in args]

# Convert each keyword arg to its CLI flag equivalent
for key, value in kwargs.items():
if type(value) is bool:
# Add just the flag (without the value) for booleans
cli_args.append(get_equivalent_cli_flag(key, value))
elif type(value) in {list, tuple}:
# Add both the flag and its values for lists/tuples
cli_args.append(get_equivalent_cli_flag(key))
cli_args.extend(map(str, value))

Check warning on line 64 in core/dbt/cli/main.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/main.py#L63-L64

Added lines #L63 - L64 were not covered by tests
else:
# Add both the flag and its value for other types
# This is a best-effort conversion, so we ignore exceptions
try:
cli_args.extend([get_equivalent_cli_flag(key), str(value)])
except Exception:
pass

Check warning on line 71 in core/dbt/cli/main.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/cli/main.py#L70-L71

Added lines #L70 - L71 were not covered by tests

# Ensure all CLI arguments are quoted as needed
cli_args = [shlex.quote(v) for v in cli_args]

return "dbt " + " ".join(cli_args)


# Programmatic invocation
class dbtRunner:
def __init__(
Expand Down Expand Up @@ -64,6 +103,12 @@
# Hack to set parameter source to custom string
dbt_ctx.set_parameter_source(key, "kwargs") # type: ignore

# Get equivalent CLI command from args
equivalent_command = get_equivalent_cli_command(args, **kwargs)

# Store the invocation command in an environment variable for access within `cli/flags.py`
os.environ["DBT_EQUIVALENT_COMMAND"] = equivalent_command

result, success = cli.invoke(dbt_ctx)
return dbtRunnerResult(
result=result,
Expand Down