Inserting gitInformation into source code?

This post mentions a PR for Context.gitInformation, which provides information like the current git commit and tag.

I would like for my running app (a Vapor app) to be able to get at this information from when it was built (in order to show version information to the user or for support purposes). I don’t see an obvious way to do this. The best I can gather is I need a build tool plugin to generate a Swift file.

But I can't tell if there’s a way to get the git info to the plugin, or if the plugin has a simlar property.

2 Likes

Yes, there should be e.g. a macro #packageVersions making a (literal) map with the versions as part of the standard library. You cannot do this with your own macro that reads the Package.resolved file, because reading files is prohibited for a macro because of security reasons.

Take a look at what Swift Testing does: we create a C/C++ target, define C macros in that target that correspond to the gitInformation fields we care about, then bubble those macros up to Swift.

There's probably room here to improve the ergonomics of this structure by exposing it directly to packages' Swift sources (somehow.) Worth filing a GitHub issue.

Done. (I was not sure if the issue should be for Swift or for Foundation, but certainly someone will move it if necessary.)

The mentioned GutHub issue is untouched since its creation, so I would like revive this topic again (the topic is not too old and has very few contributors, so I think this should be OK).

I think being able to get the version of a package during compilation in an easy way could be in the interest of many. It would be nice if it could be realized.

1 Like