diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index 307183152..f1a341c02 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -83,7 +83,7 @@ export class BuildsInfoCommand extends ApifyCommand { if (build.finishedAt) { message.push( - ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? 0))})`, + ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? build.finishedAt.getTime() - build.startedAt.getTime()))})`, ); } else { const diff = Date.now() - build.startedAt.getTime(); diff --git a/src/commands/builds/log.ts b/src/commands/builds/log.ts index 3329e1b5b..b8ddf3964 100644 --- a/src/commands/builds/log.ts +++ b/src/commands/builds/log.ts @@ -1,5 +1,6 @@ import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; import { Args } from '../../lib/command-framework/args.js'; +import { isReducedBuild } from '../../lib/commands/agent-output.js'; import { info } from '../../lib/outputs.js'; import { getLoggedClientOrThrow, outputJobLog } from '../../lib/utils.js'; @@ -39,6 +40,10 @@ export class BuildsLogCommand extends ApifyCommand { info({ message: `Log for build with ID "${buildId}":\n` }); - await outputJobLog({ job: build, apifyClient }); + const result = await outputJobLog({ job: build, apifyClient }); + + if (result === 'no-logs' && isReducedBuild(build)) { + throw new Error(`You don't have access to the log of build with ID "${buildId}".`); + } } } diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index 5d9021b32..3d029da18 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -107,6 +107,11 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL } } +// The API has no flag for the reduced build it returns to users who can't write to or build the Actor; only full builds carry `options`. +export function isReducedBuild(build: Build): boolean { + return build.options === undefined; +} + export function consoleActorUrl(actorId: string): string { return `${getConsoleUrl()}/actors/${actorId}`; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index cbf925200..ea7bd98b4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -693,14 +693,11 @@ export const outputJobLog = async ({ // In case job was already done just output log if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) { - if (process.env.APIFY_NO_LOGS_IN_TESTS) { - return; - } - - // Undefined when the job failed before its container started. + // Undefined when the job failed before its container started, or when the user can't read the log. const log = await client.log(logId).get(); - if (log) process.stderr.write(log); - return; + if (log === undefined) return 'no-logs'; + if (!process.env.APIFY_NO_LOGS_IN_TESTS) process.stderr.write(log); + return 'finished'; } // In other case stream it to stderr diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts new file mode 100644 index 000000000..e175f2cc9 --- /dev/null +++ b/test/local/commands/builds/reduced-build.test.ts @@ -0,0 +1,67 @@ +import type { ApifyClient } from 'apify-client'; + +import { BuildsInfoCommand } from '../../../../src/commands/builds/info.js'; +import { BuildsLogCommand } from '../../../../src/commands/builds/log.js'; +import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js'; +import { useAuthSetup } from '../../../__setup__/hooks/useAuthSetup.js'; +import { useConsoleSpy } from '../../../__setup__/hooks/useConsoleSpy.js'; + +const { mockGetLoggedClientOrThrow } = vitest.hoisted(() => ({ + mockGetLoggedClientOrThrow: vitest.fn(), +})); + +vitest.mock('../../../../src/lib/utils.js', async (importOriginal) => ({ + ...(await importOriginal()), + getLoggedClientOrThrow: mockGetLoggedClientOrThrow, +})); + +useAuthSetup(); + +const { logMessages } = useConsoleSpy(); + +// What the API returns to users who can read the Actor but can neither write to it nor build it. +const reducedBuild = { + meta: { origin: 'CLI' }, + id: 'buildId', + actId: 'actorId', + userId: 'userId', + buildNumber: '1.0.30', + status: 'FAILED', + startedAt: new Date('2026-01-01T10:00:00.000Z'), + finishedAt: new Date('2026-01-01T10:01:05.000Z'), +}; + +const fakeClient = { + actor: () => ({ get: async () => undefined }), + build: () => ({ get: async () => reducedBuild }), + log: () => ({ get: async () => undefined }), +} as unknown as ApifyClient; + +// The command framework records a thrown error on the real `process`; put it back so it cannot leak. +let originalExitCode: typeof process.exitCode; + +beforeEach(() => { + originalExitCode = process.exitCode; + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient); +}); + +afterEach(() => { + process.exitCode = originalExitCode; +}); + +describe('builds commands with a reduced build', () => { + it('builds info computes the duration from the timestamps', async () => { + await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); + + const output = logMessages.log.join('\n'); + + expect(output).toContain('took 1 minute 5 seconds'); + expect(output).not.toContain('Compute Units'); + }); + + it('builds log says the user has no access to the log', async () => { + await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); + + expect(logMessages.error.join('\n')).toContain(`You don't have access to the log of build with ID "buildId".`); + }); +});