Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(create-turbo): better error when GH is down #5900

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 64 additions & 6 deletions packages/create-turbo/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import path from "path";
import path from "node:path";
import childProcess from "node:child_process";
import chalk from "chalk";
import childProcess from "child_process";
import { setupTestFixtures, spyConsole } from "@turbo/test-utils";
import { create } from "../src/commands/create";
import type { CreateCommandArgument } from "../src/commands/create/types";
import { setupTestFixtures, spyConsole, spyExit } from "@turbo/test-utils";
import { logger } from "@turbo/utils";
import type { PackageManager } from "@turbo/workspaces";

// imports for mocks
import * as turboWorkspaces from "@turbo/workspaces";
import * as turboUtils from "@turbo/utils";
import type { CreateCommandArgument } from "../src/commands/create/types";
import { create } from "../src/commands/create";
import { getWorkspaceDetailsMockReturnValue } from "./test-utils";

jest.mock("@turbo/workspaces", () => ({
Expand All @@ -24,6 +23,7 @@ describe("create-turbo", () => {
});

const mockConsole = spyConsole();
const mockExit = spyExit();

test.each<{ packageManager: PackageManager }>([
{ packageManager: "yarn" },
Expand Down Expand Up @@ -97,4 +97,62 @@ describe("create-turbo", () => {
mockExecSync.mockRestore();
}
);

test.only("throws correct error message when a download error is encountered", async () => {
const { root } = useFixture({ fixture: `create-turbo` });
const packageManager = "pnpm";
const mockAvailablePackageManagers = jest
.spyOn(turboUtils, "getAvailablePackageManagers")
.mockResolvedValue({
npm: "8.19.2",
yarn: "1.22.10",
pnpm: "7.22.2",
});

const mockCreateProject = jest
.spyOn(turboUtils, "createProject")
.mockRejectedValue(new turboUtils.DownloadError("Could not connect"));

const mockGetWorkspaceDetails = jest
.spyOn(turboWorkspaces, "getWorkspaceDetails")
.mockResolvedValue(
getWorkspaceDetailsMockReturnValue({
root,
packageManager,
})
);

const mockExecSync = jest
.spyOn(childProcess, "execSync")
.mockImplementation(() => {
return "success";
});

await create(
root as CreateCommandArgument,
packageManager as CreateCommandArgument,
{
skipInstall: true,
example: "default",
}
);

expect(mockConsole.error).toHaveBeenCalledTimes(2);
expect(mockConsole.error).toHaveBeenNthCalledWith(
1,
logger.turboRed.bold(">>>"),
chalk.red("Unable to download template from Github")
);
expect(mockConsole.error).toHaveBeenNthCalledWith(
2,
logger.turboRed.bold(">>>"),
chalk.red("Could not connect")
);
expect(mockExit.exit).toHaveBeenCalledWith(1);

mockAvailablePackageManagers.mockRestore();
mockCreateProject.mockRestore();
mockGetWorkspaceDetails.mockRestore();
mockExecSync.mockRestore();
});
});
2 changes: 1 addition & 1 deletion packages/create-turbo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"jest": "^27.4.3",
"ts-jest": "^27.1.1",
"tsup": "^6.7.0",
"typescript": "^4.5.5"
"typescript": "^5.2.2"
},
"files": [
"dist"
Expand Down
32 changes: 24 additions & 8 deletions packages/create-turbo/src/commands/create/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
getAvailablePackageManagers,
createProject,
DownloadError,
logger,
} from "@turbo/utils";
import { tryGitCommit, tryGitInit } from "../../utils/git";
Expand All @@ -33,8 +34,15 @@ function handleErrors(err: unknown) {
} else if (err instanceof ConvertError && err.type !== "unknown") {
error(chalk.red(err.message));
process.exit(1);
// handle unknown errors (no special handling, just re-throw to catch at root)
} else {
// handle download errors from @turbo/utils
} else if (err instanceof DownloadError) {
error(chalk.red("Unable to download template from Github"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub

error(chalk.red(err.message));
process.exit(1);
}

// handle unknown errors (no special handling, just re-throw to catch at root)
else {
throw err;
}
}
Expand Down Expand Up @@ -85,12 +93,20 @@ export async function create(

const { example, examplePath } = opts;
const exampleName = example && example !== "default" ? example : "basic";
const { hasPackageJson, availableScripts, repoInfo } = await createProject({
appPath: root,
example: exampleName,
isDefaultExample: isDefaultExample(exampleName),
examplePath,
});

let projectData = {} as Awaited<ReturnType<typeof createProject>>;
try {
projectData = await createProject({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This eventually hits got with no timeout: https://github.com/vercel/turbo/blob/main/packages/turbo-utils/src/examples.ts#L48C34-L50

The failure mode for the last outage didn't return a 500 unicorn, it just hung indefinitely.

This is definitely an improvement, but it doesn't address all failure modes.

appPath: root,
example: exampleName,
isDefaultExample: isDefaultExample(exampleName),
examplePath,
});
} catch (err) {
handleErrors(err);
}

const { hasPackageJson, availableScripts, repoInfo } = projectData;

// create a new git repo after creating the project
tryGitInit(root, `feat(create-turbo): create ${exampleName}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export {
downloadAndExtractExample,
} from "./examples";
export { isWriteable } from "./isWriteable";
export { createProject } from "./createProject";
export { createProject, DownloadError } from "./createProject";
export { convertCase } from "./convertCase";

export * as logger from "./logger";
Expand Down
88 changes: 83 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.