Skip to content

Commit 3f255a7

Browse files
committed
fetch __dirname dynamically
1 parent d7f4e67 commit 3f255a7

File tree

5 files changed

+62
-28
lines changed

5 files changed

+62
-28
lines changed

packages/code-map/src/init-node.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import * as path from 'path'
21
import * as fs from 'fs'
3-
import { Parser } from 'web-tree-sitter'
2+
import * as path from 'path'
43
import { fileURLToPath } from 'url'
54

5+
import { Parser } from 'web-tree-sitter'
6+
7+
import { getDirnameDynamically } from './utils'
8+
69
/**
710
* Helper function to get the current directory path that works in both ESM and CJS
8-
* Uses dynamic evaluation to prevent bundlers from inlining absolute paths
11+
* Uses runtime-only approach to prevent bundlers from inlining absolute paths
912
*/
1013
function hereDir() {
11-
// In CJS, __dirname is available - use indirect eval to prevent inlining
12-
if (typeof __dirname !== 'undefined') {
13-
// Use a trick to get __dirname dynamically without bundler inlining it
14-
// This works because bundlers won't inline values from Function constructor
15-
return new Function('return __dirname')()
14+
const dirname = getDirnameDynamically()
15+
if (typeof dirname !== 'undefined') {
16+
return dirname
1617
}
1718

1819
// For ESM builds, use import.meta.url

packages/code-map/src/languages.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import * as path from 'path'
21
import * as fs from 'fs'
2+
import * as path from 'path'
33

44
// Import some types for wasm & .scm files
55
import './types'
66

77
import { Language, Parser, Query } from 'web-tree-sitter'
8+
9+
import { initTreeSitterForNode } from './init-node'
810
import { DEBUG_PARSING } from './parse'
911

1012
/* ------------------------------------------------------------------ */
@@ -19,6 +21,7 @@ import pythonQuery from './tree-sitter-queries/tree-sitter-python-tags.scm'
1921
import rubyQuery from './tree-sitter-queries/tree-sitter-ruby-tags.scm'
2022
import rustQuery from './tree-sitter-queries/tree-sitter-rust-tags.scm'
2123
import typescriptQuery from './tree-sitter-queries/tree-sitter-typescript-tags.scm'
24+
import { getDirnameDynamically } from './utils'
2225

2326
/* ------------------------------------------------------------------ */
2427
/* 2. Types and interfaces */
@@ -150,8 +153,9 @@ function resolveWasmPath(wasmFileName: string): string {
150153

151154
// Get the directory of this module
152155
const moduleDir = (() => {
153-
if (typeof __dirname !== 'undefined') {
154-
return __dirname
156+
const dirname = getDirnameDynamically()
157+
if (typeof dirname !== 'undefined') {
158+
return dirname
155159
}
156160
// For ESM builds, we can't reliably get the module directory in all environments
157161
// So we fall back to process.cwd() which works for our use case
@@ -199,7 +203,6 @@ function tryResolveFromPackage(wasmFileName: string): string | null {
199203
/* 7. One-time library init */
200204
/* ------------------------------------------------------------------ */
201205
// Initialize tree-sitter with Node.js-specific configuration
202-
import { initTreeSitterForNode } from './init-node'
203206

204207
/* ------------------------------------------------------------------ */
205208
/* 8. Unified runtime loader */

packages/code-map/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getDirnameDynamically(): string {
2+
return new Function('return __dirname')()
3+
}

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@codebuff/sdk",
33
"private": false,
4-
"version": "0.3.8",
4+
"version": "0.3.9",
55
"description": "Official SDK for Codebuff — AI coding agent & framework",
66
"license": "Apache-2.0",
77
"type": "module",

sdk/src/native/ripgrep.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { join, dirname } from 'path'
21
import { existsSync } from 'fs'
2+
import { join, dirname } from 'path'
33
import { 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

Comments
 (0)