Repository: dbt-labs/dbt-core
Pull request: Attach cli vars to configs when they come in via the RPC server (#2092)
Fixes #2092 

This fixes #2092 by attaching the parsed CLI `vars` to the config when running the "cli_args" RPC command. This has the downside of clobbering any arguments provided via `dbt rpc --vars "{...}"`. Hopefully that's a sacrifice we're willing to make!

Problem statement:
[BUG] Command line variable ignored when supplied to command in server
### Describe the bug
When I run this command against the `cli` method in the server:
```
dbt run --vars '{period_start_date: '2019-04-01', period: 'day'}' -m buildings
```
the command line variables are ignored. Instead I would expect the command line variables to be present in the context. 

Note: partial parsing is enabled in this environment

### Steps To Reproduce
tbd


**The output of `dbt --version`:**
```
O.15.2
```

Hints from issue discussion:
Hi, this also happens when we run a job and pass to it variables options as part of the dbt run command.

Commit message: Attach cli vars to configs when they come in via the RPC server

Patch to review:
diff --git a/core/dbt/task/rpc/cli.py b/core/dbt/task/rpc/cli.py
index e3bb477c454..b0fc3e0da6f 100644
--- a/core/dbt/task/rpc/cli.py
+++ b/core/dbt/task/rpc/cli.py
@@ -12,6 +12,7 @@
     Result,
 )
 from dbt.exceptions import InternalException
+from dbt.utils import parse_cli_vars
 
 from .base import RPCTask
 
@@ -36,6 +37,11 @@ def __init__(self, args, config, manifest):
 
     def set_config(self, config):
         super().set_config(config)
+
+        # patch up config with args/cli_vars
+        self.config.args = self.args
+        self.config.cli_vars = parse_cli_vars(getattr(self.args, 'vars', '{}'))
+
         if self.task_type is None:
             raise InternalException('task type not set for set_config')
         if issubclass(self.task_type, RemoteManifestMethod):
diff --git a/test/rpc/test_base.py b/test/rpc/test_base.py
index 140741bb267..14694f155a3 100644
--- a/test/rpc/test_base.py
+++ b/test/rpc/test_base.py
@@ -863,3 +863,25 @@ def test_missing_tag_sighup(
         querier.sighup()
 
         assert querier.wait_for_status('ready') is True
+
+
+def test_rpc_vars(
+    project_root, profiles_root, postgres_profile, unique_schema
+):
+    project = ProjectDefinition(
+        models={
+            'my_model.sql': 'select {{ var("param") }} as id',
+        },
+    )
+    querier_ctx = get_querier(
+        project_def=project,
+        project_dir=project_root,
+        profiles_dir=profiles_root,
+        schema=unique_schema,
+        test_kwargs={},
+    )
+
+    with querier_ctx as querier:
+        results = querier.async_wait_for_result(querier.cli_args('run --vars "{param: 100}"'))
+        assert len(results['results']) == 1
+        assert results['results'][0]['node']['compiled_sql'] == 'select 100 as id'


Code review comments:
diff_hunk: @@ -36,6 +37,11 @@ def __init__(self, args, config, manifest):
 
     def set_config(self, config):
         super().set_config(config)
+
+        # patch up config with args/cli_vars
+        self.config.args = self.args
+        self.config.cli_vars = parse_cli_vars(getattr(self.args, 'vars', '{}'))
path: core/dbt/task/rpc/cli.py
text: @user1:
Can we update the request-supplied `--vars` on top of any vars supplied to `dbt rpc`? I like the idea of the rpc-runner being able to specify base variables that are then updated in the scope of a particular request.

If this is onerous or anything other than a 1-line change to this PR, then we should discuss it further!

@author:
I think we'd also want to patch up `self.args.vars` (due to the `config.new_project()` method), but that should be no big deal.