fix(cli): show the workspace commands actually target in workspace list (#12464)

* fix(cli): show the git-branch-resolved workspace in workspace list/whoami/switch

When the current git branch matches a wmill.yaml workspace's gitBranch,
commands target that workspace regardless of the active profile, but
`workspace list`, `whoami` and `switch` only printed the stored profile.
Print a note naming the workspace commands actually target, as is already
done for fork branches. Skip it when --workspace is passed, since that wins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): cover unmatched-branch and fork-base cases in the workspace target note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): read wmill.yaml diagnostics-safe in the workspace target note

Use a peek that never exits, throws or chdirs, flag a target with no local
profile, and drop the multi-workspace claim that the active profile is skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): tolerate empty legacy entries and name the effective id in the single-workspace note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-20 16:56:01 +01:00
committed by GitHub
co-authored by Claude Opus 5.5
parent d538f2adfa
commit 5819ced95b
2 changed files with 100 additions and 21 deletions
+84 -19
View File
@@ -2,6 +2,7 @@ import { writeFile, open as fsOpen } from "node:fs/promises";
import { readTextFile } from "../../utils/utils.ts";
import process from "node:process";
import { GlobalOptions } from "../../types.ts";
import type { WorkspaceEntryConfig } from "../../core/conf.ts";
import {
getActiveWorkspaceConfigFilePath,
getWorkspaceConfigFilePath,
@@ -87,27 +88,87 @@ export async function getWorkspaceByName(
return undefined;
}
// When the current git branch is a fork branch (wm-fork/<base>/<id>), commands
// resolve the fork workspace from the branch name and ignore the active
// profile — surface that wherever we display the active workspace, so users
// don't act on the wrong "Active:" line.
async function forkBranchAutoTargetNote(): Promise<string | undefined> {
// Without --workspace, commands only fall back to the active profile when
// wmill.yaml defines no workspaces (resolveWorkspace in core/context.ts) —
// surface what they target instead wherever we display the active workspace,
// so users don't act on the wrong "Active:" line. Keep in step with that order.
async function branchAutoTargetNote(
opts: GlobalOptions
): Promise<string | undefined> {
if (opts.workspace) {
return undefined;
}
const {
getCurrentGitBranch,
getOriginalBranchForWorkspaceForks,
getWorkspaceIdForWorkspaceForkFromBranchName,
isGitRepository,
} = await import("../../utils/git.ts");
const branch = getCurrentGitBranch();
if (!branch || !getOriginalBranchForWorkspaceForks(branch)) {
const {
peekWorkspaces,
findWorkspaceByGitBranch,
getEffectiveWorkspaceId,
getWorkspaceNames,
} = await import("../../core/conf.ts");
// Not readConfigFile: a note must never exit, throw or chdir the command.
const workspacesConf = await peekWorkspaces();
const branch = isGitRepository() ? getCurrentGitBranch() : null;
const originalBranch = branch ? getOriginalBranchForWorkspaceForks(branch) : null;
const match = branch
? findWorkspaceByGitBranch(workspacesConf, originalBranch ?? branch)
: undefined;
// Without a profile for the target, commands offer to create one and,
// when they cannot, may end up on the active profile after all.
const profiles = await allWorkspaces(opts.configDir);
const missingProfile = (wsName: string, entry: WorkspaceEntryConfig) => {
const workspaceId = getEffectiveWorkspaceId(wsName, entry);
let remote: string;
try {
remote = new URL(entry.baseUrl!).toString();
} catch {
return "";
}
return profiles.some((p) => p.remote === remote && p.workspaceId === workspaceId)
? ""
: ` No local profile targets it yet: run \`wmill workspace add <profile> ${workspaceId} ${remote}\`, otherwise commands may fall back to the active profile.`;
};
if (match?.[1].baseUrl) {
const [wsName, entry] = match;
if (originalBranch) {
const forkWorkspaceId = getWorkspaceIdForWorkspaceForkFromBranchName(branch!);
return (
`Note: you are on fork branch \`${branch}\` — wmill commands automatically target the fork workspace \`${forkWorkspaceId}\` on ${entry.baseUrl} here, regardless of the active profile. Use --workspace to override.`
);
}
return (
`Note: git branch \`${branch}\` matches workspace \`${wsName}\` in wmill.yaml — wmill commands here target \`${getEffectiveWorkspaceId(wsName, entry)}\` on ${entry.baseUrl}, regardless of the active profile. Use --workspace to override.` +
missingProfile(wsName, entry)
);
}
if (originalBranch) {
// resolveWorkspace exits with its own error here.
return undefined;
}
const forkWorkspaceId = getWorkspaceIdForWorkspaceForkFromBranchName(branch);
if (!forkWorkspaceId) {
return undefined;
const wsNames = getWorkspaceNames(workspacesConf);
if (wsNames.length === 1) {
const entry = (workspacesConf as any)[wsNames[0]] as WorkspaceEntryConfig;
if (!entry?.baseUrl) {
return undefined;
}
return (
`Note: wmill.yaml defines a single workspace, \`${wsNames[0]}\` — wmill commands here target \`${getEffectiveWorkspaceId(wsNames[0], entry)}\` on ${entry.baseUrl}, regardless of the active profile. Use --workspace to override.` +
missingProfile(wsNames[0], entry)
);
}
return (
`Note: you are on fork branch \`${branch}\` — wmill commands automatically target the fork workspace \`${forkWorkspaceId}\` here, regardless of the active profile. Use --workspace to override.`
);
if (wsNames.length > 1) {
return (
`Note: wmill.yaml defines workspaces ${wsNames.map((n) => `\`${n}\``).join(", ")} and the current git branch selects none of them — wmill commands here ask which one to use, or exit when not run in a terminal. Use --workspace to pick one.`
);
}
return undefined;
}
export async function list(opts: GlobalOptions) {
@@ -132,9 +193,9 @@ export async function list(opts: GlobalOptions) {
log.info("Active: " + colors.green.bold(activeName || "none"));
const forkNote = await forkBranchAutoTargetNote();
if (forkNote) {
log.info(colors.yellow(forkNote));
const branchNote = await branchAutoTargetNote(opts);
if (branchNote) {
log.info(colors.yellow(branchNote));
}
}
@@ -169,9 +230,9 @@ async function switchC(opts: GlobalOptions, workspaceName: string) {
`Switched to workspace ${workspaceName} (${workspace?.workspaceId} on ${workspace?.remote})`
)
);
const forkNote = await forkBranchAutoTargetNote();
if (forkNote) {
log.info(colors.yellow(forkNote));
const branchNote = await branchAutoTargetNote(opts);
if (branchNote) {
log.info(colors.yellow(branchNote));
}
return;
}
@@ -507,6 +568,10 @@ async function whoami(_opts: GlobalOptions) {
}
} else {
log.info("Active: " + colors.green.bold(activeName || "none"));
const branchNote = await branchAutoTargetNote(_opts);
if (branchNote) {
log.info(colors.yellow(branchNote));
}
}
}
+16 -2
View File
@@ -225,6 +225,12 @@ export async function peekWorkspaceEntry(
if (RESERVED_WORKSPACE_KEYS.has(workspaceName)) {
return undefined;
}
const entry = (await peekWorkspaces())?.[workspaceName];
return typeof entry === "object" && entry !== null ? entry : undefined;
}
/** The whole `workspaces` block, under the same diagnostics-only contract. */
export async function peekWorkspaces(): Promise<WorkspacesConfig | undefined> {
const wmillYamlPath = locateWmillYaml();
if (!wmillYamlPath) {
return undefined;
@@ -236,8 +242,16 @@ export async function peekWorkspaceEntry(
conf?.gitBranches ??
conf?.environments ??
conf?.git_branches;
const entry = (workspaces as any)?.[workspaceName];
return typeof entry === "object" && entry !== null ? entry : undefined;
if (typeof workspaces !== "object" || workspaces === null) {
return undefined;
}
// A bare `main:` key parses as null; readConfigFile reads it as {}.
return Object.fromEntries(
Object.entries(workspaces).map(([k, v]) => [
k,
typeof v === "object" && v !== null ? v : {},
])
) as WorkspacesConfig;
} catch (e) {
log.debug(`Failed to parse ${wmillYamlPath} for workspace lookup: ${e}`);
return undefined;