Issue #4748 has been updated by Paul Berry.

Status changed from Investigating to Needs design decision

What's happening here is that if Puppet can't find a variable
definition in the scope where it's referenced, it looks in
the "parent scope", and then if it can't find it there, tries all
ancestors (the parent's parent, then the parent's parent's
parent, and so on).  Parent scopes are assigned using the
following rules:

1. The parent of a derived class's scope is the scope of the base class.

2. The parent of any other class's scope is the scope of the
class that first included it or one of its derived classes.

So in the example above, the child -> parent relationships are:

* special -> toplevel

* foo -> base

* base -> toplevel (because base is included from toplevel before
  foo is included from special)

Foo's reference to $variable doesn't work because the definition
of $variable is in special, and special isn't an ancestor of foo.

However, if you comment out "include base", then the parent
relationships become:

* special -> toplevel

* foo -> base

* base -> special (because base is included only from special,
  via foo)

And now Foo's reference to $variable works, because special is an
ancestor of foo now.

Switching the order of "include base" and "include special" also
solves the problem, because it causes base to be included from
special (via foo) before it's included from toplevel.

I would prefer to fix this bug by replacing the "parent scope"
notion with a mechanism that isn't sensitive to order.  But in
order to do that I need to get a better understanding of how
variable scoping is intended to work.  I'll start a conversation
about this on the dev list.
----------------------------------------
Bug #4748: Variable scope confusing when including subclasses
http://projects.puppetlabs.com/issues/4748

Author: Felix Frank
Status: Needs design decision
Priority: Normal
Assignee: Paul Berry
Category: language
Target version: 
Affected version: development
Keywords: variables, scope, inheritance
Branch: 


When including a class that inherits another, variables used only in the 
inheriting class still seem to be scoped to the context of the original include 
of the base class.

<pre>
class base {
    file{"/tmp/foo": content => "base"}
}

class foo inherits base {
    notify{$variable:}
    File["/tmp/foo"] { content => $variable }
}

class special {
    $variable = "special"
    include foo
}

include base
include special
</pre>

With thanks to Volcane for the cleaned up example code.

Reproduced with versions 0.25.5 and 2.6.1rc4.


-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://projects.puppetlabs.com/my/account

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Bugs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-bugs?hl=en.

Reply via email to