anthropics / anthropics/claude-code-action
Action fails on PRs with newly added files - git hash-object called before PR checkout
- Lingua principale
- TypeScript
- Stelle
- 8.9k
- Fork
- 2.1k
- Merge medio
- 3g 9h
- PR unite (30g)
- 10
Descrizione
## Description
The `anthropics/claude-code-action@v1` fails when triggered on pull requests that contain newly added files. The action attempts to compute git SHA hashes for all changed files **before** checking out the PR branch, causing `git hash-object` to fail for files that don't exist in the base branch.
## Steps to Reproduce
1. Create a PR that adds new files (not just modifies existing ones)
2. Comment `@claude code review` on the PR
3. Action fails with error: `fatal: could not open '' for reading: No such file or directory`
## Expected Behavior
The action should successfully process PRs with newly added files by either:
- Computing SHAs after checking out the PR branch
- Skipping SHA computation for files marked as "ADDED"
- Using GitHub API to retrieve SHAs instead of `git hash-object`
## Actual Behavior
The action fails during the `fetchGitHubData()` call in `prepareTagMode()`, before the PR branch is checked out.
### Error Log
```
✅ Created initial comment with ID: 3963555987
Successfully fetched PR #24792 data
fatal: could not open 'net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs' for reading: No such file or directory
Failed to compute SHA for net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs: warn: Command failed: git hash-object net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs
fatal: could not open 'net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs' for reading: No such file or directory
signal: null,
status: 128,
output: [
null, "", "fatal: could not open 'net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs' for reading: No such file or directory\n"
],
pid: 2178,
stdout: "",
stderr: "fatal: could not open 'net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs' for reading: No such file or directory\n",
at genericNodeError (node:child_process:1000:13)
at checkExecSyncError (node:child_process:458:27)
at execFileSync (node:child_process:269:31)
at (/home/runner/work/_actions/anthropics/claude-code-action/v1/src/github/data/fetcher.ts:354:21)
at map (1:11)
at fetchGitHubData (/home/runner/work/_actions/anthropics/claude-code-action/v1/src/github/data/fetcher.ts:343:40)
at async prepareTagMode (/home/runner/work/_actions/anthropics/claude-code-action/v1/src/modes/tag/index.ts:52:28)
at async run (/home/runner/work/_actions/anthropics/claude-code-action/v1/src/entrypoints/run.ts:201:17)
```
Later in the same log:
```
This is an open PR, checking out PR branch...
PR #24792: 1 commits, using fetch depth 20
From https://github.com/UiPath/Cloud-RPA
* branch jj_cap_trace_integration -> FETCH_HEAD
Switched to a new branch 'jj_cap_trace_integration'
```
## Root Cause
The execution order in the action is:
1. ✅ Get PR metadata from GitHub API
2. ❌ **Call `fetchGitHubData()` which runs `git hash-object` on each changed file** (FAILS HERE)
3. ✅ Check out PR branch (files become available, but too late)
The error occurs in `src/github/data/fetcher.ts:354` when processing the changed files list. For files marked as "ADDED", these don't exist in the current checkout (base branch), causing `git hash-object` to fail.
## Environment
- **Action version**: `anthropics/claude-code-action@v1`
- **Trigger**: `issue_comment` with `@claude` mention
- **Event**: PR comment on pull request
## Technical Details
### Stack Trace Location
```
/home/runner/work/_actions/anthropics/claude-code-action/v1/src/github/data/fetcher.ts:354
↓ called from
/home/runner/work/_actions/anthropics/claude-code-action/v1/src/github/data/fetcher.ts:343 (fetchGitHubData)
↓ called from
/home/runner/work/_actions/anthropics/claude-code-action/v1/src/modes/tag/index.ts:52 (prepareTagMode)
```
### Changed Files Metadata
The GitHub API correctly returns these files with status "ADDED" and "SHA: unknown":
```
- net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubClient.cs (ADDED) +51/-0 SHA: unknown
- net5/AuthorizationService/src/Application/PolicyEngine/EventHub/EventHubMessagingService.cs (ADDED) +79/-0 SHA: unknown
- net5/AuthorizationService/src/Application/PolicyEngine/EventHub/IEventHubFacade.cs (ADDED) +12/-0 SHA: unknown
... (8 newly added files total)
```
## Suggested Fixes
### Option 1: Skip SHA computation for newly added files ⭐ (Recommended)
```typescript
// In fetcher.ts:343
const files = prData.files.map(file => {
if (file.status === 'added') {
// Skip git hash-object for newly added files
return { ...file, sha: null };
}
// Compute SHA for existing files
const sha = execSync(`git hash-object ${file.filename}`).toString().trim();
return { ...file, sha };
});
```
### Option 2: Compute SHAs after PR checkout
Move the SHA computation logic to after the PR branch is checked out, when all files are guaranteed to exist.
### Option 3: Use GitHub API for SHAs
Get file SHAs from the GitHub API response instead of running `git hash-object` locally. The PR files endpoint already includes SHA information for most files.
## Workaround
**None available** from workflow configuration.
Attempted workaround (checking out PR branch before action step) doesn't help because the action's internal code runs SHA computation before its own checkout process completes.
Users must use Claude Code CLI locally as a temporary workaround:
```bash
claude "Review the changes in this PR compared to origin/dev"
```
## Impact
This bug prevents the action from being used on **any PR that introduces new files** to the codebase, significantly limiting its usefulness for real-world development workflows where adding new files is common.
## Additional Context
- The action successfully handles PRs that only modify existing files
- The changed files metadata from GitHub API is correct and includes the "ADDED" status
- Multiple files in the same PR all fail with the same error pattern
Guida per i contributori
Apri la guida per i contributori
Valutazione
Questa issue non è ancora stata valutata.