1- import { join , dirname } from 'path'
21import { existsSync } from 'fs'
2+ import { join , dirname } from 'path'
33import { fileURLToPath } from 'url'
44
55/**
@@ -16,7 +16,7 @@ export function getBundledRgPath(importMetaUrl?: string): string {
1616 // Determine platform-specific directory name
1717 const platform = process . platform
1818 const arch = process . arch
19-
19+
2020 let platformDir : string
2121 if ( platform === 'win32' && arch === 'x64' ) {
2222 platformDir = 'x64-win32'
@@ -33,47 +33,74 @@ export function getBundledRgPath(importMetaUrl?: string): string {
3333 }
3434
3535 const binaryName = platform === 'win32' ? 'rg.exe' : 'rg'
36-
36+
3737 // Try to find the bundled binary relative to this module
3838 let vendorPath : string | undefined
39-
39+
4040 if ( importMetaUrl ) {
4141 // ESM context - use import.meta.url to find relative path
4242 const currentFile = fileURLToPath ( importMetaUrl )
4343 const currentDir = dirname ( currentFile )
44-
44+
4545 // Try relative to current file (development - from src/native/ripgrep.ts to vendor/)
46- const devPath = join ( currentDir , '..' , '..' , 'vendor' , 'ripgrep' , platformDir , binaryName )
46+ const devPath = join (
47+ currentDir ,
48+ '..' ,
49+ '..' ,
50+ 'vendor' ,
51+ 'ripgrep' ,
52+ platformDir ,
53+ binaryName ,
54+ )
4755 if ( existsSync ( devPath ) ) {
4856 vendorPath = devPath
4957 }
5058 }
51-
59+
5260 // If not found via importMetaUrl, try CJS approach or other methods
5361 if ( ! vendorPath ) {
5462 // Try from __dirname if available (CJS context)
55- if ( typeof __dirname !== 'undefined' ) {
56- const cjsPath = join ( __dirname , '..' , '..' , 'vendor' , 'ripgrep' , platformDir , binaryName )
63+ const dirname = new Function ( 'return __dirname' ) ( )
64+ if ( typeof dirname !== 'undefined' ) {
65+ const cjsPath = join (
66+ dirname ,
67+ '..' ,
68+ '..' ,
69+ 'vendor' ,
70+ 'ripgrep' ,
71+ platformDir ,
72+ binaryName ,
73+ )
5774 if ( existsSync ( cjsPath ) ) {
5875 vendorPath = cjsPath
5976 }
6077 }
6178 }
62-
79+
6380 if ( vendorPath && existsSync ( vendorPath ) ) {
6481 return vendorPath
6582 }
66-
83+
6784 // Fallback: try to find in dist/vendor (for published package)
68- const distVendorPath = join ( process . cwd ( ) , 'node_modules' , '@codebuff' , 'sdk' , 'dist' , 'vendor' , 'ripgrep' , platformDir , binaryName )
85+ const distVendorPath = join (
86+ process . cwd ( ) ,
87+ 'node_modules' ,
88+ '@codebuff' ,
89+ 'sdk' ,
90+ 'dist' ,
91+ 'vendor' ,
92+ 'ripgrep' ,
93+ platformDir ,
94+ binaryName ,
95+ )
6996 if ( existsSync ( distVendorPath ) ) {
7097 return distVendorPath
7198 }
72-
99+
73100 // No fallback available - bundled binaries are required
74101 throw new Error (
75102 `Ripgrep binary not found for ${ platform } -${ arch } . ` +
76- `Expected at: ${ vendorPath } or ${ distVendorPath } . ` +
77- `Please run 'npm run fetch-ripgrep' or set CODEBUFF_RG_PATH environment variable.`
103+ `Expected at: ${ vendorPath } or ${ distVendorPath } . ` +
104+ `Please run 'npm run fetch-ripgrep' or set CODEBUFF_RG_PATH environment variable.` ,
78105 )
79106}
0 commit comments