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.
The scenario
Section titled “The scenario”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.
Write the external part
Section titled “Write the external part”The template backend has the service api. It has no part that is external to the workspace.
defaults: ref: main
repos: backend: ~/code/backend
services: api: repo: backend port: 8080 command: npm run dev -- --port $PORTThe template frontend lists api in external. The service web reads it as it reads a service of the same template.
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:
- A workspace of the template
backendthat runsapion the branch of the frontend workspace.ref: samemeans this branch. - A workspace of the template
backendthat runsapionmain. - The staging deployment, if its
/healthpath 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.
Start the frontend
Section titled “Start the frontend”-
Make the three workspaces.
--matchselects the branchfeature-1234in each repository that has it.Terminal window lo new workspace-1 --template backendlo new workspace-2 --template backend --match feature-1234lo new workspace-3 --template frontend --match feature-1234lo newshows the source that the part resolves to. The backend does not run, so the part isstopped:workspace-3 frontend createdsource frontend feature-1234 match localservice web process stoppedexternal api workspace-2/api stopped -
Start the frontend.
Terminal window lo up workspace-3webreads the partapi. Solo upfirst startsapiinworkspace-2, and waits until it is ready. Then it startsweb. A service that lists the part independs_onhas the same effect.workspace-3 frontend upsource frontend feature-1234 match ~/.localspace/worktrees/workspace-3/frontendservice web process running 127.0.0.1:20000external api workspace-2/api running http://127.0.0.1:20001 -
Read the environment of
web.Terminal window lo env workspace-3 webAPI_URL=http://127.0.0.1:20001LOCALSPACE_SERVICE=webLOCALSPACE_WORKSPACE=workspace-3PORT=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).
Fall back to a different workspace
Section titled “Fall back to a different workspace”If workspace-2 is not there, the first source finds nothing. The second source is workspace-1.
lo up workspace-3workspace-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:20002If 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)Restart after a change
Section titled “Restart after a change”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:
lo new workspace-2 --template backend --match feature-1234lo up workspace-2lo up workspace-3workspace-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:20001web continues to use workspace-1. Restart it to use workspace-2:
lo restart workspace-3 --service weblo 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.
Make the missing workspace
Section titled “Make the missing workspace”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:
external: api: from: - workspace: { template: backend, ref: same } service: api create: trueWhen 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.
Pin a workspace
Section titled “Pin a workspace”The order of from is the default. Use lo set to select a source for one workspace.
lo set workspace-3 api=workspace:workspace-1workspace-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:
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.
Stop a workspace that others use
Section titled “Stop a workspace that others use”lo down and lo rm refuse to stop a workspace while a running service of a different workspace uses it:
lo down workspace-2lo: workspace-2 is in use: workspace-3/web reads api from api; stop those first, or use --forceStop the consumers first. Then stop the backend:
lo down workspace-3lo down workspace-2lo 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.
Use worktree mode
Section titled “Use worktree mode”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.
services: api: port: 8080 command: npm run dev -- --port $PORTexternal: 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>.
cd ~/code/shop-api-feature && lo upcd ~/code/shop-web-feature && lo upfeature-1234 shop-api up source shop-api feature-1234 explicit ~/code/shop-api-feature service api process running 127.0.0.1:20002shop-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:20002The 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.
Related
Section titled “Related”- Template reference gives the fields of
external. - References tells the values of a reference to a linked part.
- Use services that run elsewhere tells how remotes work.
- Select branches tells how
--matchand the pairs work.