@@ -99,17 +99,20 @@ def load(cls, context: DbtContext, variables: t.Optional[t.Dict[str, t.Any]] = N
9999 package = package_loader .load (path .parent )
100100 packages [package .name ] = package
101101
102+ # Variable resolution precedence:
103+ # 1. Variable overrides
104+ # 2. Package-scoped variables in the root project's dbt_project.yml
105+ # 3. Global project variables in the root project's dbt_project.yml
106+ # 4. Variables in the package's dbt_project.yml
102107 all_project_variables = {** (project_yaml .get ("vars" ) or {}), ** (variable_overrides or {})}
103108 for name , package in packages .items ():
104- package_vars = all_project_variables .get (name )
105-
106- if isinstance (package_vars , dict ):
107- package .variables .update (package_vars )
108-
109- if name == context .project_name :
110- package .variables .update (all_project_variables )
109+ if isinstance (all_project_variables .get (name ), dict ):
110+ project_vars_copy = all_project_variables .copy ()
111+ package_scoped_vars = project_vars_copy .pop (name )
112+ package .variables .update (project_vars_copy )
113+ package .variables .update (package_scoped_vars )
111114 else :
112- package .variables .update (variable_overrides )
115+ package .variables .update (all_project_variables )
113116
114117 return Project (context , profile , packages )
115118
0 commit comments