Skip to content
Open
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
30 changes: 26 additions & 4 deletions apps/desktop/src/lib/trpc/routers/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { eq } from "drizzle-orm";
import { MEMORY_SCAFFOLD_ENABLED } from "main/lib/feature-flags";
import { findRealBinary } from "main/lib/agent-setup/utils";
import { localDb } from "main/lib/local-db";
import {
CONFIG_FILE_NAME,
PROJECT_SUPERSET_DIR_NAME,
} from "shared/constants";
import type { SetupAction, SetupDetectionResult } from "shared/types/config";
import { z } from "zod";
import { publicProcedure, router } from "../..";
Expand Down Expand Up @@ -44,6 +48,10 @@ function getRuntimeAvailability(force: boolean): RuntimeAvailability {
function hasConfiguredScripts(
project: Pick<SelectProject, "id" | "mainRepoPath">,
) {
if (!project.mainRepoPath) {
return false;
}

const config = loadSetupConfig({
mainRepoPath: project.mainRepoPath,
projectId: project.id,
Expand Down Expand Up @@ -365,12 +373,18 @@ async function detectSetupDefaults(
}

function getConfigPath(mainRepoPath: string): string {
return join(mainRepoPath, ".superset", "config.json");
return join(mainRepoPath, PROJECT_SUPERSET_DIR_NAME, CONFIG_FILE_NAME);
}

function ensureConfigExists(mainRepoPath: string): string {
if (!mainRepoPath) {
throw new Error(
"This project has no repo path, so it has no setup/teardown scripts.",
);
}

const configPath = getConfigPath(mainRepoPath);
const supersetDir = join(mainRepoPath, ".superset");
const supersetDir = join(mainRepoPath, PROJECT_SUPERSET_DIR_NAME);

if (!existsSync(configPath)) {
// Create .superset directory if it doesn't exist
Expand Down Expand Up @@ -441,7 +455,7 @@ export const createConfigRouter = () => {
.from(projects)
.where(eq(projects.id, input.projectId))
.get();
if (!project) {
if (!project || !project.mainRepoPath) {
return null;
}
return ensureConfigExists(project.mainRepoPath);
Expand All @@ -456,7 +470,7 @@ export const createConfigRouter = () => {
.from(projects)
.where(eq(projects.id, input.projectId))
.get();
if (!project) {
if (!project || !project.mainRepoPath) {
return { content: null, exists: false };
}

Expand Down Expand Up @@ -484,6 +498,14 @@ export const createConfigRouter = () => {
if (!project) {
throw new Error("Project not found");
}
if (!project.mainRepoPath) {
return {
projectSummary: "",
actions: [],
setupTemplate: [],
signals: {},
};
}

return await detectSetupDefaults(project.mainRepoPath);
}),
Expand Down
36 changes: 20 additions & 16 deletions apps/desktop/src/lib/trpc/routers/workspaces/utils/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { PROJECTS_DIR_NAME, SUPERSET_DIR_NAME } from "shared/constants";
import {
PROJECT_SUPERSET_DIR_NAME,
PROJECTS_DIR_NAME,
SUPERSET_DIR_NAME,
} from "shared/constants";
import { loadSetupConfig } from "./setup";

const TEST_DIR = join(tmpdir(), `superset-test-setup-${process.pid}`);
Expand All @@ -18,7 +22,7 @@ const USER_CONFIG_DIR = join(

describe("loadSetupConfig", () => {
beforeEach(() => {
mkdirSync(join(MAIN_REPO, ".superset"), { recursive: true });
mkdirSync(join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
});

afterEach(() => {
Expand All @@ -43,7 +47,7 @@ describe("loadSetupConfig", () => {
};

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(setupConfig),
);

Expand All @@ -53,7 +57,7 @@ describe("loadSetupConfig", () => {

test("returns null for invalid JSON", () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
"{ invalid json",
);

Expand All @@ -63,7 +67,7 @@ describe("loadSetupConfig", () => {

test("validates setup field must be an array", () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ setup: "not-an-array" }),
);

Expand All @@ -76,13 +80,13 @@ describe("loadSetupConfig", () => {
const worktreeConfig = { setup: ["scripts/setup-worktree.sh"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

mkdirSync(join(WORKTREE, ".superset"), { recursive: true });
mkdirSync(join(WORKTREE, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
writeFileSync(
join(WORKTREE, ".superset", "config.json"),
join(WORKTREE, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(worktreeConfig),
);

Expand All @@ -97,7 +101,7 @@ describe("loadSetupConfig", () => {
const mainConfig = { setup: ["npm install"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand All @@ -115,7 +119,7 @@ describe("loadSetupConfig", () => {
const userConfig = { setup: ["custom-setup.sh"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand All @@ -136,9 +140,9 @@ describe("loadSetupConfig", () => {
const worktreeConfig = { setup: ["worktree-setup.sh"] };
const userConfig = { setup: ["user-override-setup.sh"] };

mkdirSync(join(WORKTREE, ".superset"), { recursive: true });
mkdirSync(join(WORKTREE, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
writeFileSync(
join(WORKTREE, ".superset", "config.json"),
join(WORKTREE, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(worktreeConfig),
);

Expand All @@ -160,7 +164,7 @@ describe("loadSetupConfig", () => {
const mainConfig = { setup: ["npm install"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand All @@ -175,7 +179,7 @@ describe("loadSetupConfig", () => {
const mainConfig = { setup: ["npm install"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand All @@ -190,7 +194,7 @@ describe("loadSetupConfig", () => {
const userConfig = { setup: [], teardown: [] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand All @@ -212,7 +216,7 @@ describe("loadSetupConfig", () => {
const mainConfig = { setup: ["npm install"] };

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify(mainConfig),
);

Expand Down
34 changes: 19 additions & 15 deletions apps/desktop/src/lib/trpc/routers/workspaces/utils/teardown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { PROJECTS_DIR_NAME, SUPERSET_DIR_NAME } from "shared/constants";
import {
PROJECT_SUPERSET_DIR_NAME,
PROJECTS_DIR_NAME,
SUPERSET_DIR_NAME,
} from "shared/constants";
import { runTeardown } from "./teardown";

const TEST_DIR = join(tmpdir(), `superset-test-teardown-${process.pid}`);
Expand All @@ -25,7 +29,7 @@ const USER_CONFIG_DIR = join(
describe("runTeardown", () => {
beforeEach(() => {
// Create test directories
mkdirSync(join(MAIN_REPO, ".superset"), { recursive: true });
mkdirSync(join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
mkdirSync(WORKTREE, { recursive: true });
});

Expand All @@ -52,7 +56,7 @@ describe("runTeardown", () => {

test("returns success when config has no teardown commands", async () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ setup: ["echo setup"] }),
);

Expand All @@ -66,7 +70,7 @@ describe("runTeardown", () => {

test("returns success when teardown array is empty", async () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [] }),
);

Expand All @@ -84,7 +88,7 @@ describe("runTeardown", () => {
const markerFile = join(WORKTREE, "main-repo-config-executed.txt");

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "executed" > "${markerFile}"`] }),
);

Expand All @@ -101,9 +105,9 @@ describe("runTeardown", () => {

test("uses worktreePath config when present", async () => {
const worktreeMarker = join(WORKTREE, "worktree-config-executed.txt");
mkdirSync(join(WORKTREE, ".superset"), { recursive: true });
mkdirSync(join(WORKTREE, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
writeFileSync(
join(WORKTREE, ".superset", "config.json"),
join(WORKTREE, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "executed" > "${worktreeMarker}"`] }),
);

Expand All @@ -123,13 +127,13 @@ describe("runTeardown", () => {
const worktreeMarker = join(WORKTREE, "from-worktree.txt");

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "main" > "${mainMarker}"`] }),
);

mkdirSync(join(WORKTREE, ".superset"), { recursive: true });
mkdirSync(join(WORKTREE, PROJECT_SUPERSET_DIR_NAME), { recursive: true });
writeFileSync(
join(WORKTREE, ".superset", "config.json"),
join(WORKTREE, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "worktree" > "${worktreeMarker}"`] }),
);

Expand All @@ -146,7 +150,7 @@ describe("runTeardown", () => {

test("returns error when teardown command fails", async () => {
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: ["exit 1"] }),
);

Expand All @@ -162,7 +166,7 @@ describe("runTeardown", () => {
test("chains multiple teardown commands with &&", async () => {
const testFile = join(WORKTREE, "teardown-test.txt");
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({
teardown: [`echo "created" > "${testFile}"`, `test -f "${testFile}"`],
}),
Expand All @@ -180,7 +184,7 @@ describe("runTeardown", () => {
test("sets environment variables for teardown scripts", async () => {
const envFile = join(WORKTREE, "env-test.txt");
writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({
teardown: [
`echo "$SUPERSET_WORKSPACE_NAME|$SUPERSET_ROOT_PATH" > "${envFile}"`,
Expand All @@ -204,7 +208,7 @@ describe("runTeardown", () => {
const userMarker = join(WORKTREE, "from-user.txt");

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "main" > "${mainMarker}"`] }),
);

Expand All @@ -231,7 +235,7 @@ describe("runTeardown", () => {
const mainMarker = join(WORKTREE, "from-main.txt");

writeFileSync(
join(MAIN_REPO, ".superset", "config.json"),
join(MAIN_REPO, PROJECT_SUPERSET_DIR_NAME, "config.json"),
JSON.stringify({ teardown: [`echo "main" > "${mainMarker}"`] }),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Button } from "@superset/ui/button";
import { toast } from "@superset/ui/sonner";
import { cn } from "@superset/ui/utils";
import { useCallback, useEffect, useRef, useState } from "react";
import { HiArrowTopRightOnSquare, HiDocumentArrowUp } from "react-icons/hi2";
Expand Down Expand Up @@ -185,6 +186,9 @@ export function ScriptsEditor({ projectId, className }: ScriptsEditorProps) {
utils.config.getConfigContent.invalidate({ projectId });
utils.config.shouldShowSetupCard.invalidate({ projectId });
},
onError: (error) => {
toast.error(error.message || "Could not save scripts");
},
});

const handleSetupChange = useCallback((value: string) => {
Expand Down