Repository: opentofu/opentofu
File: internal/tofu/transform_provider.go (Go)

Code before:
		switch v := v.(type) {
		case GraphNodeProviderConsumer:
			addr, ok = v.ProvidedBy().ProviderConfig.(addrs.AbsProviderConfig)
			if !ok {
				continue
			}
		case *graphNodeProxyProvider:
			addr = v.Target().ProviderAddr()
		default:
			// Since it can't be referenced, skip it
			continue
		}

		referencedProviders[addr.UniqueKey()] = struct{}{}
	}

	for _, p := range providerVerts {
		// If there are no resources on this provider, we change it
		// to a stub provider
		testProvider := p
		if proxied, ok := p.(*graphNodeProxyProvider); ok {
			testProvider = proxied.Target()
		}
		if _, ok := referencedProviders[testProvider.ProviderAddr().UniqueKey()]; !ok {
			g.Remove(testProvider)
			createStubProvider(g, providerVerts, testProvider.ProviderAddr())
		}
	}
	for _, v := range g.Vertices() {
		// Provider function references
		if nr, ok := v.(GraphNodeReferencer); ok && t.Config != nil {
			for _, ref := range nr.References() {
				if pf, ok := ref.Subject.(addrs.ProviderFunction); ok {
					refPath := nr.ModulePath()

					if outside, isOutside := v.(GraphNodeReferenceOutside); isOutside {
						_, refPath = outside.ReferenceOutside()
					}

					key := ProviderFunctionReference{
						ModulePath:    refPath.String(),
						ProviderName:  pf.ProviderName,
						ProviderAlias: pf.ProviderAlias,
					}

					// We already know about this provider and can link directly
					if provider, ok := providerReferences[key]; ok {
						// Is it worth skipping if we have already connected this provider?
						g.Connect(dag.BasicEdge(v, provider))
						continue
					}

Review comment (bug):
Not sure about this...
`g.Remove()` will remove the node and **all its edges**.
`createStubProvider` will generate new nodes with **no edges**.

This way there will be orphan provider nodes that will be pruned later.
Please correct me if I am wrong.

Code after:

	// Locate all providerVerts in the graph
	providerVerts := providerVertexMap(g)
	// LuT of provider reference -> provider vertex
	providerReferences := make(map[ProviderFunctionReference]dag.Vertex)

	for _, v := range g.Vertices() {
		// Provider function references
		if nr, ok := v.(GraphNodeReferencer); ok && t.Config != nil {
			for _, ref := range nr.References() {
				if pf, ok := ref.Subject.(addrs.ProviderFunction); ok {
					refPath := nr.ModulePath()

					if outside, isOutside := v.(GraphNodeReferenceOutside); isOutside {
						_, refPath = outside.ReferenceOutside()
					}

					key := ProviderFunctionReference{
						ModulePath:    refPath.String(),
						ProviderName:  pf.ProviderName,
						ProviderAlias: pf.ProviderAlias,
					}

					// We already know about this provider and can link directly
					if provider, ok := providerReferences[key]; ok {
						// Is it worth skipping if we have already connected this provider?
						g.Connect(dag.BasicEdge(v, provider))
						continue
					}

					// Find the config that this node belongs to
					mc := t.Config.Descendent(refPath)
					if mc == nil {
						// I don't think this is possible
						diags = diags.Append(&hcl.Diagnostic{
							Severity: hcl.DiagError,
							Summary:  "Unknown Descendent Module",
							Detail:   refPath.String(),
							Subject:  ref.SourceRange.ToHCL().Ptr(),
						})
						continue
					}

					// Find the provider type from required_providers
					pr, ok := mc.Module.ProviderRequirements.RequiredProviders[pf.ProviderName]
					if !ok {
						diags = diags.Append(&hcl.Diagnostic{
							Severity: hcl.DiagError,
							Summary:  "Unknown function provider",
							Detail:   fmt.Sprintf("Provider %q does not exist within the required_providers of this module", pf.ProviderName),
							Subject:  ref.SourceRange.ToHCL().Ptr(),