MetaCodable icon indicating copy to clipboard operation
MetaCodable copied to clipboard

[Feature Request] No HelperCoder generated when check compilation condition

Open mo5tone opened this issue 1 year ago • 6 comments

Describe the bug Below code works well.

working code
@Codable
@CodedAs<String?>
@CodedAt("operation")
protocol ResponseAttributes {}

@Codable
struct Response {
    @CodedIn("data")
    let id: String
    @CodedIn("data")
    let type: String
    @CodedIn("data")
    @CodedBy(ResponseAttributesCoder())
    let attributes: ResponseAttributes
}

@Codable
struct RegistrationAttributes: ResponseAttributes, DynamicCodable {
    static var identifier: DynamicCodableIdentifier<String?> { .one("REGISTRATION") }
    let id: String
    @CodedAt("status-code")
    let statusCode: String
    let operation: String
}
@Codable
struct VerificationAttributes: ResponseAttributes, DynamicCodable {
    static var identifier: DynamicCodableIdentifier<String?> { .one(nil) }
    let id: String
    let operation: String?
    let expiresIn: UInt
    @CodedAt("xxx-token")
    let xxxToken: String
    @CodedAt("yyy-token")
    let yyyToken: String
}

But the ResponseAttributesCoder will be missing if add #if SOME_SWIFT_ACTIVE_COMPILATION_CONDITION.

Xcode say 'Cannot find 'AnonymousAttestationResponseAttributesCoder' in scope'.

failed to work
// Have set the `SOME_SWIFT_ACTIVE_COMPILATION_CONDITION` in `swiftSettings` from `Package.swift`.
#if SOME_SWIFT_ACTIVE_COMPILATION_CONDITION
@Codable
@CodedAs<String?>
@CodedAt("operation")
protocol ResponseAttributes {}

@Codable
struct Response {
    @CodedIn("data")
    let id: String
    @CodedIn("data")
    let type: String
    @CodedIn("data")
    @CodedBy(ResponseAttributesCoder())
    let attributes: ResponseAttributes
}

@Codable
struct RegistrationAttributes: ResponseAttributes, DynamicCodable {
    static var identifier: DynamicCodableIdentifier<String?> { .one("REGISTRATION") }
    let id: String
    @CodedAt("status-code")
    let statusCode: String
    let operation: String
}
@Codable
struct VerificationAttributes: ResponseAttributes, DynamicCodable {
    static var identifier: DynamicCodableIdentifier<String?> { .one(nil) }
    let id: String
    let operation: String?
    let expiresIn: UInt
    @CodedAt("xxx-token")
    let xxxToken: String
    @CodedAt("yyy-token")
    let yyyToken: String
}
#endif

To Reproduce Steps to reproduce the behavior:

  1. Create an empty swift package
  2. Add MetaCodable to dependencies
  3. Copy and paste the code above
  4. commnad + B

Expected behavior Compilation condition check shouldn't fail plugin or macro.

Environment (please complete the following information, remove ones not applicable):

  • OS: macOS
  • Version 13.6.6
  • Xcode 15.2
  • Swift 5.9.2

mo5tone avatar May 09 '24 06:05 mo5tone

@mo5tone build tool plugins don't have access to build settings, hence you are seeing no HelperCoder being generated. What is the use case you are trying to achieve here?

soumyamahunt avatar May 13 '24 15:05 soumyamahunt

@mo5tone build tool plugins don't have access to build settings, hence you are seeing no HelperCoder being generated. What is the use case you are trying to achieve here?

I am building a private swift package to deliver some common functionalities across projects. Some projects use non-standard dependency management like tuist which map swift package to project but cannot handle macro and plugin properly.

So I want to use compilation condition to skip usage of macro and plugin on compile time when my package is converted by tuist and does not affect other projects that use swift package manager.

mo5tone avatar May 14 '24 02:05 mo5tone

There is a new module for swift-syntax and it seems that with it it would be possible to respect compilation conditions https://github.com/swiftlang/swift-syntax/pull/1816

Gray-Wind avatar Jul 12 '24 09:07 Gray-Wind

I am building a private swift package to deliver some common functionalities across projects. Some projects use non-standard dependency management like tuist which map swift package to project but cannot handle macro and plugin properly.

So I want to use compilation condition to skip usage of macro and plugin on compile time when my package is converted by tuist and does not affect other projects that use swift package manager.

I found that tuist can work well with another macro package but not MetaCodable.

I have raised https://github.com/tuist/tuist/issues/6579.

mo5tone avatar Aug 29 '24 00:08 mo5tone

There is a new module for swift-syntax and it seems that with it it would be possible to respect compilation conditions swiftlang/swift-syntax#1816

@Gray-Wind this feature seems to be under active development and hasn't been released yet, will have a look on how this could possibly solve this issue once it is released.

soumyamahunt avatar Oct 01 '24 04:10 soumyamahunt

I am building a private swift package to deliver some common functionalities across projects. Some projects use non-standard dependency management like tuist which map swift package to project but cannot handle macro and plugin properly.

@mo5tone Can you try just including MetaCodable as a Swift package instead of mapping into project. You can find the integration example in the Examples folder of this repo. I think the internal hacks being done by tuist to map a Swift package to project is failing here.

soumyamahunt avatar Oct 01 '24 04:10 soumyamahunt