Skip to content

Commit 65987a7

Browse files
committed
Add more debug
1 parent a1c4740 commit 65987a7

File tree

4 files changed

+140
-50
lines changed

4 files changed

+140
-50
lines changed

src/commands/fix/npm-fix.mts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,42 @@ async function install(
2424
pkgEnvDetails: EnvDetails,
2525
options: InstallOptions,
2626
): Promise<NodeClass | null> {
27-
const { args, cwd, spinner } = {
27+
const {
28+
args: extraArgs,
29+
cwd,
30+
spinner,
31+
} = {
2832
__proto__: null,
2933
...options,
3034
} as InstallOptions
35+
const args = [
36+
'--ignore-scripts',
37+
'--no-audit',
38+
'--no-fund',
39+
'--no-progress',
40+
'--no-save',
41+
'--silent',
42+
...(extraArgs ?? []),
43+
]
44+
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
45+
debugFn('stdio', `spawn: ${quotedCmd}`)
3146
try {
3247
await runAgentInstall(pkgEnvDetails, {
33-
args: [
34-
'--ignore-scripts',
35-
'--no-audit',
36-
'--no-fund',
37-
'--no-progress',
38-
'--no-save',
39-
'--silent',
40-
...(args ?? []),
41-
],
48+
args,
4249
spinner,
4350
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
4451
})
52+
} catch (e) {
53+
debugFn('error', `caught: ${quotedCmd} failed`)
54+
debugDir('inspect', { error: e })
55+
return null
56+
}
57+
try {
4558
return await getActualTree(cwd)
46-
} catch {}
59+
} catch (e) {
60+
debugFn('error', 'caught: Arborist error')
61+
debugDir('inspect', { error: e })
62+
}
4763
return null
4864
}
4965

src/commands/fix/pnpm-fix.mts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,42 @@ async function install(
3333
pkgEnvDetails: EnvDetails,
3434
options: InstallOptions,
3535
): Promise<NodeClass | null> {
36-
const { args, cwd, spinner } = {
36+
const {
37+
args: extraArgs,
38+
cwd,
39+
spinner,
40+
} = {
3741
__proto__: null,
3842
...options,
3943
} as InstallOptions
44+
const args = [
45+
// Enable pnpm updates to pnpm-lock.yaml in CI environments.
46+
// https://pnpm.io/cli/install#--frozen-lockfile
47+
'--no-frozen-lockfile',
48+
// Enable a non-interactive pnpm install
49+
// https://github.com/pnpm/pnpm/issues/6778
50+
'--config.confirmModulesPurge=false',
51+
...(extraArgs ?? []),
52+
]
53+
const quotedCmd = `\`${pkgEnvDetails.agent} install ${args.join(' ')}\``
54+
debugFn('stdio', `spawn: ${quotedCmd}`)
4055
try {
4156
await runAgentInstall(pkgEnvDetails, {
42-
args: [
43-
// Enable pnpm updates to pnpm-lock.yaml in CI environments.
44-
// https://pnpm.io/cli/install#--frozen-lockfile
45-
'--no-frozen-lockfile',
46-
// Enable a non-interactive pnpm install
47-
// https://github.com/pnpm/pnpm/issues/6778
48-
'--config.confirmModulesPurge=false',
49-
...(args ?? []),
50-
],
57+
args,
5158
spinner,
5259
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
5360
})
61+
} catch (e) {
62+
debugFn('error', `caught: ${quotedCmd} failed`)
63+
debugDir('inspect', { error: e })
64+
return null
65+
}
66+
try {
5467
return await getActualTree(cwd)
55-
} catch {}
68+
} catch (e) {
69+
debugFn('error', 'caught: Arborist error')
70+
debugDir('inspect', { error: e })
71+
}
5672
return null
5773
}
5874

src/commands/fix/pull-request.mts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,21 @@ export async function setGitRemoteGithubRepoUrl(
534534
repo: string,
535535
token: string,
536536
cwd = process.cwd(),
537-
): Promise<void> {
537+
): Promise<boolean> {
538+
const { host } = new URL(constants.ENV.GITHUB_SERVER_URL)
539+
const url = `https://x-access-token:${token}@${host}/${owner}/${repo}`
538540
const stdioIgnoreOptions: SpawnOptions = {
539541
cwd,
540542
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
541543
}
542-
const { host } = new URL(constants.ENV.GITHUB_SERVER_URL)
543-
const url = `https://x-access-token:${token}@${host}/${owner}/${repo}`
544+
const quotedCmd = `\`git remote set-url origin ${url}\``
545+
debugFn('stdio', `spawn: ${quotedCmd}`)
544546
try {
545547
await spawn('git', ['remote', 'set-url', 'origin', url], stdioIgnoreOptions)
548+
return true
546549
} catch (e) {
547-
debugFn('error', 'caught: unexpected error')
550+
debugFn('error', `caught: ${quotedCmd} failed`)
548551
debugDir('inspect', { error: e })
549552
}
553+
return false
550554
}

src/utils/git.mts

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,21 @@ export type GitCreateAndPushBranchOptions = {
9797
user?: string | undefined
9898
}
9999

100-
export async function gitCleanFdx(cwd = process.cwd()): Promise<void> {
100+
export async function gitCleanFdx(cwd = process.cwd()): Promise<boolean> {
101101
const stdioIgnoreOptions: SpawnOptions = {
102102
cwd,
103103
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
104104
}
105-
// TODO: propagate CResult?
106-
await spawn('git', ['clean', '-fdx'], stdioIgnoreOptions)
105+
const quotedCmd = '`git clean -fdx`'
106+
debugFn('stdio', `spawn: ${quotedCmd}`)
107+
try {
108+
await spawn('git', ['clean', '-fdx'], stdioIgnoreOptions)
109+
return true
110+
} catch (e) {
111+
debugFn('error', `caught: ${quotedCmd} failed`)
112+
debugDir('inspect', { error: e })
113+
}
114+
return false
107115
}
108116

109117
export async function gitCheckoutBranch(
@@ -114,10 +122,15 @@ export async function gitCheckoutBranch(
114122
cwd,
115123
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
116124
}
125+
const quotedCmd = `\`git checkout ${branch}\``
126+
debugFn('stdio', `spawn: ${quotedCmd}`)
117127
try {
118128
await spawn('git', ['checkout', branch], stdioIgnoreOptions)
119129
return true
120-
} catch {}
130+
} catch (e) {
131+
debugFn('error', `caught: ${quotedCmd} failed`)
132+
debugDir('inspect', { error: e })
133+
}
121134
return false
122135
}
123136

@@ -132,10 +145,15 @@ export async function gitCreateBranch(
132145
cwd,
133146
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
134147
}
148+
const quotedCmd = `\`git branch ${branch}\``
149+
debugFn('stdio', `spawn: ${quotedCmd}`)
135150
try {
136151
await spawn('git', ['branch', branch], stdioIgnoreOptions)
137152
return true
138-
} catch {}
153+
} catch (e) {
154+
debugFn('error', `caught: ${quotedCmd} failed`)
155+
debugDir('inspect', { error: e })
156+
}
139157
return false
140158
}
141159

@@ -147,6 +165,8 @@ export async function gitPushBranch(
147165
cwd,
148166
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
149167
}
168+
const quotedCmd = `\`git push --force --set-upstream origin ${branch}\``
169+
debugFn('stdio', `spawn: ${quotedCmd}`)
150170
try {
151171
await spawn(
152172
'git',
@@ -155,14 +175,11 @@ export async function gitPushBranch(
155175
)
156176
return true
157177
} catch (e) {
158-
debugFn(
159-
'error',
160-
`caught: git push --force --set-upstream origin ${branch} failed`,
161-
)
178+
debugFn('error', `caught: ${quotedCmd} failed`)
162179
if (isSpawnError(e) && e.code === 128) {
163180
debugFn(
164181
'error',
165-
'denied: token requires contents: write; pull-requests: write permissions',
182+
"denied: token requires write permissions for 'contents' and 'pull-requests'",
166183
)
167184
}
168185
debugDir('inspect', { error: e })
@@ -186,16 +203,31 @@ export async function gitCommit(
186203
// Lazily access constants.ENV.SOCKET_CLI_GIT_USER_NAME.
187204
user = constants.ENV.SOCKET_CLI_GIT_USER_NAME,
188205
} = { __proto__: null, ...options } as GitCreateAndPushBranchOptions
206+
207+
await gitEnsureIdentity(user, email, cwd)
208+
189209
const stdioIgnoreOptions: SpawnOptions = {
190210
cwd,
191211
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
192212
}
213+
const quotedAddCmd = `\`git add ${filepaths.join(' ')}\``
214+
debugFn('stdio', `spawn: ${quotedAddCmd}`)
193215
try {
194-
await gitEnsureIdentity(user, email, cwd)
195216
await spawn('git', ['add', ...filepaths], stdioIgnoreOptions)
217+
} catch (e) {
218+
debugFn('error', `caught: ${quotedAddCmd} failed`)
219+
debugDir('inspect', { error: e })
220+
}
221+
222+
const quotedCommitCmd = `\`git commit -m ${commitMsg}\``
223+
debugFn('stdio', `spawn: ${quotedCommitCmd}`)
224+
try {
196225
await spawn('git', ['commit', '-m', commitMsg], stdioIgnoreOptions)
197226
return true
198-
} catch {}
227+
} catch (e) {
228+
debugFn('error', `caught: ${quotedCommitCmd} failed`)
229+
debugDir('inspect', { error: e })
230+
}
199231
return false
200232
}
201233

@@ -207,11 +239,16 @@ export async function gitDeleteBranch(
207239
cwd,
208240
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
209241
}
242+
const quotedCmd = `\`git branch -D ${branch}\``
243+
debugFn('stdio', `spawn: ${quotedCmd}`)
210244
try {
211245
// Will throw with exit code 1 if branch does not exist.
212246
await spawn('git', ['branch', '-D', branch], stdioIgnoreOptions)
213247
return true
214-
} catch {}
248+
} catch (e) {
249+
debugFn('error', `caught: ${quotedCmd} failed`)
250+
debugDir('inspect', { error: e })
251+
}
215252
return false
216253
}
217254

@@ -220,10 +257,6 @@ export async function gitEnsureIdentity(
220257
email: string,
221258
cwd = process.cwd(),
222259
): Promise<void> {
223-
const stdioIgnoreOptions: SpawnOptions = {
224-
cwd,
225-
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
226-
}
227260
const stdioPipeOptions: SpawnOptions = { cwd }
228261
const identEntries: Array<[string, string]> = [
229262
['user.email', name],
@@ -239,10 +272,16 @@ export async function gitEnsureIdentity(
239272
).stdout
240273
} catch {}
241274
if (configValue !== value) {
275+
const stdioIgnoreOptions: SpawnOptions = {
276+
cwd,
277+
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
278+
}
279+
const quotedCmd = `\`git config ${prop} ${value}\``
280+
debugFn('stdio', `spawn: ${quotedCmd}`)
242281
try {
243282
await spawn('git', ['config', prop, value], stdioIgnoreOptions)
244283
} catch (e) {
245-
debugFn('error', `caught: git config ${prop} ${value} failed`)
284+
debugFn('error', `caught: ${quotedCmd} failed`)
246285
debugDir('inspect', { error: e })
247286
}
248287
}
@@ -258,6 +297,8 @@ export async function gitLocalBranchExists(
258297
cwd,
259298
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
260299
}
300+
const quotedCmd = `\`git show-ref --quiet refs/heads/${branch}\``
301+
debugFn('stdio', `spawn: ${quotedCmd}`)
261302
try {
262303
// Will throw with exit code 1 if the branch does not exist.
263304
await spawn(
@@ -266,7 +307,10 @@ export async function gitLocalBranchExists(
266307
stdioIgnoreOptions,
267308
)
268309
return true
269-
} catch {}
310+
} catch (e) {
311+
debugFn('error', `caught: ${quotedCmd} failed`)
312+
debugDir('inspect', { error: e })
313+
}
270314
return false
271315
}
272316

@@ -302,29 +346,39 @@ export async function gitResetAndClean(
302346
export async function gitResetHard(
303347
branch = 'HEAD',
304348
cwd = process.cwd(),
305-
): Promise<void> {
349+
): Promise<boolean> {
306350
const stdioIgnoreOptions: SpawnOptions = {
307351
cwd,
308352
stdio: isDebug('stdio') ? 'inherit' : 'ignore',
309353
}
310-
await spawn('git', ['reset', '--hard', branch], stdioIgnoreOptions)
354+
const quotedCmd = `\`git reset --hard ${branch}\``
355+
debugFn('stdio', `spawn: ${quotedCmd}`)
356+
try {
357+
await spawn('git', ['reset', '--hard', branch], stdioIgnoreOptions)
358+
return true
359+
} catch (e) {
360+
debugFn('error', `caught: ${quotedCmd} failed`)
361+
debugDir('inspect', { error: e })
362+
}
363+
return false
311364
}
312365

313366
export async function gitUnstagedModifiedFiles(
314367
cwd = process.cwd(),
315368
): Promise<CResult<string[]>> {
369+
const stdioPipeOptions: SpawnOptions = { cwd }
370+
const quotedCmd = `\`git diff --name-only\``
316371
try {
317-
const stdioPipeOptions: SpawnOptions = { cwd }
318372
const changedFilesDetails = (
319373
await spawn('git', ['diff', '--name-only'], stdioPipeOptions)
320374
).stdout
321-
const relPaths = changedFilesDetails.split('\n') ?? []
375+
const relPaths = changedFilesDetails.split('\n')
322376
return {
323377
ok: true,
324378
data: relPaths.map(p => normalizePath(p)),
325379
}
326380
} catch (e) {
327-
debugFn('error', 'caught: git diff --name-only failed')
381+
debugFn('error', `caught: ${quotedCmd} failed`)
328382
debugDir('inspect', { error: e })
329383
return {
330384
ok: false,

0 commit comments

Comments
 (0)