krzysztofzablocki / Sourcery

Meta-programming for Swift, stop writing boilerplate code.
http://merowing.info
MIT License
7.69k stars 619 forks source link

Unable to honor environment variables that end in "==" #1118

Open piofusco opened 1 year ago

piofusco commented 1 year ago

I have a secret that ends in "==" and am having trouble using sourcery to convert that secret into my stencil.

Steps to reproduce:

  1. Install Sourcery with brew brew install sourcery
  2. Create a simple stencil, Secrets.stencil like the following
public struct Secrets {
    public var vendor: VendorSecrets

    public struct VendorSecrets {
        public var credentials: String
    }
}

public var secrets = Secrets(
    vendor: .init(
        credentials: "{{ argument.VENDOR_CREDENTIALS }}"
    )
)
  1. Run the following command in a terminal
    export VENDOR_CREDENTIALS="banana==" && sourcery \
    --templates . \
    --sources . \
    --output . \
    --args VENDOR_CREDENTIALS=$VENDOR_CREDENTIALS

Result: Sourcery will produce Secrets.generated.stencil that looks like the following

// Generated using Sourcery 1.9.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
public struct Secrets {
    public var vendor: VendorSecrets

    public struct VendorSecrets {
        public var credentials: String
    }
}

public var secrets = Secrets(
    vendor: .init(
        credentials: "1" // Uh oh
    )
)

Expected result: Sourcery will produce Secrets.generated.stencil that looks like the following

// Generated using Sourcery 1.9.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT
public struct Secrets {
    public var vendor: VendorSecrets

    public struct VendorSecrets {
        public var credentials: String
    }
}

public var secrets = Secrets(
    vendor: .init(
        credentials: "banana=="
    )
)

OS: macOS 12.6.1 (M1 Max)

art-divin commented 10 months ago

Seems like when the environment variable is expanded, Stencil template is considering == as an actual operator, thus providing output 1. Unsure which comparison is used in runtime, but that should be the reason.

I'll keep this in the back of my head, thanks for reporting!