Skip to content

Commit dadc736

Browse files
committed
fix(ripgrep): use npm_config_arch for platform-package resolution
1 parent 5e9ec35 commit dadc736

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

src/services/ripgrep/__tests__/index.spec.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ describe("getBinPath", () => {
112112
expect(await getBinPath(appRoot)).toBeUndefined()
113113
})
114114

115+
it("returns undefined when platform-package resolution fails", async () => {
116+
mockFileExists.mockResolvedValue(false)
117+
mockExistsSync.mockReturnValue(true)
118+
mockCreateRequire.mockReturnValue({
119+
resolve: vi.fn(() => {
120+
throw new Error("module not found")
121+
}),
122+
} as unknown as NodeRequire)
123+
124+
await expect(getBinPath(appRoot)).resolves.toBeUndefined()
125+
})
126+
115127
// Regression test for https://github.com/Zoo-Code-Org/Zoo-Code/issues/1024
116128
// VS Code 1.130+ ships @vscode/ripgrep >=1.18, where the binary lives in a
117129
// platform-specific optional package resolved via the wrapper's package.json.
@@ -120,20 +132,44 @@ describe("getBinPath", () => {
120132
it("resolves ripgrep via the @vscode/ripgrep >=1.18 platform-package layout", async () => {
121133
const platformBin = `/vscode/ripgrep-${process.platform}-${process.arch}/bin/${binName}`
122134

123-
// All static candidates miss.
124135
mockFileExists.mockResolvedValue(false)
125-
126-
// Simulate @vscode/ripgrep wrapper manifest existing and resolving to the platform bin.
127136
mockExistsSync.mockReturnValue(true)
128137
const mockRequireFromWrapper = { resolve: vi.fn().mockReturnValue(platformBin) }
129138
const mockRequireFromApp = { resolve: vi.fn().mockReturnValue("/vscode/ripgrep/index.js") }
130139
mockCreateRequire
131140
.mockReturnValueOnce(mockRequireFromApp as unknown as NodeRequire)
132141
.mockReturnValueOnce(mockRequireFromWrapper as unknown as NodeRequire)
133-
134-
// The resolved platform bin exists on disk.
135142
mockFileExists.mockImplementation(async (p: string) => p === platformBin)
136143

137144
expect(await getBinPath(appRoot)).toBe(platformBin)
138145
})
146+
147+
it("respects npm_config_arch override when resolving platform-package", async () => {
148+
const overrideArch = "x64"
149+
const platformBin = `/vscode/ripgrep-${process.platform}-${overrideArch}/bin/${binName}`
150+
151+
const original = process.env.npm_config_arch
152+
process.env.npm_config_arch = overrideArch
153+
try {
154+
mockFileExists.mockResolvedValue(false)
155+
mockExistsSync.mockReturnValue(true)
156+
const mockRequireFromWrapper = { resolve: vi.fn().mockReturnValue(platformBin) }
157+
const mockRequireFromApp = { resolve: vi.fn().mockReturnValue("/vscode/ripgrep/index.js") }
158+
mockCreateRequire
159+
.mockReturnValueOnce(mockRequireFromApp as unknown as NodeRequire)
160+
.mockReturnValueOnce(mockRequireFromWrapper as unknown as NodeRequire)
161+
mockFileExists.mockImplementation(async (p: string) => p === platformBin)
162+
163+
expect(await getBinPath(appRoot)).toBe(platformBin)
164+
expect(mockRequireFromWrapper.resolve).toHaveBeenCalledWith(
165+
`@vscode/ripgrep-${process.platform}-${overrideArch}/bin/${binName}`,
166+
)
167+
} finally {
168+
if (original === undefined) {
169+
delete process.env.npm_config_arch
170+
} else {
171+
process.env.npm_config_arch = original
172+
}
173+
}
174+
})
139175
})

src/services/ripgrep/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ export function resolvePlatformRipgrepPath(vscodeAppRoot: string): string | unde
118118
const requireFromApp = createRequire(path.join(vscodeAppRoot, "package.json"))
119119
const wrapperEntry = requireFromApp.resolve("@vscode/ripgrep")
120120
const requireFromWrapper = createRequire(wrapperEntry)
121-
return requireFromWrapper.resolve(`@vscode/ripgrep-${process.platform}-${process.arch}/bin/${binName}`)
121+
const arch = process.env.npm_config_arch || process.arch
122+
return requireFromWrapper.resolve(`@vscode/ripgrep-${process.platform}-${arch}/bin/${binName}`)
122123
} catch {
123124
return undefined
124125
}

0 commit comments

Comments
 (0)