How can I share code between all my projects I have locally?

I want to be able to share my own source code between my projects I work on locally. When I change that shared source code, I want all projects that use it locally, to pick up the changes, that is, when I run a project.

Yes, I can create a local library or repository or something like this, but will this give me a flexibity I want?
Namely, when I change it, will the changes be automatically picked up by my projects? Or will I have to delete the mix.lock file and do other stuff?
I can publish it on my github also, however, this will require me to re-publish it each time I change it locally and also increment its version – too much effort.

Also, when I deploy a project, that shared local library should embed the current shapshot of itself into a project as it is currently. Or at least its latest build.


The bottom line is that I want to be able to share code locally effortlessly.

How can I do this?

You can use :path-dependencies, those are updated on every run of mix deps.update $name. But you really shouldn’t use those. Those projects can’t (easily) worked on on different workstations.

Also, picking up changes automatically isn’t what you want. I’ve been there, done that… Believe me, it makes development easier in the first instance, at least it seems it were. But after you had a running A, then changed the shared codebase S to make something work in B, and you are happy, continue to work at C and D, and don’t touch S at all. Then you need to get back to A, and without having changed anything in it, nothing works, because some old changes in S.

In a properly versioned system you can simply stick to the known to work version of S until you fixed it and A will not break.

2 Likes

You should be able to use the path option on declaring deps, such as:

defp deps do
  [
    # other deps
    {:foobar, path: "path/to/foobar"}
  ]
end

As per the docs:

Path and in umbrella dependencies are automatically recompiled by the parent project whenever they change.

which seems to be what you want.

This should also work out of the box via path as long as you build releases (in contrast to, say, pushing to heroku), but I’ve never tried it.

Is there a middle ground where you use a git dependency with a file:// URL to a local repo?

Since those URLs aren’t (easily) transferable between workstations I try to avoid them at all cost.

then what do you propose if not “:path”?

Umbrella if applicable or a (private) git-repository you push to.

2 Likes

Git submodules might be worth looking into

how? path is local, whereas when deploying an application, for example, phoenix one, it’s compiled on a server where my library doesn’t exist.

I think, that @bobbypriambodo means, that a built release of an application will embed your shared code. A library of course can’t, except you link the shared files in terms of a filesystems soft- or hardlink directly into the not-shared file tree. Of course, this is the last thing I’d evver suggest and the first I’d advise against!