Repository: fal-ai/dbt-fal

Issue:
fal run --scripts selector with global scripts not working
<!-- *** Make sure you have searched for an existing bug report for this issue *** -->

**Describe the bug**
Fal CLI `run` command using `--scripts` flag selector does not execute any script when the script passed is under the `before` key in the `schema.yml` configuration.

**Your environment**
- OS: macOS Monterrey 12.6
- Paste the following commands output:
```
fal --0.6.0
dbt --1.2.1
```
- Adapter being used: bigquery

**How to reproduce**
Add scripts to run under `--before` key in the `schema.yml`:
```
version: 2

fal:
  scripts:
    before:
      - fal_scripts/delete_bq_datasets.py
      - fal_scripts/download_prod_artifacts.py
```
File structure:
```
dbt_project
├── analysis
├── dbt_packages
├── dbt_project.yml
├── fal_scripts
│   ├── delete_bq_datasets.py
│   └── download_prod_artifacts.py
├── logs
├── macros
├── models
│   ├── exposures
│   └── schema.yml
├── packages.yml
├── seeds
├── snapshots
├── target
└── tests

```
Then run:
```sh
fal run --before --scripts download_prod_artifacts.py
```
Or:
```sh
fal run --scripts download_prod_artifacts.py
```

**Expected behavior**
Run only the script passed to the `--scripts` flag: `download_prod_artifacts.py`.  

**Actual behavior**
Does nothing for neither case.

```sh
fal run --scripts download_prod_artifacts.py
```
```
13:25:19  Found 370 models, 544 tests, 0 snapshots, 18 analyses, 583 macros, 0 operations, 42 seed files, 129 sources, 2 exposures, 0 metrics
13:25:19  Could not read dbt sources artifact
```
```sh
fal run --before --scripts download_prod_artifacts.py
```
```
13:27:34  Found 370 models, 544 tests, 0 snapshots, 18 analyses, 583 macros, 0 operations, 42 seed files, 129 sources, 2 exposures, 0 metrics
13:27:34  Could not read dbt sources artifact
```
**Additional context**
The issue might be [this](https://github.com/fal-ai/fal/blob/771ad3dc8946dbda57e91b188719f8a20c6eb353/src/fal/cli/fal_runner.py#L47) section of code (related with `scripts` and `global_scripts` variables).



Discussion:
Thanks for raising this issue, @jorserpl ! We're looking into it.

Resolving patch:
diff --git a/src/fal/cli/fal_runner.py b/src/fal/cli/fal_runner.py
--- a/src/fal/cli/fal_runner.py
+++ b/src/fal/cli/fal_runner.py
@@ -50,15 +50,15 @@ def fal_run(args: argparse.Namespace):
     models = _get_filtered_models(faldbt, args.all, selector_flags, args.before)
 
     scripts = _select_scripts(args, models, faldbt)
-
-    global_scripts = _get_global_scripts(faldbt, args.before)
+    global_scripts = _get_global_scripts(faldbt, args)
 
     if args.before:
-        if not _scripts_flag(args):
-            # run globals when no --script is passed
+        if not _scripts_flag(args) or not selector_flags:
+            # run globals when no --script is passed or no selector is passed
             _run_scripts(args, global_scripts, faldbt)
 
         pre_hook_scripts = _get_hooks_for_model(models, faldbt, "pre-hook")
+
         _run_scripts(args, pre_hook_scripts, faldbt)
 
         _run_scripts(args, scripts, faldbt)
@@ -69,7 +69,7 @@ def fal_run(args: argparse.Namespace):
         post_hook_scripts = _get_hooks_for_model(models, faldbt, "post-hook")
         _run_scripts(args, post_hook_scripts, faldbt)
 
-        if not _scripts_flag(args):
+        if not _scripts_flag(args) or not selector_flags:
             # run globals when no --script is passed
             _run_scripts(args, global_scripts, faldbt)
 
@@ -121,10 +121,12 @@ def _select_scripts(
     return scripts
 
 
-def _get_global_scripts(faldbt: FalDbt, is_before: bool):
+def _get_global_scripts(faldbt: FalDbt, args: argparse.Namespace):
+    scripts_flag = _scripts_flag(args)
     return [
         FalScript(faldbt, None, path)
-        for path in faldbt._global_script_paths["before" if is_before else "after"]
+        for path in faldbt._global_script_paths["before" if args.before else "after"]
+        if not scripts_flag or path in args.scripts
     ]
 
 


Test patch:
diff --git a/integration_tests/features/globals.feature b/integration_tests/features/globals.feature
--- a/integration_tests/features/globals.feature
+++ b/integration_tests/features/globals.feature
@@ -12,7 +12,16 @@ Feature: global scripts
     Then the following scripts are ran:
       | some_model.after.py | GLOBAL.after.py |
 
-  Scenario: fal run does not trigger globals with scripts flag
+  Scenario: fal run triggers globals with select flag
+    Given the project 004_globals
+    When the following command is invoked:
+      """
+      fal run --profiles-dir $profilesDir --project-dir $baseDir --select some_model
+      """
+    Then the following scripts are ran:
+      | some_model.after.py | GLOBAL.after.py |
+
+  Scenario: fal run triggers globals by default with before
     Given the project 004_globals
     When the following shell command is invoked:
       """
@@ -20,29 +29,25 @@ Feature: global scripts
       """
     When the following command is invoked:
       """
-      fal run --profiles-dir $profilesDir --project-dir $baseDir --scripts fal_scripts/after.py
+      fal run --profiles-dir $profilesDir --project-dir $baseDir --before
       """
     Then the following scripts are ran:
-      | some_model.after.py |
+      | GLOBAL.before.py | GLOBAL.before_b.py | some_model.before.py |
 
-  Scenario: fal run triggers globals with select flag
+  Scenario: Fal works with global before script selection
     Given the project 004_globals
     When the following command is invoked:
       """
-      fal run --profiles-dir $profilesDir --project-dir $baseDir --select some_model
+      fal run --profiles-dir $profilesDir --project-dir $baseDir --before --script fal_scripts/before_b.py
       """
     Then the following scripts are ran:
-      | some_model.after.py | GLOBAL.after.py |
+      | GLOBAL.before_b.py |
 
-  Scenario: fal run triggers globals by default with before
+  Scenario: Fal selects global and not-global scripts
     Given the project 004_globals
-    When the following shell command is invoked:
-      """
-      dbt run --profiles-dir $profilesDir --project-dir $baseDir
-      """
     When the following command is invoked:
       """
-      fal run --profiles-dir $profilesDir --project-dir $baseDir --before
+      fal run --profiles-dir $profilesDir --project-dir $baseDir --before --script fal_scripts/before.py
       """
     Then the following scripts are ran:
       | GLOBAL.before.py | some_model.before.py |
diff --git a/integration_tests/projects/004_globals/fal_scripts/before_b.py b/integration_tests/projects/004_globals/fal_scripts/before_b.py
new file mode 100644
--- /dev/null
+++ b/integration_tests/projects/004_globals/fal_scripts/before_b.py
@@ -0,0 +1,12 @@
+import os
+from functools import reduce
+
+model_name = context.current_model.name if context.current_model else "GLOBAL"
+
+output = f"Model name: {model_name}"
+
+temp_dir = os.environ["temp_dir"]
+print(temp_dir)
+write_dir = open(reduce(os.path.join, [temp_dir, model_name + ".before_b.txt"]), "w")
+write_dir.write(output)
+write_dir.close()
diff --git a/integration_tests/projects/004_globals/models/schema.yml b/integration_tests/projects/004_globals/models/schema.yml
--- a/integration_tests/projects/004_globals/models/schema.yml
+++ b/integration_tests/projects/004_globals/models/schema.yml
@@ -14,5 +14,6 @@ fal:
   scripts:
     before:
       - fal_scripts/before.py
+      - fal_scripts/before_b.py
     after:
       - fal_scripts/after.py