Skip to content

Connect workspaces of different templates

Large systems keep their parts in separate templates. One template can be for the backend and one for the frontend. A frontend workspace must then use the backend workspace that runs the same branch. If there is no such workspace, it must use a default one.

An external part solves this. The template of the frontend lists where api can come from, in order. localspace starts the first service that is available, and gives its address to the frontend. For the fields, see the template reference.

Three workspaces run on one machine:

Workspace Template Branch
workspace-1 backend main
workspace-2 backend feature-1234
workspace-3 frontend feature-1234

The service web of workspace-3 must reach the service api of workspace-2. If there is no backend on feature-1234, it must reach workspace-1. If there is no backend on main, it must reach staging.

The template backend has the service api. It has no part that is external to the workspace.

~/.localspace/templates/backend/localspace.yaml
defaults:
ref: main
repos:
backend: ~/code/backend
services:
api:
repo: backend
port: 8080
command: npm run dev -- --port $PORT

The template frontend lists api in external. The service web reads it as it reads a service of the same template.

~/.localspace/templates/frontend/localspace.yaml
defaults:
ref: main
repos:
frontend: ~/code/frontend
external:
api:
from:
- workspace: { template: backend, ref: same }
- workspace: { template: backend, ref: main }
- { name: staging, url: "https://api.staging.acme.com", ready: /health }
service: api
start: true
services:
web:
repo: frontend
port: 3000
command: npm run dev -- --port $PORT
env:
API_URL: ${api.internal_url}

localspace tries the sources of from in order:

  1. A workspace of the template backend that runs api on the branch of the frontend workspace. ref: same means this branch.
  2. A workspace of the template backend that runs api on main.
  3. The staging deployment, if its /health path answers.

start: true lets lo up start a workspace that is stopped. Without it, a workspace source is available only when its service runs.

A workspace source finds a workspace by its template and its ref. It does not use the name of the workspace. The names workspace-1 and workspace-2 are not in the template.

  1. Make the three workspaces. --match selects the branch feature-1234 in each repository that has it.

    Terminal window
    lo new workspace-1 --template backend
    lo new workspace-2 --template backend --match feature-1234
    lo new workspace-3 --template frontend --match feature-1234

    lo new shows the source that the part resolves to. The backend does not run, so the part is stopped:

    workspace-3 frontend created
    source frontend feature-1234 match local
    service web process stopped
    external api workspace-2/api stopped
  2. Start the frontend.

    Terminal window
    lo up workspace-3

    web reads the part api. So lo up first starts api in workspace-2, and waits until it is ready. Then it starts web. A service that lists the part in depends_on has the same effect.

    workspace-3 frontend up
    source frontend feature-1234 match ~/.localspace/worktrees/workspace-3/frontend
    service web process running 127.0.0.1:20000
    external api workspace-2/api running http://127.0.0.1:20001
  3. Read the environment of web.

    Terminal window
    lo env workspace-3 web
    API_URL=http://127.0.0.1:20001
    LOCALSPACE_SERVICE=web
    LOCALSPACE_WORKSPACE=workspace-3
    PORT=20000

A service of a workspace reaches a linked service through the host: 127.0.0.1 and the host port. A service in a container gets the host alias of the engine in place of 127.0.0.1. The reference is the same for the two. See linked parts.

The external row of the output shows the workspace and the service that the part resolves to, the status of the service and its address. A pin adds (set to ...). lo graph draws the link with the name of the workspace: api workspace-2/api http://127.0.0.1:20001 (running).

If workspace-2 is not there, the first source finds nothing. The second source is workspace-1.

Terminal window
lo up workspace-3
workspace-3 frontend up
source frontend feature-1234 match ~/.localspace/worktrees/workspace-3/frontend
service web process running 127.0.0.1:20000
external api workspace-1/api running http://127.0.0.1:20002

If no source is available, the part is unresolved and the services that read it do not start. The message tells the reason for each source:

external api - unresolved (no source of api is available: no workspace of backend on feature-1234; no workspace of backend on main; remote:staging does not answer)

A service reads its address when it starts. localspace records the source that each part resolved to at that time. When a part now resolves to a different source, the service shows (restart for api).

For example, make workspace-2 and start it while web runs with workspace-1:

Terminal window
lo new workspace-2 --template backend --match feature-1234
lo up workspace-2
lo up workspace-3
workspace-3 frontend up
source frontend feature-1234 match ~/.localspace/worktrees/workspace-3/frontend
service web process running 127.0.0.1:20000 (restart for api)
external api workspace-2/api running http://127.0.0.1:20001

web continues to use workspace-1. Restart it to use workspace-2:

Terminal window
lo restart workspace-3 --service web

lo ls --json has the same information. The service has restartFor, a list of the parts that resolve to a different source now. The linked service has users, a list of the services of other workspaces that reach it.

A part can make the workspace that it does not find. Add create: true and keep only the source that must have the same branch:

~/.localspace/templates/frontend/localspace.yaml
external:
api:
from:
- workspace: { template: backend, ref: same }
service: api
create: true

When there is no backend workspace on the branch of the frontend, lo up makes it from the template backend, starts it, and then starts web. The new workspace has the name of the branch, or <template>--<branch> when a workspace has that name.

external api - unresolved (no workspace of backend runs api on feature-1234 yet; lo up makes one)

This message is the state before lo up. After lo up, the row shows the new workspace: external api feature-1234/api running http://127.0.0.1:20003.

With create: true, the first workspace source that finds no workspace makes one. localspace does not try the sources after it. Put the source that must make the workspace first, or use create with one workspace source.

A workspace has worktrees and data, so create is off by default.

The order of from is the default. Use lo set to select a source for one workspace.

Terminal window
lo set workspace-3 api=workspace:workspace-1
workspace-3 frontend up
source frontend feature-1234 match ~/.localspace/worktrees/workspace-3/frontend
service web process running 127.0.0.1:20000
external api workspace-1/api running http://127.0.0.1:20002 (set to workspace:workspace-1)

These choices work for a part:

Choice Meaning
api=workspace:workspace-1 The api of that workspace. This choice is a pin.
api=remote:staging The remote source with the name staging.
api=https://api.qa.acme.com A URL that you enter. The template does not list it.
api=auto The order of from again. This choice removes the pin.

The same choices work in lo new, in the MCP tools and in worktree mode.

lo set restarts the services that read the part. To give an attribute to a URL, add --attr:

Terminal window
lo set workspace-3 api=https://api.qa.acme.com --attr api.api_key=qa-key

--attr refuses the names that localspace gives, such as url, host and port. Set api=<url> in their place.

lo down and lo rm refuse to stop a workspace while a running service of a different workspace uses it:

Terminal window
lo down workspace-2
lo: workspace-2 is in use: workspace-3/web reads api from api; stop those first, or use --force

Stop the consumers first. Then stop the backend:

Terminal window
lo down workspace-3
lo down workspace-2

lo down --force stops the backend while a consumer runs. The consumer then loses its backend. lo rm refuses in the same way. Its --force also deletes work that you did not save, so stop the consumers first.

In worktree mode, each repository has a .localspace.yaml file. The name of the template is the name of the directory of the repository. In this example, the repositories are shop-api and shop-web.

shop-api/.localspace.yaml
services:
api:
port: 8080
command: npm run dev -- --port $PORT
shop-web/.localspace.yaml
external:
api:
from:
- workspace: { template: shop-api, ref: same }
- workspace: { template: shop-api, ref: main }
service: api
start: true
services:
web:
port: 3000
command: npm run dev -- --port $PORT
env:
API_URL: ${api.internal_url}

In worktree mode, ref: same is the branch of the worktree. Do not use repo.

Run lo up in a worktree of each repository, on the same branch. A workspace has the name of its branch. The second repository on the same branch has the name <repo>--<branch>.

Terminal window
cd ~/code/shop-api-feature && lo up
cd ~/code/shop-web-feature && lo up
feature-1234 shop-api up
source shop-api feature-1234 explicit ~/code/shop-api-feature
service api process running 127.0.0.1:20002
shop-web--feature-1234 shop-web up
source shop-web feature-1234 explicit ~/code/shop-web-feature
service web process running 127.0.0.1:20003
external api feature-1234/api running http://127.0.0.1:20002

The match uses the template and the ref, and not the names. The workspace feature-1234 runs api of shop-api on the branch feature-1234, so the part resolves to it.