-
Notifications
You must be signed in to change notification settings - Fork 57
fix(cdxgen): report why cdxgen failed instead of exiting 1 in silence #1470
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
John-David Dalton (jdalton)
merged 1 commit into
main
from
jdalton/surf-1045-cdxgen-silent-failure
Aug 4, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /** | ||
| * Diagnostics for the `socket cdxgen` command. | ||
| * | ||
| * Cdxgen runs as a child process, so when it cannot start there is nothing on | ||
| * stdout or stderr to explain why. These helpers turn that into a message that | ||
| * names where the CLI looked for cdxgen and what to try next. | ||
| */ | ||
|
|
||
| import { existsSync } from 'node:fs' | ||
|
|
||
| import { errorMessage } from '@socketsecurity/lib-stable/errors/message' | ||
|
|
||
| import { resolveCdxgen } from './resolve-binary.mjs' | ||
| import { InputError } from '../error/errors-types.mts' | ||
|
|
||
| /** | ||
| * Describe where the CLI resolved cdxgen from, so an error can say which of | ||
| * the two sources actually failed. | ||
| */ | ||
| export function describeCdxgenSource(): string { | ||
| const resolution = resolveCdxgen() | ||
| if (resolution.type === 'local') { | ||
| return `the local override SOCKET_CLI_CDXGEN_LOCAL_PATH=${resolution.path}` | ||
| } | ||
| if (resolution.type === 'dlx') { | ||
| const { name, version } = resolution.details | ||
| return `${name}@${version}, downloaded and cached by Socket's dlx` | ||
| } | ||
| return "a binary downloaded and cached by Socket's dlx" | ||
| } | ||
|
|
||
| /** | ||
| * Build the message shown when cdxgen fails to produce a result. | ||
| * | ||
| * Pass the underlying error when there is one. Pass nothing for the case where | ||
| * the child process ended without reporting either an exit code or a signal, | ||
| * which is the shape that used to exit 1 with no output at all. | ||
| */ | ||
| export function formatCdxgenFailureMessage( | ||
| cause?: unknown | undefined, | ||
| ): string { | ||
| // An InputError is raised with a message already written for the exact thing | ||
| // that went wrong, so pass it through. Wrapping it would nest one | ||
| // Where/Saw/Fix block inside another, and the generic Fix below would tell | ||
| // the user to set SOCKET_CLI_CDXGEN_LOCAL_PATH when a bad value for that | ||
| // very variable is what they are being told about. | ||
| if (cause instanceof InputError) { | ||
| return cause.message | ||
| } | ||
|
|
||
| const detail = cause | ||
| ? errorMessage(cause) | ||
| : 'the cdxgen process ended without reporting an exit code or a signal' | ||
|
|
||
| return [ | ||
| 'socket cdxgen could not run cdxgen.', | ||
| ` Where: ${describeCdxgenSource()}`, | ||
| ` Saw: ${detail || 'no error message was reported'}`, | ||
| ' Fix: Re-run with SOCKET_CLI_DEBUG=1 to see the full error. If cdxgen', | ||
| ' cannot be downloaded on this machine, install it yourself and', | ||
| ' point SOCKET_CLI_CDXGEN_LOCAL_PATH at the binary.', | ||
| ].join('\n') | ||
| } | ||
|
|
||
| /** | ||
| * Build the message shown when SOCKET_CLI_CDXGEN_LOCAL_PATH points at | ||
| * something that is not there. | ||
| * | ||
| * Without this check the override is silently ignored and the failure looks | ||
| * identical to a download failure, which makes the override appear to have no | ||
| * effect at all. | ||
| */ | ||
| export function formatMissingCdxgenLocalPathMessage(localPath: string): string { | ||
| return [ | ||
| 'SOCKET_CLI_CDXGEN_LOCAL_PATH points at a file that does not exist.', | ||
| ` Where: ${localPath}`, | ||
| ' Saw: nothing at that path', | ||
| ' Fix: Correct the path, or unset SOCKET_CLI_CDXGEN_LOCAL_PATH to let', | ||
| ' Socket download cdxgen itself. On a CI runner, check that the', | ||
| ' path exists in the same step that runs socket cdxgen.', | ||
| ].join('\n') | ||
| } | ||
|
|
||
| /** | ||
| * True when a local cdxgen override is configured but missing from disk. | ||
| */ | ||
| export function isMissingCdxgenLocalPath(localPath: string): boolean { | ||
| return !existsSync(localPath) | ||
| } |
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
189 changes: 189 additions & 0 deletions
189
packages/cli/test/unit/commands/manifest/cmd-manifest-cdxgen-failure-reporting.test.mts
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,189 @@ | ||
| /** | ||
| * Unit tests for the cdxgen command's failure reporting. | ||
| * | ||
| * Purpose: The command arms process.exitCode = 1 before spawning cdxgen, and | ||
| * cdxgen itself runs with stdio: 'inherit'. Together that means any path out | ||
| * of the spawn that neither exits deliberately nor prints leaves the user with | ||
| * exit code 1 and an empty log. These tests cover every one of those paths. | ||
| * | ||
| * Related Files: - src/commands/manifest/cmd-manifest-cdxgen.mts. | ||
| */ | ||
|
|
||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { cmdManifestCdxgen } from '../../../../src/commands/manifest/cmd-manifest-cdxgen.mts' | ||
| import { formatMissingCdxgenLocalPathMessage } from '../../../../src/util/dlx/cdxgen-diagnostics.mts' | ||
| import { InputError } from '../../../../src/util/error/errors-types.mts' | ||
|
|
||
| const mockLogger = vi.hoisted(() => ({ | ||
| error: vi.fn(), | ||
| fail: vi.fn(), | ||
| info: vi.fn(), | ||
| log: vi.fn(), | ||
| success: vi.fn(), | ||
| warn: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock(import('@socketsecurity/lib-stable/logger/default'), () => ({ | ||
| getDefaultLogger: () => mockLogger, | ||
| })) | ||
|
|
||
| const mockDebugNs = vi.hoisted(() => vi.fn()) | ||
|
|
||
| vi.mock( | ||
| import('@socketsecurity/lib-stable/debug/output'), | ||
| async importOriginal => ({ | ||
| ...(await importOriginal()), | ||
| debugNs: mockDebugNs, | ||
| }), | ||
| ) | ||
|
|
||
| const mockRunCdxgen = vi.hoisted(() => vi.fn()) | ||
| const mockDetectNodejsCdxgenSources = vi.hoisted(() => | ||
| vi.fn().mockResolvedValue({ hasLockfile: true, hasNodeModules: true }), | ||
| ) | ||
| const mockIsNodejsCdxgenType = vi.hoisted(() => vi.fn().mockReturnValue(true)) | ||
|
|
||
| vi.mock(import('../../../../src/commands/manifest/run-cdxgen.mts'), () => ({ | ||
| detectNodejsCdxgenSources: mockDetectNodejsCdxgenSources, | ||
| isNodejsCdxgenType: mockIsNodejsCdxgenType, | ||
| runCdxgen: mockRunCdxgen, | ||
| })) | ||
|
|
||
| describe('cmd-manifest-cdxgen failure reporting', () => { | ||
| const importMeta = { url: 'file:///test/cmd-manifest-cdxgen.mts' } | ||
| const context = { parentName: 'socket manifest' } | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mockDetectNodejsCdxgenSources.mockResolvedValue({ | ||
| hasLockfile: true, | ||
| hasNodeModules: true, | ||
| }) | ||
| mockIsNodejsCdxgenType.mockReturnValue(true) | ||
| process.exitCode = undefined | ||
| }) | ||
|
|
||
| describe('never fails silently', () => { | ||
| // The command arms process.exitCode = 1 before spawning cdxgen. Every | ||
| // way out of the spawn must therefore either exit deliberately or print | ||
| // something, otherwise the user gets exit code 1 and an empty log. | ||
| it('reports an actionable error when cdxgen cannot be started', async () => { | ||
| mockRunCdxgen.mockRejectedValue(new Error('spawn cdxgen ENOENT')) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| expect(mockLogger.fail).toHaveBeenCalledTimes(1) | ||
| const message = String(mockLogger.fail.mock.calls[0]?.[0] ?? '') | ||
| expect(message.trim()).not.toBe('') | ||
| expect(message).toContain('socket cdxgen could not run cdxgen.') | ||
| expect(message).toContain('spawn cdxgen ENOENT') | ||
| expect(process.exitCode).toBe(1) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| it('reports an actionable error when the spawn itself rejects', async () => { | ||
| mockRunCdxgen.mockResolvedValue({ | ||
| spawnPromise: Promise.reject(new Error('cdxgen download failed')), | ||
| }) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| expect(mockLogger.fail).toHaveBeenCalledTimes(1) | ||
| const message = String(mockLogger.fail.mock.calls[0]?.[0] ?? '') | ||
| expect(message).toContain('cdxgen download failed') | ||
| expect(process.exitCode).toBe(1) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| it('reports an actionable error when cdxgen ends with no exit code and no signal', async () => { | ||
| mockRunCdxgen.mockResolvedValue({ | ||
| spawnPromise: Promise.resolve({ code: undefined, signal: undefined }), | ||
| }) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| expect(mockExit).not.toHaveBeenCalled() | ||
| expect(mockLogger.fail).toHaveBeenCalledTimes(1) | ||
| const message = String(mockLogger.fail.mock.calls[0]?.[0] ?? '') | ||
| expect(message).toContain('without reporting an exit code or a signal') | ||
| expect(process.exitCode).toBe(1) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| // The failure message tells the user to re-run with SOCKET_CLI_DEBUG=1. | ||
| // That flag turns on the 'error' debug category, so the underlying detail | ||
| // has to be emitted there or the advice sends them nowhere. | ||
| it('makes SOCKET_CLI_DEBUG=1 worth running after a failed spawn', async () => { | ||
| const cause = new Error('spawn cdxgen ENOENT') | ||
| mockRunCdxgen.mockRejectedValue(cause) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| const debugCall = mockDebugNs.mock.calls.find(call => call[0] === 'error') | ||
| expect(debugCall).toBeDefined() | ||
| expect(debugCall).toContain(cause) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| it('makes SOCKET_CLI_DEBUG=1 worth running after a resultless exit', async () => { | ||
| const spawnResult = { code: undefined, signal: undefined } | ||
| mockRunCdxgen.mockResolvedValue({ | ||
| spawnPromise: Promise.resolve(spawnResult), | ||
| }) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| const debugCall = mockDebugNs.mock.calls.find(call => call[0] === 'error') | ||
| expect(debugCall).toBeDefined() | ||
| expect(debugCall).toContain(spawnResult) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| it('prints the missing-override message as written, without wrapping it', async () => { | ||
| const explained = formatMissingCdxgenLocalPathMessage('/tmp/acme-cdxgen') | ||
| mockRunCdxgen.mockRejectedValue(new InputError(explained)) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| expect(mockLogger.fail).toHaveBeenCalledTimes(1) | ||
| const message = String(mockLogger.fail.mock.calls[0]?.[0] ?? '') | ||
| expect(message).toBe(explained) | ||
| expect(process.exitCode).toBe(1) | ||
| mockExit.mockRestore() | ||
| }) | ||
|
|
||
| it('stays quiet on the success path', async () => { | ||
| mockRunCdxgen.mockResolvedValue({ | ||
| spawnPromise: Promise.resolve({ code: 0, signal: undefined }), | ||
| }) | ||
| const mockExit = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((() => {}) as unknown) | ||
|
|
||
| await cmdManifestCdxgen.run(['.'], importMeta, context) | ||
|
|
||
| expect(mockLogger.fail).not.toHaveBeenCalled() | ||
| expect(mockExit).toHaveBeenCalledWith(0) | ||
| mockExit.mockRestore() | ||
| }) | ||
| }) | ||
| }) |
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.