// validateProviderAlias validates that all provider alias references are
// defined at some point in the parent tree. This improves UX by catching
// alias typos at the slight cost of requiring a declaration of usage. This
// is usually a good tradeoff since not many aliases are used.
func (t *Tree) validateProviderAlias() error {
	// If we're not the root, don't perform this validation. We must be the
	// root since we require full tree visibilty.
	if len(t.path) != 0 {
		return nil
	}

	// We'll use a graph to keep track of defined aliases at each level.
	// As long as a parent defines an alias, it is okay.
	var g dag.AcyclicGraph
	t.buildProviderAliasGraph(&g, nil)

	// Go through the graph and check that the usage is all good.
	var err error
	for _, v := range g.Vertices() {
		pv, ok := v.(*providerAliasVertex)
		if !ok {
			// This shouldn't happen, just ignore it.
			continue
		}

		// If we're not using any aliases, fast track and just continue
		if len(pv.Used) == 0 {
			continue
		}

		// Grab the ancestors since we're going to have to check if our
		// parents define any of our aliases.
		var parents []*providerAliasVertex
		ancestors, _ := g.Ancestors(v)
		for _, raw := range ancestors.List() {
			if pv, ok := raw.(*providerAliasVertex); ok {
				parents = append(parents, pv)
			}
		}
		for k, _ := range pv.Used {
			// Check if we define this
			if _, ok := pv.Defined[k]; ok {
				continue
			}

			// Check for a parent
			found := false
			for _, parent := range parents {
				_, found = parent.Defined[k]
				if found {
					break
				}
			}
			if found {
				continue
			}

			// We didn't find the alias, error!
			err = multierror.Append(err, fmt.Errorf(
				"module %s: provider alias must be defined by the module or a parent: %s",
				strings.Join(pv.Path, "."), k))
		}
	}

	return err
}