Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/server/src/cli/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,21 @@ describe("t3 app", () => {
Effect.gen(function* () {
const baseDir = NodePath.join(root, "t3-home");
const explicitPath = NodePath.join(root, "project");
const workspaceFile = NodePath.join(root, "project.code-workspace");
const platform = yield* HostProcessPlatform;
const workingDirectory = yield* HostProcessWorkingDirectory;
const desktop = yield* fakeDesktop({ baseDir });

yield* runCli(["app"], { T3CODE_HOME: baseDir });
yield* runCli(["app", explicitPath, "--base-dir", baseDir]);
yield* runCli(["app", workspaceFile, "--base-dir", baseDir]);

expect(desktop.received.map((request) => request.workspaceRoot)).toEqual([
workingDirectory,
explicitPath,
root,
]);
expect(desktop.received[2]).toMatchObject({ workspaceFile });
expect(desktop.received.every((request) => request.platform === platform)).toBe(true);
}).pipe(Effect.scoped),
),
Expand Down
11 changes: 9 additions & 2 deletions apps/server/src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ const runAppCommand = Effect.fn("cli.app")(function* (flags: {
const allowDevFallback = Option.isNone(flags.baseDir) && !environment.t3Home?.trim();
const rawWorkspaceRoot =
Option.getOrUndefined(flags.workspaceRoot) ?? (yield* HostProcessWorkingDirectory);
const workspaceRoot = path.resolve(yield* expandHomePath(rawWorkspaceRoot));
const resolvedInput = path.resolve(yield* expandHomePath(rawWorkspaceRoot));
const workspaceFile = resolvedInput.toLowerCase().endsWith(".code-workspace")
? resolvedInput
: undefined;
const workspaceRoot = workspaceFile === undefined ? resolvedInput : path.dirname(workspaceFile);
const userId = yield* HostProcessUserId;
const resolveAddress = (stateSubdirectory: "userdata" | "dev") =>
resolveDesktopAppControlAddress({
Expand All @@ -217,6 +221,7 @@ const runAppCommand = Effect.fn("cli.app")(function* (flags: {
requestId: NodeCrypto.randomUUID(),
type: "open-workspace",
workspaceRoot,
...(workspaceFile === undefined ? {} : { workspaceFile }),
platform: hostPlatform,
};
const address = resolveAddress("userdata");
Expand Down Expand Up @@ -252,7 +257,9 @@ const runAppCommand = Effect.fn("cli.app")(function* (flags: {
export const appCommand = Command.make("app", {
baseDir: baseDirFlag,
workspaceRoot: Argument.string("path").pipe(
Argument.withDescription("Project directory. Default: current directory."),
Argument.withDescription(
"Project directory or .code-workspace file. Default: current directory.",
),
Argument.optional,
),
}).pipe(
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/components/CommandPalette.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import {
getBrowseDirectoryPath,
hasTrailingPathSeparator,
inferProjectTitleFromPath,
inferProjectTitleFromWorkspaceFile,
isExplicitRelativeProjectPath,
isUnsupportedWindowsProjectPath,
resolveProjectPathForDispatch,
Expand Down Expand Up @@ -2799,13 +2800,12 @@ function OpenCommandPaletteDialog(props: {
);
}

const fileName = workspaceFilePath.split(/[/\\]/).pop() ?? workspaceFilePath;
const title = fileName.replace(/\.code-workspace$/i, "").trim();
const title = inferProjectTitleFromWorkspaceFile(resolved.workspaceFilePath);

await handleAddProject(resolved.anchorDir, {
workspaceFile: resolved.workspaceFilePath,
repoRoots: resolved.repoRoots,
...(title.length > 0 ? { title } : {}),
...(title === null ? {} : { title }),
});
},
[browseEnvironmentId, handleAddProject, readWorkspaceFile],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import { useEffect, useEffectEvent, useRef } from "react";

import { handleDesktopAppActivationRequest } from "../../desktopAppActivation";
import { useNewThreadHandler } from "../../hooks/useHandleNewThread";
import { findProjectByPath, inferProjectTitleFromPath } from "../../lib/projectPaths";
import {
findProjectByPath,
inferProjectTitleFromPath,
inferProjectTitleFromWorkspaceFile,
} from "../../lib/projectPaths";
import { newProjectId } from "../../lib/utils";
import { readProjects, waitForProject } from "../../state/entities";
import { usePrimaryEnvironment } from "../../state/environments";
import { filesystemEnvironment } from "../../state/filesystem";
import { projectEnvironment } from "../../state/projects";
import { useEnvironmentQuery } from "../../state/query";
import { environmentShell } from "../../state/shell";
import { useAtomCommand } from "../../state/use-atom-command";
import { useAtomQueryRunner } from "../../state/use-atom-query-runner";

export function DesktopAppActivationCoordinator() {
const primaryEnvironment = usePrimaryEnvironment();
const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });
const readWorkspaceFile = useAtomQueryRunner(filesystemEnvironment.readWorkspaceFile, {
reportFailure: false,
});
const openThread = useNewThreadHandler();
const queueRef = useRef(Promise.resolve());
const activation = window.desktopBridge?.appActivation;
Expand Down Expand Up @@ -49,14 +58,36 @@ export function DesktopAppActivationCoordinator() {
readProjects().filter((project) => project.environmentId === environmentId),
workspaceRoot,
) ?? null,
createProject: async (environmentId, workspaceRoot) => {
createProject: async (environmentId, workspaceRoot, workspaceFile) => {
let workspace = {};
// The palette names a workspace project after its file, so the CLI has
// to as well; the anchor directory is often a generic parent.
let title = inferProjectTitleFromPath(workspaceRoot);
if (workspaceFile !== undefined) {
const read = await readWorkspaceFile({
environmentId,
input: { workspaceFilePath: workspaceFile },
});
if (read._tag === "Failure") {
const error = squashAtomCommandFailure(read);
throw error instanceof Error
? error
: new Error("T3 Code could not read the workspace file.");
}
workspace = {
workspaceFile: read.value.workspaceFilePath,
repoRoots: read.value.repoRoots,
};
title = inferProjectTitleFromWorkspaceFile(read.value.workspaceFilePath) ?? title;
}
const projectId = newProjectId();
const result = await createProject({
environmentId,
input: {
projectId,
title: inferProjectTitleFromPath(workspaceRoot),
title,
workspaceRoot,
...workspace,
createWorkspaceRootIfMissing: false,
defaultModelSelection: null,
},
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/desktopAppActivation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ describe("desktop app activation", () => {
expect(response).toMatchObject({ ok: true, projectId: createdProjectId });
});

it("passes a workspace file through to project creation", async () => {
const workspaceFile = "/workspace/project/project.code-workspace";
const deps = dependencies({ findProject: () => null });

const response = await handleDesktopAppActivationRequest({ ...request, workspaceFile }, deps);

expect(deps.createProject).toHaveBeenCalledWith(
environmentId,
request.workspaceRoot,
workspaceFile,
);
expect(response).toMatchObject({ ok: true, projectId: createdProjectId });
});

it("rejects a Windows path when the primary environment is WSL", async () => {
const response = await handleDesktopAppActivationRequest(
{ ...request, platform: "win32" },
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/desktopAppActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface DesktopAppActivationDependencies {
readonly createProject: (
environmentId: EnvironmentId,
workspaceRoot: string,
workspaceFile: string | undefined,
) => Promise<ProjectId>;
readonly waitForProject: (projectRef: ScopedProjectRef) => Promise<void>;
readonly openThread: (
Expand Down Expand Up @@ -79,7 +80,11 @@ export async function handleDesktopAppActivationRequest(
let projectId = dependencies.findProject(target.environmentId, request.workspaceRoot)?.id ?? null;
if (projectId === null) {
try {
projectId = await dependencies.createProject(target.environmentId, request.workspaceRoot);
projectId = await dependencies.createProject(
target.environmentId,
request.workspaceRoot,
request.workspaceFile,
);
await dependencies.waitForProject({ environmentId: target.environmentId, projectId });
} catch (error) {
return failure(
Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/lib/projectPaths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getBrowseParentPath,
hasTrailingPathSeparator,
inferProjectTitleFromPath,
inferProjectTitleFromWorkspaceFile,
isExplicitRelativeProjectPath,
isFilesystemBrowseQuery,
normalizeProjectPathForComparison,
Expand Down Expand Up @@ -46,6 +47,16 @@ describe("projectPaths", () => {
expect(inferProjectTitleFromPath("/home/user\\project/")).toBe("user\\project");
});

it("titles workspace-file projects after the file, not its directory", () => {
expect(inferProjectTitleFromWorkspaceFile("/home/user/code/platform.code-workspace")).toBe(
"platform",
);
expect(inferProjectTitleFromWorkspaceFile("C:\\Work\\code\\Platform.CODE-WORKSPACE")).toBe(
"Platform",
);
expect(inferProjectTitleFromWorkspaceFile("/home/user/code/.code-workspace")).toBe(null);
});

it("detects browse queries across supported path styles", () => {
expect(isFilesystemBrowseQuery(".")).toBe(false);
expect(isFilesystemBrowseQuery("..")).toBe(false);
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/lib/projectPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
getBrowseParentPath,
hasTrailingPathSeparator,
inferProjectTitleFromPath,
inferProjectTitleFromWorkspaceFile,
isExplicitRelativeProjectPath,
isFilesystemBrowseQuery,
isUnsupportedWindowsProjectPath,
Expand Down
13 changes: 13 additions & 0 deletions packages/client-runtime/src/state/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ export function inferProjectTitleFromPath(value: string): string {
return segments.findLast(Boolean) ?? normalized;
}

/**
* Titles a `.code-workspace`-backed project after the file rather than the
* directory holding it, which is often a generic parent such as `~/code`.
* Returns null when the file name carries no title, leaving the caller to fall
* back to {@link inferProjectTitleFromPath} on the anchor directory.
*/
export function inferProjectTitleFromWorkspaceFile(workspaceFilePath: string): string | null {
const title = inferProjectTitleFromPath(workspaceFilePath)
.replace(/\.code-workspace$/i, "")
.trim();
return title.length > 0 ? title : null;
}

export function appendBrowsePathSegment(currentPath: string, segment: string): string {
const separator = preferredPathSeparator(currentPath);
return `${getBrowseDirectoryPath(currentPath)}${segment}${separator}`;
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/desktopAppActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const DesktopAppActivationRequest = Schema.Struct({
requestId: TrimmedNonEmptyString,
type: Schema.Literal("open-workspace"),
workspaceRoot: TrimmedNonEmptyString,
workspaceFile: Schema.optional(TrimmedNonEmptyString),
platform: DesktopAppActivationPlatform,
});
export type DesktopAppActivationRequest = typeof DesktopAppActivationRequest.Type;
Expand Down
Loading