Commit 06097fd7 authored by Benjamin Woodruff's avatar Benjamin Woodruff

fix(dev-overlay): Better handle edge-case file paths in launchEditor (#79526)

Without this, some files may fail to open correctly.

Internal discussion:
https://vercel.slack.com/archives/C07UWUAF95Z/p1747261772629689
parent bda731fa
import { escapeApplescriptStringFragment } from './launch-editor'
describe('applescript string escaping', () => {
it('should escape strings correctly', () => {
const result = escapeApplescriptStringFragment(`abc\\def"ghi\\\\`)
expect(result).toBe(`abc\\\\def\\"ghi\\\\\\\\`)
})
})
...@@ -303,7 +303,22 @@ function printInstructions(fileName: string, errorMessage: string | null) { ...@@ -303,7 +303,22 @@ function printInstructions(fileName: string, errorMessage: string | null) {
console.log() console.log()
} }
function launchEditor(fileName: string, lineNumber: number, colNumber: number) { export function escapeApplescriptStringFragment(input: string): string {
// The only two special characters in a quoted applescript string are
// backslash and double quote. Both are escaped with a preceeding backslash.
//
// Some whitespace characters (like newlines) can be escaped (as `\n`), but
// aren't required to be escaped (so we're not bothering to do that).
//
// https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-BBCIAHJF:~:text=Special%20String%20Characters
return input.replaceAll(/[\\"]/g, (original) => `\\${original}`)
}
export function launchEditor(
fileName: string,
lineNumber: number,
colNumber: number
) {
if (!fs.existsSync(fileName)) { if (!fs.existsSync(fileName)) {
return return
} }
...@@ -387,15 +402,12 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) { ...@@ -387,15 +402,12 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
}) })
} else if (isTerminalEditor(editor)) { } else if (isTerminalEditor(editor)) {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
const escapedScript = escapeApplescriptStringFragment(
shellQuote.quote([editor, ...args])
)
p = child_process.spawn( p = child_process.spawn(
'osascript', 'osascript',
[ ['-e', `tell application "Terminal" to do script "${escapedScript}"`],
'-e',
`tell application "Terminal" to do script "${shellQuote.quote([
editor,
...args,
])}"`,
],
{ stdio: 'ignore' } { stdio: 'ignore' }
) )
} else { } else {
...@@ -416,5 +428,3 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) { ...@@ -416,5 +428,3 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
}) })
} }
} }
export { launchEditor }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment