-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: skip mkdir when C3 parent dir already exists #15288
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
Open
AiAlchemist0
wants to merge
5
commits into
cloudflare:main
Choose a base branch
from
AiAlchemist0:fix/c3-mkdir-drive-root
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−20
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bae8067
fix: skip mkdir when C3 parent dir already exists
AiAlchemist0 a2686e6
test: cover C3 mkdir skip when parent exists
AiAlchemist0 c459ce3
docs: add JSDoc on setupProjectDirectory
AiAlchemist0 4d8a0bc
style: oxfmt project-directory test imports
AiAlchemist0 b920304
style: sort project-directory import in cli.ts
AiAlchemist0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "create-cloudflare": patch | ||
| --- | ||
|
|
||
| Skip `mkdir` when the project parent directory already exists, so `create-cloudflare` works at a Windows drive root (`E:\`) instead of throwing `EPERM`. |
50 changes: 50 additions & 0 deletions
50
packages/create-cloudflare/src/__tests__/project-directory.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { existsSync, mkdirSync } from "node:fs"; | ||
| import { chdir } from "node:process"; | ||
| import { resolve } from "node:path"; | ||
| import { beforeEach, describe, test, vi } from "vitest"; | ||
| import { setupProjectDirectory } from "../project-directory"; | ||
| import type { C3Context } from "types"; | ||
|
|
||
| vi.mock("node:fs"); | ||
| vi.mock("node:process", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("node:process")>(); | ||
| return { ...actual, chdir: vi.fn() }; | ||
| }); | ||
|
|
||
| const ctxFor = (projectPath: string): C3Context => | ||
| ({ | ||
| args: {}, | ||
| project: { name: "my-app", path: projectPath }, | ||
| template: {}, | ||
| deployment: {}, | ||
| originalCWD: "/", | ||
| gitRepoAlreadyExisted: false, | ||
| }) as unknown as C3Context; | ||
|
|
||
| describe("setupProjectDirectory", () => { | ||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| test("does not mkdir when the parent already exists", ({ expect }) => { | ||
| const projectPath = resolve("already-there", "my-app"); | ||
| const parent = resolve("already-there"); | ||
| vi.mocked(existsSync).mockImplementation((p) => String(p) === parent); | ||
|
|
||
| setupProjectDirectory(ctxFor(projectPath)); | ||
|
|
||
| expect(mkdirSync).not.toHaveBeenCalled(); | ||
| expect(chdir).toHaveBeenCalledWith(parent); | ||
| }); | ||
|
|
||
| test("creates the parent when it is missing", ({ expect }) => { | ||
| const projectPath = resolve("new-parent", "my-app"); | ||
| const parent = resolve("new-parent"); | ||
| vi.mocked(existsSync).mockReturnValue(false); | ||
|
|
||
| setupProjectDirectory(ctxFor(projectPath)); | ||
|
|
||
| expect(mkdirSync).toHaveBeenCalledWith(parent, { recursive: true }); | ||
| expect(chdir).toHaveBeenCalledWith(parent); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { existsSync, mkdirSync } from "node:fs"; | ||
| import { dirname } from "node:path"; | ||
| import { chdir } from "node:process"; | ||
| import { validateProjectDirectory } from "./validators"; | ||
| import type { C3Context } from "types"; | ||
|
|
||
| /** | ||
| * Validates the target project directory and ensures its parent exists before | ||
| * changing into it. Skips `mkdir` when the parent already exists so a Windows | ||
| * drive root (`E:\`) does not throw `EPERM`. | ||
| * | ||
| * @param ctx - The C3 context containing the resolved project path and args | ||
| */ | ||
| export const setupProjectDirectory = (ctx: C3Context) => { | ||
| const path = ctx.project.path; | ||
| const err = validateProjectDirectory(path, ctx.args); | ||
| if (err) { | ||
| throw new Error(err); | ||
| } | ||
|
|
||
| const directory = dirname(path); | ||
|
|
||
| // Creating a Windows drive root (`E:\`) throws EPERM. Skip if it already exists. | ||
| if (!existsSync(directory)) { | ||
| mkdirSync(directory, { recursive: true }); | ||
| } | ||
|
|
||
| chdir(directory); | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.