Can a container inside a grouping be augmented in YANG?

The first module contains this:

    module a
    {
        namespace "my:namespace";
        prefix a;
        grouping mygrouping
        {
            container firstcontainer {
                list modules {
                    leaf firstmodule;
                    leaf secondmodule;
                }    
            }
        }
    }

Now I want to augment it in the second module as follows:

module b
{
    namespace "my:namespace";
    prefix a;
    import b {
        prefix b;
    }
    augment "/b:mygrouping/b:firstcontainer/b:modules"{
        leaf thirdmodule;
    }
}

This does not work, apparently because a grouping cannot be augmented. But there must be a way, since what I really want to extend is the list, not the grouping itself.

Is there another way to have the intended result using another way ?

Grouping are a conceptual aggregation of nodes, and they do not exist in the final schema i.e. when you use a uses xyz_grouping clause in schema, the compiler internally copies all the elements of xyz_grouping within it.

If you do not use any uses statement the grouping has no effect on final schema.

So in your example

module a
{
   .
   .
   .
    container commands-a {
        uses mygrouping;
    }
}

This would be equivalent of writing this from POV of compiler

module a
{
   .
   .
   .
    container commands-a {
            container firstcontainer {
                list modules {
                    leaf firstmodule;
                    leaf secondmodule;
                }    
          }        
    }
}

Then you can augment leaf thirdmodule onto that node that exist in the schema (in this case commands-a)

i.e.

module b
{
    namespace "my:namespace";
    prefix b;
    import  a {
        prefix a ;
    }
    augment "/a:commands-a/a:firstcontainer/a:modules"{
        leaf thirdmodule;
    }
}

(Disclaimer: The statements about confdc compiler are from my understanding by using it. I have not seen the code and hence may be wrong).

Another option is that augment can be a substatement on the uses statement, so rather than have a separate standalone augment statement, you can at the point where you “use” the grouping, also augment it whatever way you want.

Yes this works for me. Thank you.

But what if I want to augment an enumeration which is inside the grouping ?

You can’t augment a type.

If your requirement is to have dynamic enumerations (i.e. be able to add new enumerations), you should replace the enumeration with an identityref type. Refer to: https://tools.ietf.org/html/rfc8407#section-4.11.1 for an example.

Or, if you want to restrict certain values, you can use the ‘if-feature’ statement, or a ‘must’ statement.