Repository: dbt-labs/dbt-core

Issue:
compiled "analysis" code writing to wrong target directory
### Describe the bug
I have a custom path other than "analysis" for some SQL that I want compiled by not run. I ran
- dbt clean
- dbt deps
- dbt compile
The SQL files that are in my custom analysis path were successfully compiled. However the path where the compiled code lives is: /target/compiled/customer_data_model/analysis. I'd expect it to be in /target/compiled/customer_data_model/custom-analysis-path


### Steps To Reproduce

- I set my analysis path in my dbt_project to "test_paths."
- I created a model in the directory "test_paths" called "test.sql" with "select 1 as test" as the SQL
- I ran dbt clean, dbt deps, dbt compile
- The test.sql model was compiled but the resulting compiled code lives in target/compiled/customer_data_model/**analysis**

### Expected behavior
I'd expect the compiled code to live in target/compiled/customer_data_model/**test_paths**

### Screenshots and log output
If applicable, add screenshots or log output to help explain your problem.

### System information
**Which database are you using dbt with?**
- [x ] postgres
- [ ] redshift
- [ ] bigquery
- [ ] snowflake
- [ ] other (specify: ____________)


**The output of `dbt --version`:**
```
0.15.2
```

**The operating system you're using:**
10.15.3

**The output of `python --version`:**
3.7.4
### Additional context
Add any other context about the problem here.



Discussion:
Thanks for making this issue @ninashayne! I think this is a good suggestion, but I'm sort of on the fence about what the exact behavior should be. Do you think we should:
1. _never_ include resource-type subdirectories (`analysis/`, `tests/`, etc)?
2. _always_ include resource-type subdirectories?

I could either imagine throwing away the top-level folder name, or instead adding it to the path. 

That would mean that models would live in:
```
target/compiled/customer_data_model/models/...
```

And analyses would live in:
```
target/compiled/customer_data_model/analysis/...
```

I think that _always_ adding the top-level dir is probably a good idea, but I'm curious to hear what you think! Whatever we choose, we should just make sure to be consistent.
I like the idea of always adding it to the path since instinctively that's what i thought would happen. I just reread the initial issue and realize I left the expected subdirectory off in my initial summary (which might've triggered your question). I like the idea of the compiled code mimicking the resource subdirectory structure. 

To give a bit more color to why this even came up -- I want to use the compiled output of some analyses in a downstream process. Keeping them in their own directory helps with organization and standardization (my team can just add additional queries to a given directory without worrying about updating anything else). The downstream process can then just pick up the compiled SQL based on the name of the directory, without needing to know the names of specific analyses and also without picking up everything. 
I realize what happened... the markdown compilation hid the part that said 'custom-analysis-path' 
huh, ok @ninashayne - I read this issue totally differently this morning. I buy what you're saying here and i think we can make it happen :)
Thanks @drewbanin! Apologies for the confusion :) 

Resolving patch:
diff --git a/core/dbt/contracts/graph/parsed.py b/core/dbt/contracts/graph/parsed.py
--- a/core/dbt/contracts/graph/parsed.py
+++ b/core/dbt/contracts/graph/parsed.py
@@ -158,8 +158,15 @@ class ParsedNodeDefaults(ParsedNodeMandatory):
     build_path: Optional[str] = None
 
     def write_node(self, target_path: str, subdirectory: str, payload: str):
+        if (os.path.basename(self.path) ==
+                os.path.basename(self.original_file_path)):
+            # One-to-one relationship of nodes to files.
+            path = self.original_file_path
+        else:
+            #  Many-to-one relationship of nodes to files.
+            path = os.path.join(self.original_file_path, self.path)
         full_path = os.path.join(
-            target_path, subdirectory, self.package_name, self.path
+            target_path, subdirectory, self.package_name, path
         )
 
         write_file(full_path, payload)


Test patch:
diff --git a/test/integration/029_docs_generate_tests/test_docs_generate.py b/test/integration/029_docs_generate_tests/test_docs_generate.py
--- a/test/integration/029_docs_generate_tests/test_docs_generate.py
+++ b/test/integration/029_docs_generate_tests/test_docs_generate.py
@@ -903,7 +903,7 @@ def expected_seeded_manifest(self, model_database=None):
         return {
             'nodes': {
                 'model.test.model': {
-                    'build_path': Normalized('target/compiled/test/model.sql'),
+                    'build_path': Normalized('target/compiled/test/models/model.sql'),
                     'name': 'model',
                     'root_path': self.test_root_dir,
                     'resource_type': 'model',
@@ -1050,7 +1050,7 @@ def expected_seeded_manifest(self, model_database=None):
                 },
                 'test.test.not_null_model_id': {
                     'alias': 'not_null_model_id',
-                    'build_path': Normalized('target/compiled/test/schema_test/not_null_model_id.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/not_null_model_id.sql'),
                     'column_name': 'id',
                     'columns': {},
                     'config': {
@@ -1103,7 +1103,7 @@ def expected_seeded_manifest(self, model_database=None):
                 },
                 'test.test.test_nothing_model_': {
                     'alias': 'test_nothing_model_',
-                    'build_path': Normalized('target/compiled/test/schema_test/test_nothing_model_.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/test_nothing_model_.sql'),
                     'column_name': None,
                     'columns': {},
                     'config': {
@@ -1155,7 +1155,7 @@ def expected_seeded_manifest(self, model_database=None):
                 },
                 'test.test.unique_model_id': {
                     'alias': 'unique_model_id',
-                    'build_path': Normalized('target/compiled/test/schema_test/unique_model_id.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/unique_model_id.sql'),
                     'column_name': 'id',
                     'columns': {},
                     'config': {
@@ -1274,7 +1274,7 @@ def expected_postgres_references_manifest(self, model_database=None):
             'nodes': {
                 'model.test.ephemeral_copy': {
                     'alias': 'ephemeral_copy',
-                    'build_path': Normalized('target/compiled/test/ephemeral_copy.sql'),
+                    'build_path': Normalized('target/compiled/test/ref_models/ephemeral_copy.sql'),
                     'columns': {},
                     'config': {
                         'column_types': {},
@@ -1320,7 +1320,7 @@ def expected_postgres_references_manifest(self, model_database=None):
                 },
                 'model.test.ephemeral_summary': {
                     'alias': 'ephemeral_summary',
-                    'build_path': Normalized('target/compiled/test/ephemeral_summary.sql'),
+                    'build_path': Normalized('target/compiled/test/ref_models/ephemeral_summary.sql'),
                     'columns': {
                         'first_name': {
                             'description': 'The first name being summarized',
@@ -1383,7 +1383,7 @@ def expected_postgres_references_manifest(self, model_database=None):
                 },
                 'model.test.view_summary': {
                     'alias': 'view_summary',
-                    'build_path': Normalized('target/compiled/test/view_summary.sql'),
+                    'build_path': Normalized('target/compiled/test/ref_models/view_summary.sql'),
                     'columns': {
                         'first_name': {
                             'description': 'The first name being summarized',
@@ -1729,7 +1729,7 @@ def expected_bigquery_complex_manifest(self):
                     'sources': [],
                     'depends_on': {'macros': [], 'nodes': ['seed.test.seed']},
                     'fqn': ['test', 'clustered'],
-                    'build_path': Normalized('target/compiled/test/clustered.sql'),
+                    'build_path': Normalized('target/compiled/test/bq_models/clustered.sql'),
                     'name': 'clustered',
                     'original_file_path': clustered_sql_path,
                     'package_name': 'test',
@@ -1791,7 +1791,7 @@ def expected_bigquery_complex_manifest(self):
                 },
                 'model.test.multi_clustered': {
                     'alias': 'multi_clustered',
-                    'build_path': Normalized('target/compiled/test/multi_clustered.sql'),
+                    'build_path': Normalized('target/compiled/test/bq_models/multi_clustered.sql'),
                     'config': {
                         'cluster_by': ['first_name', 'email'],
                         'column_types': {},
@@ -1869,7 +1869,7 @@ def expected_bigquery_complex_manifest(self):
                 },
                 'model.test.nested_view': {
                     'alias': 'nested_view',
-                    'build_path': Normalized('target/compiled/test/nested_view.sql'),
+                    'build_path': Normalized('target/compiled/test/bq_models/nested_view.sql'),
                     'config': {
                         'column_types': {},
                         'enabled': True,
@@ -1948,7 +1948,7 @@ def expected_bigquery_complex_manifest(self):
                 },
                 'model.test.nested_table': {
                     'alias': 'nested_table',
-                    'build_path': Normalized('target/compiled/test/nested_table.sql'),
+                    'build_path': Normalized('target/compiled/test/bq_models/nested_table.sql'),
                     'config': {
                         'column_types': {},
                         'enabled': True,
@@ -2131,7 +2131,7 @@ def expected_redshift_incremental_view_manifest(self):
         return {
             'nodes': {
                 'model.test.model': {
-                    'build_path': Normalized('target/compiled/test/model.sql'),
+                    'build_path': Normalized('target/compiled/test/rs_models/model.sql'),
                     'name': 'model',
                     'root_path': self.test_root_dir,
                     'resource_type': 'model',
@@ -2392,7 +2392,7 @@ def expected_run_results(self, quote_schema=True, quote_model=False,
                 'node': {
                     'alias': 'model',
                     'build_path': Normalized(
-                        'target/compiled/test/model.sql'
+                        'target/compiled/test/models/model.sql'
                     ),
                     'columns': {
                         'id': {
@@ -2559,7 +2559,7 @@ def expected_run_results(self, quote_schema=True, quote_model=False,
                 'warn': None,
                 'node': {
                     'alias': 'not_null_model_id',
-                    'build_path': Normalized('target/compiled/test/schema_test/not_null_model_id.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/not_null_model_id.sql'),
                     'column_name': 'id',
                     'columns': {},
                     'compiled': True,
@@ -2622,7 +2622,7 @@ def expected_run_results(self, quote_schema=True, quote_model=False,
                 'warn': None,
                 'node': {
                     'alias': 'test_nothing_model_',
-                    'build_path': Normalized('target/compiled/test/schema_test/test_nothing_model_.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/test_nothing_model_.sql'),
                     'column_name': None,
                     'columns': {},
                     'compiled': True,
@@ -2684,7 +2684,7 @@ def expected_run_results(self, quote_schema=True, quote_model=False,
                 'warn': None,
                 'node': {
                     'alias': 'unique_model_id',
-                    'build_path': Normalized('target/compiled/test/schema_test/unique_model_id.sql'),
+                    'build_path': Normalized('target/compiled/test/models/schema.yml/schema_test/unique_model_id.sql'),
                     'column_name': 'id',
                     'columns': {},
                     'compiled': True,
@@ -2774,7 +2774,7 @@ def expected_postgres_references_run_results(self):
                 'node': {
                     'alias': 'ephemeral_summary',
                     'build_path': Normalized(
-                        'target/compiled/test/ephemeral_summary.sql'
+                        'target/compiled/test/ref_models/ephemeral_summary.sql'
                     ),
                     'columns': {
                         'first_name': {
@@ -2853,7 +2853,7 @@ def expected_postgres_references_run_results(self):
                 'node': {
                     'alias': 'view_summary',
                     'build_path': Normalized(
-                        'target/compiled/test/view_summary.sql'
+                        'target/compiled/test/ref_models/view_summary.sql'
                     ),
                     'alias': 'view_summary',
                     'columns': {