-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: run add-dependency installs in a PTY #3167
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
856a259
feat: run add-dependency installs in a PTY
wwwillchen 1cde31f
docs: record session learnings
wwwillchen 701efef
Address PR review comments
wwwillchen 21eaaa2
Address PR review comments
wwwillchen 79a7d3b
Update src/ipc/processors/executeAddDependency.ts
wwwillchen d737bea
Update src/ipc/processors/executeAddDependency.ts
wwwillchen 2bacec8
Address PR review comments
wwwillchen 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
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
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
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,8 @@ | ||
| # Native Modules | ||
|
|
||
| Read this when adding Electron native dependencies such as `node-pty`, or any package that ships `.node` binaries, helper executables, or rebuild-time headers. | ||
|
|
||
| - This repo's `forge.config.ts` uses a deny-by-default `ignore` filter for most `node_modules` content. When adding a native dependency, explicitly allowlist the runtime package and any rebuild-time helper packages it requires (for example `node-addon-api`), or Electron Forge can fail during `Preparing native dependencies` with errors like `Cannot find module 'node-addon-api'`. | ||
| - Add native runtime packages to `vite.main.config.mts` `build.rollupOptions.external` so Vite does not bundle them into the main-process build. | ||
| - Add native runtime packages to `forge.config.ts` `rebuildConfig.extraModules` so Electron Forge rebuilds them against the packaged Electron version. | ||
| - If the package loads helper binaries from disk at runtime (for example `node-pty` loading `spawn-helper` or `winpty-agent` next to its native module), unpack the whole package directory with `packagerConfig.asar.unpackDir`; auto-unpacking `.node` files alone is not enough. |
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,146 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| normalizePtyOutput, | ||
| PtyCommandExecutionError, | ||
| runPtyCommand, | ||
| } from "./pty_command_runner"; | ||
|
|
||
| const { spawnMock } = vi.hoisted(() => ({ | ||
| spawnMock: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("node-pty", () => ({ | ||
| spawn: spawnMock, | ||
| })); | ||
|
|
||
| interface MockPtyController { | ||
| emitData(data: string): void; | ||
| emitExit(event: { exitCode: number; signal?: number }): void; | ||
| pty: { | ||
| kill: ReturnType<typeof vi.fn>; | ||
| onData: ReturnType<typeof vi.fn>; | ||
| onExit: ReturnType<typeof vi.fn>; | ||
| }; | ||
| } | ||
|
|
||
| function createMockPtyController(): MockPtyController { | ||
| const dataListeners = new Set<(data: string) => void>(); | ||
| const exitListeners = new Set< | ||
| (event: { exitCode: number; signal?: number }) => void | ||
| >(); | ||
|
|
||
| return { | ||
| emitData(data) { | ||
| for (const listener of dataListeners) { | ||
| listener(data); | ||
| } | ||
| }, | ||
| emitExit(event) { | ||
| for (const listener of exitListeners) { | ||
| listener(event); | ||
| } | ||
| }, | ||
| pty: { | ||
| kill: vi.fn(), | ||
| onData: vi.fn((listener: (data: string) => void) => { | ||
| dataListeners.add(listener); | ||
| return { | ||
| dispose: () => dataListeners.delete(listener), | ||
| }; | ||
| }), | ||
| onExit: vi.fn( | ||
| (listener: (event: { exitCode: number; signal?: number }) => void) => { | ||
| exitListeners.add(listener); | ||
| return { | ||
| dispose: () => exitListeners.delete(listener), | ||
| }; | ||
| }, | ||
| ), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("normalizePtyOutput", () => { | ||
| it("strips ANSI sequences and keeps the last carriage-return update", () => { | ||
| expect( | ||
| normalizePtyOutput( | ||
| "\u001b]0;npm install\u0007\u001b[32mfetching\u001b[0m\rfetched\nabc\bXY\r\n", | ||
| ), | ||
| ).toBe("fetched\nabXY"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("runPtyCommand", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("captures normalized PTY output on success", async () => { | ||
| const controller = createMockPtyController(); | ||
| spawnMock.mockReturnValue(controller.pty); | ||
|
|
||
| const promise = runPtyCommand("npx", ["sfw", "--help"], { | ||
| cwd: "/tmp/app", | ||
| }); | ||
|
|
||
| expect(spawnMock).toHaveBeenCalledWith( | ||
| "npx", | ||
| ["sfw", "--help"], | ||
| expect.objectContaining({ | ||
| cols: 160, | ||
| cwd: "/tmp/app", | ||
| encoding: "utf8", | ||
| env: process.env, | ||
| name: "xterm-color", | ||
| rows: 24, | ||
| }), | ||
| ); | ||
|
|
||
| controller.emitData("\u001b[32mResolving\u001b[0m\rResolved\n"); | ||
| controller.emitData("added 1 package\r\n"); | ||
| controller.emitExit({ exitCode: 0 }); | ||
|
|
||
| await expect(promise).resolves.toEqual({ | ||
| output: "Resolved\nadded 1 package", | ||
| }); | ||
| }); | ||
|
|
||
| it("rejects with the captured output when the PTY exits non-zero", async () => { | ||
| const controller = createMockPtyController(); | ||
| spawnMock.mockReturnValue(controller.pty); | ||
|
|
||
| const promise = runPtyCommand("pnpm", ["add", "react"]); | ||
|
|
||
| controller.emitData("blocked react\n"); | ||
| controller.emitExit({ exitCode: 1 }); | ||
|
|
||
| await expect(promise).rejects.toMatchObject({ | ||
| exitCode: 1, | ||
| message: "Command 'pnpm add react' exited with code 1", | ||
| name: "PtyCommandExecutionError", | ||
| output: "blocked react", | ||
| } satisfies Partial<PtyCommandExecutionError>); | ||
| }); | ||
|
|
||
| it("kills the PTY and rejects when the command times out", async () => { | ||
| vi.useFakeTimers(); | ||
| const controller = createMockPtyController(); | ||
| spawnMock.mockReturnValue(controller.pty); | ||
|
|
||
| const promise = runPtyCommand("npx", ["sfw"], { | ||
| timeoutMs: 25, | ||
| }); | ||
| controller.emitData("still running"); | ||
|
|
||
| const rejection = expect(promise).rejects.toMatchObject({ | ||
| exitCode: null, | ||
| message: "Command 'npx sfw' timed out after 25ms", | ||
| output: "still running", | ||
| } satisfies Partial<PtyCommandExecutionError>); | ||
|
|
||
| await vi.advanceTimersByTimeAsync(25); | ||
| await rejection; | ||
| expect(controller.pty.kill).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.