yonaskolb / XcodeGen

A Swift command line tool for generating your Xcode project
MIT License
6.88k stars 809 forks source link

Xcodegen cache bug? #1457

Closed Snikss closed 1 month ago

Snikss commented 4 months ago

I have a git hook post-checkout, when i checkout i start a script it's just a one line - "xcodegen generate --use-cache". So the bug is that it's not work in my case. My case: Everything happens in local repo. I create new branch from develop for example branch-1. I create a new file on branch-1 with some swift class, commit it in local repo and then go back to develop. When i checkout hook start a command - "xcodegen generate --use-cache" but it says "Project ProjectName has not changed since cache was written", but develop doesn't has a new file which i created on branch-1. For this case i need to write xcodegen generate on terminal by hand. Whats wrong why it doesn't work and how fix it?

yonaskolb commented 2 months ago

Yeah it's possible to get some false positive cache hits when regenerating a project that is in the same project configuration state it was in last time, but the underlying xcode project has had a change made via the interface. This happens as the cache uses the project spec and the files in all sources as the cache key. If you make changes to the project like adding a file, XcodeGen doesn't know about this. When you revert the changes, and try and regenerate, XcodeGen doesn't know the underlying project has been changed.

Thinking about it now, XcodeGen could theoretically use the xcode project contents as part of the cache key, but that could also lead to less caching as the project could change underneath as Xcode saves it's own state, and doesn't always 100% equal what XcodeGen generates.

One workaround for now, is running xcodegen with cache after each commit (in a post-commit hook). That would add your new file to the cache, so that when you go back to develop it notices a change. But this is perhaps non workable if you use swift packages, as Xcode likes to resolve packages each time it detects project changes (or is opened), which can get annoying rather quickly

Thoughts?

yonaskolb commented 2 months ago

Just had an idea about this, as the issue came up in a project I'm working on. We have xcodegen run with cache on post-rewrite, post-merge and post-checkout. Your above issue can happen, but there's another one. If someone:

So the idea is to add a new command to xcodegen that tells it to just update it's cache without generating. This can then run in a post-commit command, and would fix both issues.

Then the only way I can foresee the cache getting out of date is if someone adds a file in xcode, and then discards the changes in git. XcodeGen wouldn't know about that (there's no discard changes git hook as far as I'm aware), and the project would be invalid. There's a rarer case though.