Recompilation when caching .build folder on CI

I'm currently trying to optimise CI builds for a server-side application on Gitlab CI.
In a first stage, we're trying to compile the Swift project with swift build --build-tests
The resulting .build folder is cached for the next stage, where we run swift test

What I would expect
In the swift test stage, we don't need to recompile anything.

What happens instead
Some of the code is recompiled for some reason, but apparently not everything.

I've created a sample project on Gitlab to showcase the issue. The behaviour can be observed by looking at the pipeline execution. (note: gitlab CI actually calls the "cache" between different stages "artifacts".)

I have in the past observed similar behaviour in docker containers where some code was being rebuilt even though the swift build step had been cached.

I don't understand why this happens. I could understand that things need to recompiled when the host machine changes, but given that everything is executed in a docker environment, the architecture etc. should be the same? Can anybody shed some light on this?

It’s probably a variation of this:

1 Like

Yep... looks like the same issue.
I don't suppose there is some kind of workaround for now? :/

If your cache only matters between stages, then you could merge them into one so the build artifacts don’t need to be copied in and out.

But in general I’m not sure that anyone has been able to isolate the root cause yet, so I know of no workaround when another process does need to copy the artifacts.