Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getStatsFromTree, processTraceFiles, showTree, treeIdNodes } from './tr
import { getTracePanel, prepareWebView } from './webview'
import { getCurrentConfig } from './configuration'
import { log } from './logger'
import { createTraceCommand } from './shell'
import type { CommandId } from './constants'
import { addTraceFile, getWorkspacePath, openTerminal, openTraceDirectoryExternal, setLastMessageTrigger } from './storage'
import { addTraceDiagnostics, clearTaceDiagnostics } from './traceDiagnostics'
Expand Down Expand Up @@ -128,9 +129,7 @@ async function runTrace(args?: unknown[]) {
return
}

const quotedTraceDir = `'${traceDir}'`
// eslint-disable-next-line no-template-curly-in-string
const fullCmd = `(cd '${newDirName ?? workspacePath}'; ${traceCmd.replace('${traceDir}', quotedTraceDir)})`
const fullCmd = createTraceCommand(traceCmd, traceDir)

log(fullCmd)

Expand All @@ -147,8 +146,9 @@ async function runTrace(args?: unknown[]) {

setStatusBarState('traceError', false)

log(`shell: ${process.env.SHELL}`)
const cmdProcess = spawn(fullCmd, [], { cwd: newProjectPath, shell: process.env.SHELL })
log(`cwd: ${newProjectPath}`)
log(`shell: ${process.env.SHELL ?? 'default'}`)
const cmdProcess = spawn(fullCmd, [], { cwd: newProjectPath, shell: process.env.SHELL || true })

let err = ''
cmdProcess.stderr.on('data', data => err += data.toString())
Expand Down
15 changes: 15 additions & 0 deletions src/shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as process from 'node:process'

export function quoteShellArg(value: string, platform: NodeJS.Platform = process.platform) {
if (platform === 'win32') {
return `"${value.replace(/"/g, '\\"')}"`
}

return `'${value.replace(/'/g, `'\\''`)}'`
}

export function createTraceCommand(traceCmd: string, traceDir: string, platform: NodeJS.Platform = process.platform) {
const traceDirPlaceholder = '$' + '{traceDir}'

return traceCmd.replace(traceDirPlaceholder, quoteShellArg(traceDir, platform))
}
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { describe, expect, it } from 'vitest'
import { createTraceCommand, quoteShellArg } from '../src/shell'

describe('should', () => {
it('exported', () => {
expect(1).toEqual(1)
})
})

describe('shell command helpers', () => {
it('quotes trace directories for posix shells', () => {
expect(quoteShellArg('/tmp/a b/trace', 'darwin')).toBe('\'/tmp/a b/trace\'')
expect(quoteShellArg('/tmp/it\'s/trace', 'linux')).toBe('\'/tmp/it\'\\\'\'s/trace\'')
})

it('quotes trace directories for windows shells', () => {
expect(quoteShellArg('c:\\Users\\Name With Space\\trace', 'win32')).toBe('"c:\\Users\\Name With Space\\trace"')
})

it('builds the trace command without embedding a cd command', () => {
const traceDirPlaceholder = '$' + '{traceDir}'
const command = createTraceCommand(`npx tsc --noEmit --generateTrace ${traceDirPlaceholder}`, 'p:\\_godot\\trace', 'win32')

expect(command).toBe('npx tsc --noEmit --generateTrace "p:\\_godot\\trace"')
expect(command).not.toContain('cd ')
})
})