Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does Stack rebuild unrelated source files? What are the recommended project structures? #6382

Closed
SmartHypercube opened this issue Dec 16, 2023 · 2 comments

Comments

@SmartHypercube
Copy link

Example repo: https://github.com/SmartHypercube/aoc2023-haskell

When I add another executable (by adding an executable in package.yaml and adding a file in src directory), and run stack run <new executable name>, Stack rebuilds all other executables. I would like to know why does this happen. I guess it is because I used a bad project structure, but I have tried some other ways to organized the files and directories, and each structure has its own problems.

I wish Stack could provide some tips about how should people organize project files for different types of projects (e.g. 1 lib + 1 exe, 1 lib + many exe, 0 lib + many exe, etc.). A good project structure should mean that adding / removing / updating source files should always result in the minimum set of files recompiled.

@mpilgrem
Copy link
Member

mpilgrem commented Dec 16, 2023

Purportedly due to a bug in some versions of Cabal (the library), the first time Stack builds any component of a local package, it builds all the executable components. So, for an example package foo with executables foo1 and foo2:

❯ stack ide targets
foo:lib
foo:exe:foo1-exe
foo:exe:foo2-exe
foo:test:foo-test

❯ stack build foo:lib # Just build the library components
Building all executables for foo once. After a successful build of all of them, only specified
executables will be rebuilt.
foo> configure (lib + exe)
...
Installing executable foo1-exe in D:\Users\mike\Code\Haskell\foo\.stack-work\install\3f52e67e\bin
Installing executable foo2-exe in D:\Users\mike\Code\Haskell\foo\.stack-work\install\3f52e67e\bin
Registering library for foo-0.1.0.0..

❯ stack build foo:lib # Second time around (nothing will build; already all built)

When you add a new executable component to an existing local package description, Stack knows it has to unregister the previous version of the package and re-build the 'new' package - and the above applies to the building of the 'new' package.

@mpilgrem
Copy link
Member

See #3486 for background to the bug in some versions of Cabal (the library). Stack may be able to move forward with this problem after Stack 2.15.1, because I am proposing that Stack cease supporting Cabal < 2.2. See #6377.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants