Repository: dbt-labs/dbt-core

Issue:
ref uses most recently referenced project rather than current
## Issue
The expected behavior for a reference with no specified project like `{{ref('model_one')}}` is that it will refer to the model "model_one" in the current project. However this is not the case when a ref earlier in the model specifies a project of its own. For instance were I to define a model in a project named "A" with these contents:
```SELECT * FROM {{ref('B', 'model_one')}}, {{ref('model_two')}}```, and then run `dbt docs generate` both model_one and model_two will be searched for in project "B", and dbt will throw an error (in this case, model_two is in the "A" project).

I believe this is due to the variable target_model_package being assigned in the outer loop of method process_refs.  https://github.com/fishtown-analytics/dbt/blob/master/core/dbt/parser/util.py#L163-L169 This causes each ref within a node to use the target_model_package of the previous ref as its default target_model_package, rather than None as I believe was intended.


While based on my reading of the docs and code I don't believe this is the intended behavior, if it is I would argue it should be changed. Having the order in which refs occur change the model they point at could for instance cause a stylistic change like refactoring a query to move part of it into a CTE to have a functional impact.

If this is indeed an issue I'd like to volunteer to make a PR for it!
### Issue description

Profile type "redshift"

### Results
```
(dbt-test-3)  ~/Test/proj_one  dbt docs generate
Running with dbt=0.13.1
Encountered an error:
Compilation Error in model model_one (models/example/model_one.sql)
  Model 'model.proj_one.model_one' depends on model 'model_two' in package 'proj_two' which was not found or is disabled
```
### System information
The output of `dbt --version`:
```
installed version: 0.13.1
   latest version: 0.13.1

Up to date!
```

The operating system you're running on:
Mac OS Mojave 10.14
The python version you're using (probably the output of `python --version`)
Python 3.6.2
### Steps to reproduce
1. dbt init two fresh projects, in this case we're calling them proj_one and proj_two.
2. change the "name" of these projects in the dbt_project.yml file to "proj_one" and "proj_two" respectively
3.delete the default example models in the models folder
4. create a file model_one.sql in the models folder of "proj_one" with this definition "select * from {{ref('proj_two', 'model_a')}}, {{ref('model_two')}}"
5. create a second file in the models folder of "proj_one" named "model_two.sql" with the contents "Select 1"
6. create a file in the models folder of "proj_two" with the name "model_a.sql" with the contents "select 1"
7. in proj_one, define a packages.yml file with these contents """
packages:
  - local: ../proj2
"""
8. run dbt deps in proj_one to install proj_two as a package
9. run dbt docs generate in proj_one. This will raise the error " Model 'model.proj_one.model_one' depends on model 'model_two' in package 'proj_two' which was not found or is disabled". This is incorrect, as the ref for model_two does not specify a project, so the current project should be used by default. Dbt instead appears to be using the project of the most recent ref.




Discussion:
Hey @tbescherer - thanks for the very comprehensive report! I think you're 100% right - we should be resetting the `target_model_package` inside of the loop over `node.refs`. I'd definitely welcome a PR for this change - don't hesitate to let me know how I can help!

Resolving patch:
diff --git a/core/dbt/parser/util.py b/core/dbt/parser/util.py
--- a/core/dbt/parser/util.py
+++ b/core/dbt/parser/util.py
@@ -173,11 +173,11 @@ def process_docs(cls, manifest, current_project):
     @classmethod
     def process_refs_for_node(cls, manifest, current_project, node):
         """Given a manifest and a node in that manifest, process its refs"""
-        target_model = None
-        target_model_name = None
-        target_model_package = None
-
         for ref in node.refs:
+            target_model = None
+            target_model_name = None
+            target_model_package = None
+
             if len(ref) == 1:
                 target_model_name = ref[0]
             elif len(ref) == 2:


Test patch: