Infinite loop refreshing view with EnvironmentValue

Adding environment value openURL or dismiss to a View in a NavigationStack, without even using it, causes an infinite refresh loop.

What doesn't work:

a)

struct ViewA: View {
    @State private var path = NavigationPath()
    
    var body: some View {
        NavigationStack(path: $path) {
            ViewB()
        }
    }
}

struct ViewB: View {
    @Environment(\.openURL) var openURL
    
    var body: some View {
        NavigationLink("Next", value: 1)
            .navigationDestination(for: Int.self, destination: itemView)
    }
    
    func itemView(_ item: Int) -> some View {
        Text("Item \(item)")
    }
}

Prints ViewB: _openURL changed. infinitely.

b) Passing the path to ViewB and appending the value with a Button

What works:

a)

.navigationDestination(for: Int.self) {
    Text("Item \($0)")
}

Prints

ViewB: @self, @identity, _openURL changed.
ViewB: @self, _openURL changed.
ViewB: _openURL changed. (3 times)

b) Handling the destination on ViewA, which is not ideal for my use case.

Prints

ViewB: @self, @identity, _openURL changed.
ViewB: _openURL changed. (5 times)

While the workaround would work, it is still unclear how the environment value can cause the freeze (and eventual crash). Also that passing a function as parameter fails, while providing the destination in place does not. The code is stripped down to the minimal reproducible version. Any thoughts?

Observing the same behaviour even without NavigationStack. It can be reproduced with simpler code.

Xcode 15.2, iOS 17.2.

struct ContentView: View {
    @Environment(\.openURL) private var openURL

    var body: some View {
        let _ = Self._printChanges()

        Text("Hello, world!")
    }
}

This code prints out:

ContentView: @self, @identity, _openURL changed.
ContentView: _openURL changed.

If I comment out openURL's Environment variable, the code prints out expected one redraw:

ContentView: @self changed.

This behaviour seems expected, but I cannot find any information on how to mitigate the redraws.

Hiyooo adding my predicament to this list as well. If you happen to display a sheet/popover attached to this view, the redraws will cause them to close.

I'm actually OK with the redraws, so long as they don't close the sheet/popover :)

Infinite loop refreshing view with EnvironmentValue
 
 
Q