add check for circular dependency

@tfoote @wjwwood Please review.