0

The code below is located in the application module:

enum API {}

extension API {
    protocol Endpoint: Sendable {
        var basePath: String { get }
        var apiName: String { get }
        var urlComponents: URLComponents? { get }
    }
}

extension API.Endpoint {
    var basePath: String { "/api/v2/" }
}

extension API {
    enum AutorizationEndpoint: String {
        case login
        case logout
    }
}

extension API.AutorizationEndpoint: API.Endpoint {
    var apiName: String { "auth/" }
    var urlComponents: URLComponents? {
        let path = basePath + apiName + rawValue
        return URLComponents(string: path)
    }
}

Now I want to test:

@Test("Endpoint URLComponents validation", arguments: [
    API.AutorizationEndpoint.login,
    API.AutorizationEndpoint.logout,
])
func testComponentsNotNil(_ endpoint: API.Endpoint) {
    #expect(endpoint.urlComponents != nil)
}

However, the compiler produces the following error:

Main actor-isolated property 'urlComponents' cannot be accessed from outside of the actor

Why? What happened? I don't mark any protocol or enum with @MainActor directly. This is a fairly simple test and there shouldn't be any problems with it.

Environment: macOS 26.1; Exode 26.1.1; Swift 6

2
  • I suspect you have the default actor isolation setting set to main actor (the default). Please let me know if setting that to nonisolated fixes the issue. Commented Nov 24 at 6:55
  • @Sweeper, Yes, setting the value to "nonisolated" solves the problem. It's a very strange decision to isolate the entire to MainActor. Commented Nov 24 at 12:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.