Search code examples
buck

How do cells work in Buck?


I understand that there is an undocumented feature in called cells. There are some tests that demonstrate how they work, but is there any more written information about them?

How do cells work in Buck?


Solution

  • Have a look at this example repo.


    What Problem Does Cells Solve?

    Cells are a great fit for this situtation:

    • You have a library in a repo that builds with Buck
    • You want to use that library in your project
    • You copy the library into your project but the library's BUCK files don't work, because the root (//) has changed to the root of your project
    • You don't want to rewrite your library's BUCK files!

    Cells allow us to create isolated sub-projects that have their own .buckconfig and their own Buck target heirarchy. Cells can use targets from other cells when those targets are exported.

    How Does it Work?

    The meat of the example is in .buckconfig:

    [repositories]
      neither = vendor/neither
    

    The repositories tag tells Buck where each cell lives in your project's folder structure. Each cell has an alias. Here we have neither = vendor/neither, which means the subfolder vendor/neither is a cell with the name neither.

    We can refer to targets inside of a cell using this syntax: cell-name//:target-in-cell.

    So to make our example target depend on LoopPerfect/neither:

    deps = [
      'neither//:neither',
    ],
    

    This is a really powerful feature, because it makes a library's BUCK files portable. All you need to do to expose a target inside of a cell is to make it public:

    visibility = [
      'PUBLIC',
    ]