|
1 | 1 | import { mkdirSync } from 'fs' |
2 | 2 | import path from 'path' |
3 | 3 |
|
| 4 | +import { rgPath as vscodeRgPath } from '@vscode/ripgrep' |
4 | 5 | import { spawnSync } from 'bun' |
5 | | -import { getBundledRgPath } from '@codebuff/sdk' |
6 | 6 |
|
7 | 7 | import { CONFIG_DIR } from '../credentials' |
8 | 8 | import { logger } from '../utils/logger' |
9 | 9 |
|
10 | 10 | const getRipgrepPath = async (): Promise<string> => { |
11 | | - let bundledRgPath: string |
12 | | - try { |
13 | | - bundledRgPath = getBundledRgPath() |
14 | | - } catch (error) { |
15 | | - logger.error({ error }, 'Failed to resolve bundled ripgrep path') |
16 | | - throw error |
17 | | - } |
18 | | - |
19 | | - // In dev mode, use the bundled path directly |
| 11 | + // In dev mode, use the vscode ripgrep binary |
20 | 12 | if (!process.env.IS_BINARY) { |
21 | | - return bundledRgPath |
| 13 | + return vscodeRgPath |
22 | 14 | } |
23 | 15 |
|
24 | | - // Compiled mode - stage the bundled binary in the config directory |
| 16 | + // Compiled mode - self-extract the embedded binary |
25 | 17 | const rgFileName = process.platform === 'win32' ? 'rg.exe' : 'rg' |
26 | 18 | const outPath = path.join(CONFIG_DIR, rgFileName) |
27 | 19 |
|
| 20 | + // Check if already extracted |
| 21 | + if (await Bun.file(outPath).exists()) { |
| 22 | + return outPath |
| 23 | + } |
| 24 | + |
| 25 | + // Extract the embedded binary |
28 | 26 | try { |
29 | | - if (await Bun.file(outPath).exists()) { |
30 | | - return outPath |
31 | | - } |
| 27 | + // Use require() on a static string path to make sure rg is included in the compiled binary |
| 28 | + const embeddedRgPath = |
| 29 | + process.platform === 'win32' |
| 30 | + ? require('../../../node_modules/@vscode/ripgrep/bin/rg.exe') |
| 31 | + : require('../../../node_modules/@vscode/ripgrep/bin/rg') |
32 | 32 |
|
| 33 | + // Create cache directory |
33 | 34 | mkdirSync(path.dirname(outPath), { recursive: true }) |
34 | | - await Bun.write(outPath, await Bun.file(bundledRgPath).arrayBuffer()) |
35 | 35 |
|
| 36 | + // Copy embedded binary to cache location |
| 37 | + await Bun.write(outPath, await Bun.file(embeddedRgPath).arrayBuffer()) |
| 38 | + |
| 39 | + // Make executable on Unix systems |
36 | 40 | if (process.platform !== 'win32') { |
37 | 41 | spawnSync(['chmod', '+x', outPath]) |
38 | 42 | } |
39 | 43 |
|
40 | 44 | return outPath |
41 | 45 | } catch (error) { |
42 | | - logger.error( |
43 | | - { error }, |
44 | | - 'Failed to stage bundled ripgrep binary, using fallback path', |
45 | | - ) |
46 | | - return bundledRgPath |
| 46 | + logger.error({ error }, 'Failed to extract ripgrep binary') |
| 47 | + // Fallback to vscode ripgrep if extraction fails |
| 48 | + return vscodeRgPath |
47 | 49 | } |
48 | 50 | } |
49 | 51 |
|
|
0 commit comments