Skip to content

Commit 421bd9f

Browse files
authored
fix: run Bazel Maven scans on Windows (#1658)
* fix: run Bazel Maven scans on Windows Generate Maven manifests with native Windows tools. Preserve paths and arguments, use the scan environment for prerequisite checks, and clean up servers started by the connector. Add pinned Windows fixtures and packaged CLI checks. Assisted-by: Codex:GPT-6 * fix: keep native Windows executable paths Use executable lookup before unwrapping Node launchers so native Bazel and Python executables remain runnable. Keep custom batch wrappers intact and invoke their selected paths. Pin the existing which version without changing resolutions. Assisted-by: Codex:GPT-6 * test: handle Windows executable path casing Compare executable identity so case-insensitive Windows paths do not fail when PATHEXT uses an uppercase extension. Assisted-by: Codex:GPT-6 * fix: only stop Bazel servers this scan started Ask Bazel for the effective output base before shutdown, so an rc file that selects a caller-owned output base keeps its server alive. Skip every socket-bazel- output directory during workspace discovery, and strip quotes from PATH entries before resolving them. Simplify the Windows process helpers. Use one environment key lookup, one batch escaper and one Python shim result shape, require explicit env and cwd, shut down servers in parallel, and cancel superseded Windows CI runs. Assisted-by: Claude Code:claude-opus-5-5 * fix: stop servers the scan started, keep inputs Shut down any Bazel server whose install or output base is in the scan's own root, including one started in an explicit output base, and always remove the root afterwards, even when Bazel cannot report its bases. A python on PATH that is not Python 3 falls back to python3. Quotes in Bazel flags only open at the start of a value or after =, so apostrophes stay literal. A relative --bazel resolves against the current directory again. The Windows Bazel workflow runs only for Bazel changes. Assisted-by: Claude Code:claude-opus-5-5 * fix: stop launcher trees and verify server exit A Bazel timeout or cancellation now kills every process a launcher started, through a process group on Unix and taskkill on Windows. If something still holds the output pipes, the call settles after a short grace period. Timeouts surface as BazelTimeoutError. Cleanup runs under its own time limit, so it still works after Ctrl-C aborts the scan. It reads the pid of each server the scan owns before shutdown, waits for that server to exit, and kills it if shutdown left it running. The output root is kept only while an owned server is still alive. Real Bazel tests cover timeout and cancellation behind a JavaScript launcher, and behind a batch launcher on Windows. Assisted-by: Claude Code:claude-opus-5-5 * fix: keep scans passing when pid files can't be read Bazel cleanup runs after a scan has produced its manifests. An unreadable server pid file made cleanup throw and failed the scan. Cleanup now warns, still shuts down the servers it knows about, and keeps the output root because it can't confirm the servers stopped. Assisted-by: Claude Code:claude-opus-5-5 * fix: report cancelled Bazel runs as aborted on Windows On Windows, stopping a cancelled Bazel run kills the launcher before the abort reaches the spawn, so the run failed with the launcher's exit code. It now fails with an AbortError like on other platforms. The escaped-process test also waits for its child to exit, because Windows keeps the temporary directory locked until then. Assisted-by: Claude Code:claude-opus-5-5
1 parent ea8ec86 commit 421bd9f

31 files changed

Lines changed: 2272 additions & 845 deletions
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
name: Bazel on Windows
2+
3+
on:
4+
pull_request:
5+
branches: [main, v1.x]
6+
paths:
7+
- .github/workflows/bazel-windows.yml
8+
- package.json
9+
- pnpm-lock.yaml
10+
- src/commands/manifest/bazel/**
11+
- test/fixtures/manifest-bazel/**
12+
push:
13+
branches: [main, v1.x]
14+
paths:
15+
- .github/workflows/bazel-windows.yml
16+
- package.json
17+
- pnpm-lock.yaml
18+
- src/commands/manifest/bazel/**
19+
- test/fixtures/manifest-bazel/**
20+
21+
permissions:
22+
contents: read
23+
24+
concurrency:
25+
group: bazel-windows-${{ github.ref }}
26+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
27+
28+
jobs:
29+
maven:
30+
name: Maven on Windows x64
31+
runs-on: windows-2022
32+
timeout-minutes: 45
33+
defaults:
34+
run:
35+
shell: pwsh
36+
steps:
37+
- name: Checkout source
38+
env:
39+
CHECKOUT_REF: ${{ github.sha }}
40+
GITHUB_TOKEN: ${{ github.token }}
41+
SERVER_URL: ${{ github.server_url }}
42+
REPOSITORY: ${{ github.repository }}
43+
run: |
44+
$PSNativeCommandUseErrorActionPreference = $true
45+
git init -q
46+
git config --local advice.detachedHead false
47+
git remote add origin "$env:SERVER_URL/$env:REPOSITORY"
48+
$auth = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("x-access-token:$env:GITHUB_TOKEN"))
49+
$env:GIT_CONFIG_COUNT = '1'
50+
$env:GIT_CONFIG_KEY_0 = "http.$env:SERVER_URL/.extraheader"
51+
$env:GIT_CONFIG_VALUE_0 = "AUTHORIZATION: basic $auth"
52+
git fetch --no-tags --depth=1 origin $env:CHECKOUT_REF
53+
git checkout -q --detach FETCH_HEAD
54+
55+
- name: Install pinned toolchains
56+
run: |
57+
$tools = Join-Path $env:RUNNER_TEMP 'bazel-tools'
58+
New-Item -ItemType Directory -Force "$tools/bin" | Out-Null
59+
function Get-VerifiedTool($url, $file, $sha256) {
60+
Invoke-WebRequest -Uri $url -OutFile $file
61+
if ((Get-FileHash $file -Algorithm SHA256).Hash.ToLowerInvariant() -ne $sha256) {
62+
throw "Checksum mismatch for $file"
63+
}
64+
}
65+
Get-VerifiedTool 'https://nodejs.org/dist/v24.14.1/node-v24.14.1-win-x64.zip' "$tools/node.zip" '6e50ce5498c0cebc20fd39ab3ff5df836ed2f8a31aa093cecad8497cff126d70'
66+
Get-VerifiedTool 'https://api.nuget.org/v3-flatcontainer/python/3.12.10/python.3.12.10.nupkg' "$tools/python.zip" '0eb85c2dfccccf1b17352de4c397f69194035b7d37149eacc16f1147d93de3b8'
67+
Get-VerifiedTool 'https://github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.6%2B7/OpenJDK21U-jdk_x64_windows_hotspot_21.0.6_7.zip' "$tools/java.zip" '897c8eebb0f85a99ccecbd482ebae9a45d88c19d6077054f6529ebab49b6d259'
68+
Get-VerifiedTool 'https://github.com/bazelbuild/bazelisk/releases/download/v1.26.0/bazelisk-windows-amd64.exe' "$tools/bin/bazelisk.exe" '023734f33ed6b9c6d65468fe20bb2c5fb32473ccb8aca2fc5bf1521e61ce1622'
69+
Get-VerifiedTool 'https://github.com/pnpm/pnpm/releases/download/v10.33.0/pnpm-win-x64.exe' "$tools/bin/pnpm.exe" 'afc96009dc39fe23a835d65192049e6a995f342496b175585dc2beda7d42d33f'
70+
Expand-Archive "$tools/node.zip" "$tools/node"
71+
Expand-Archive "$tools/python.zip" "$tools/python"
72+
Expand-Archive "$tools/java.zip" "$tools/java"
73+
"$tools/node/node-v24.14.1-win-x64" | Add-Content $env:GITHUB_PATH
74+
"$tools/python/tools" | Add-Content $env:GITHUB_PATH
75+
"$tools/java/jdk-21.0.6+7/bin" | Add-Content $env:GITHUB_PATH
76+
"$tools/bin" | Add-Content $env:GITHUB_PATH
77+
"JAVA_HOME=$tools/java/jdk-21.0.6+7" | Add-Content $env:GITHUB_ENV
78+
'USE_BAZEL_VERSION=7.4.1' | Add-Content $env:GITHUB_ENV
79+
'SOCKET_BAZEL_OUTPUT_ROOT=C:/sb' | Add-Content $env:GITHUB_ENV
80+
New-Item -ItemType Directory -Force 'C:/sb' | Out-Null
81+
82+
- name: Record versions and verify prerequisites
83+
run: |
84+
$PSNativeCommandUseErrorActionPreference = $true
85+
git rev-parse HEAD
86+
node --version
87+
pnpm --version
88+
bazelisk --version
89+
python --version
90+
java -version
91+
Write-Output "PowerShell $($PSVersionTable.PSVersion)"
92+
Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture
93+
Get-Command node, bazelisk, python, java | Select-Object Name, Source
94+
95+
- name: Install dependencies and build
96+
run: |
97+
$PSNativeCommandUseErrorActionPreference = $true
98+
pnpm install --frozen-lockfile
99+
pnpm build
100+
101+
- name: Test executable handling and prerequisites
102+
run: |
103+
$PSNativeCommandUseErrorActionPreference = $true
104+
pnpm test:unit src/commands/manifest/bazel/bazel-bin-detect.test.mts src/commands/manifest/bazel/bazel-process.test.mts src/commands/manifest/bazel/bazel-java-shim.test.mts src/commands/manifest/bazel/bazel-python-shim.test.mts src/commands/manifest/bazel/bazel-query-runner.test.mts src/commands/manifest/bazel/bazel-cquery.test.mts
105+
106+
- name: Install the packaged CLI
107+
run: |
108+
$PSNativeCommandUseErrorActionPreference = $true
109+
$packages = Join-Path $env:RUNNER_TEMP 'bazel-packages'
110+
$installation = Join-Path $env:RUNNER_TEMP 'bazel-installation'
111+
pnpm pack --pack-destination $packages
112+
$tarball = Get-ChildItem "$packages/*.tgz" | Select-Object -First 1
113+
npm install --ignore-scripts --no-audit --no-fund --prefix $installation $tarball.FullName
114+
"SOCKET_BAZEL_TEST_CLI=$installation/node_modules/socket/bin/cli.js" | Add-Content $env:GITHUB_ENV
115+
116+
- name: Verify Maven manifests and cleanup
117+
env:
118+
SOCKET_BAZEL_INTEGRATION: '1'
119+
run: |
120+
$PSNativeCommandUseErrorActionPreference = $true
121+
pnpm exec vitest run --config vitest.e2e.config.mts src/commands/manifest/bazel/bazel-native.e2e.test.mts

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
66

77
## [Unreleased]
88

9+
### Added
10+
- Set `SOCKET_BAZEL_OUTPUT_ROOT` to choose a short parent directory for temporary Bazel output, including on Windows.
11+
912
### Changed
1013
- `--dynamic-sbom-inference` now builds every Maven POM a scan uploads, not only `pom.xml`: `<artifactId>-<version>.pom` and `.pom.xml` files, a bare `pom`, and names in any case, each as a build of its own. Like the upload, it skips POMs matched by `.gitignore` or by `projectIgnorePaths` in `socket.yml`.
1114

15+
### Fixed
16+
- Bazel Maven extraction handles native Windows executables, batch launchers, and JavaScript entry points while preserving arguments and the selected executable path.
17+
- Bazel prerequisite checks use the extraction environment and verify Python 3. Windows requires a working `python.exe` without creating a Python symlink.
18+
- Quoted Bazel flags preserve spaces and Windows backslashes. Quotes open at the start of a value or after `=`, so apostrophes inside a value stay literal. Unclosed quotes produce an error.
19+
- Bazel cleanup uses the extraction workspace, environment, and startup options, retries temporary file locks, and only stops Bazel servers the scan started.
20+
- Bazel timeouts and cancellation stop every process a Bazel launcher started. Cleanup still runs after Ctrl-C and keeps temporary output until the scan's own Bazel server has exited.
21+
- A relative `--bazel` path resolves against the current directory. A relative `bazel` path in `socket.json` resolves against the scanned directory.
22+
1223
## [1.11.0](https://github.com/SocketDev/socket-cli/releases/tag/v1.11.0) - 2026-10-26
1324

1425
### Added

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
"typescript-eslint": "8.43.0",
199199
"unplugin-purge-polyfills": "0.1.0",
200200
"vitest": "3.2.4",
201+
"which": "=5.0.0",
201202
"yaml": "2.8.1",
202203
"yargs-parser": "21.1.1",
203204
"yoctocolors-cjs": "2.1.3",

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/commands/manifest/bazel/bazel-bin-detect.mts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { existsSync } from 'node:fs'
2+
import path from 'node:path'
23

3-
import { whichBin } from '@socketsecurity/registry/lib/bin'
4-
4+
import {
5+
findBazelExecutable,
6+
resolveBazelExecutable,
7+
} from './bazel-process.mts'
58
import { InputError } from '../../../utils/errors.mts'
69

710
/**
@@ -15,21 +18,24 @@ import { InputError } from '../../../utils/errors.mts'
1518
*/
1619
export async function resolveBazelBinary(
1720
explicit: string | undefined,
21+
env: NodeJS.ProcessEnv,
22+
cwd: string,
1823
): Promise<string> {
1924
if (explicit) {
20-
if (!existsSync(explicit)) {
25+
const resolved = path.resolve(cwd, explicit)
26+
if (!existsSync(resolved)) {
2127
throw new InputError(
22-
`--bazel path does not exist: ${explicit}. Install bazelisk or bazel, or pass an existing path via --bazel.`,
28+
`--bazel path does not exist: ${resolved}. Install bazelisk or bazel, or pass an existing path via --bazel.`,
2329
)
2430
}
25-
return explicit
31+
return resolveBazelExecutable(resolved)
2632
}
2733
// Prefer bazelisk: respects .bazelversion in the workspace.
28-
const bazelisk = await whichBin('bazelisk', { nothrow: true })
34+
const bazelisk = await findBazelExecutable('bazelisk', env, cwd)
2935
if (bazelisk) {
3036
return bazelisk
3137
}
32-
const bazel = await whichBin('bazel', { nothrow: true })
38+
const bazel = await findBazelExecutable('bazel', env, cwd)
3339
if (bazel) {
3440
return bazel
3541
}
Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,95 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest'
1+
import {
2+
copyFileSync,
3+
mkdtempSync,
4+
realpathSync,
5+
rmSync,
6+
statSync,
7+
} from 'node:fs'
8+
import os from 'node:os'
9+
import path from 'node:path'
210

3-
// Mock whichBin so tests run with no bazel on PATH.
4-
vi.mock('@socketsecurity/registry/lib/bin', () => ({
5-
whichBin: vi.fn(),
6-
}))
7-
8-
import { whichBin } from '@socketsecurity/registry/lib/bin'
11+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
912

1013
import { resolveBazelBinary } from './bazel-bin-detect.mts'
14+
import { getBazelEnvironment } from './bazel-process.mts'
1115

1216
describe('resolveBazelBinary', () => {
13-
const mocked = vi.mocked(whichBin)
17+
let cwd: string
1418

1519
beforeEach(() => {
16-
mocked.mockReset()
20+
cwd = realpathSync(mkdtempSync(path.join(os.tmpdir(), 'bazel binaries ')))
1721
})
1822

19-
it('returns explicit path when it exists', async () => {
20-
// Use a path that definitely exists on every dev machine.
21-
const existing = process.execPath
22-
await expect(resolveBazelBinary(existing)).resolves.toBe(existing)
23+
afterEach(() => {
24+
rmSync(cwd, { recursive: true, force: true, maxRetries: 5 })
2325
})
2426

25-
it('throws InputError when explicit path does not exist', async () => {
26-
await expect(
27-
resolveBazelBinary('/no/such/bazel/binary/xyz'),
28-
).rejects.toThrow(/--bazel path does not exist/)
27+
function install(name: string): string {
28+
const bin = path.join(
29+
cwd,
30+
`${name}${process.platform === 'win32' ? '.exe' : ''}`,
31+
)
32+
copyFileSync(process.execPath, bin)
33+
return bin
34+
}
35+
36+
it('resolves an explicit path relative to the scan directory', async () => {
37+
const bin = install('bazel')
38+
const resolved = await resolveBazelBinary(
39+
path.basename(bin),
40+
getBazelEnvironment(),
41+
cwd,
42+
)
43+
expect(statSync(resolved).ino).toBe(statSync(bin).ino)
44+
})
45+
46+
it('preserves an explicit executable outside PATH', async () => {
47+
const bin = install('selected')
48+
const resolved = await resolveBazelBinary(
49+
bin,
50+
getBazelEnvironment({ PATH: '' }),
51+
cwd,
52+
)
53+
expect(statSync(resolved).ino).toBe(statSync(bin).ino)
2954
})
3055

31-
it('returns bazelisk when on PATH', async () => {
32-
mocked.mockResolvedValueOnce('/usr/local/bin/bazelisk')
33-
await expect(resolveBazelBinary(undefined)).resolves.toBe(
34-
'/usr/local/bin/bazelisk',
56+
it('prefers Bazelisk over Bazel', async () => {
57+
const bazelisk = install('bazelisk')
58+
install('bazel')
59+
const resolved = await resolveBazelBinary(
60+
undefined,
61+
getBazelEnvironment({ PATH: cwd }),
62+
cwd,
3563
)
64+
expect(statSync(resolved).ino).toBe(statSync(bazelisk).ino)
3665
})
3766

38-
it('falls back to bazel when bazelisk is missing', async () => {
39-
mocked
40-
.mockResolvedValueOnce(null)
41-
.mockResolvedValueOnce('/usr/local/bin/bazel')
42-
await expect(resolveBazelBinary(undefined)).resolves.toBe(
43-
'/usr/local/bin/bazel',
67+
it('falls back to Bazel when Bazelisk is absent', async () => {
68+
const bazel = install('bazel')
69+
const resolved = await resolveBazelBinary(
70+
undefined,
71+
getBazelEnvironment({ PATH: cwd }),
72+
cwd,
4473
)
74+
expect(statSync(resolved).ino).toBe(statSync(bazel).ino)
4575
})
4676

47-
it('throws InputError when neither is on PATH', async () => {
48-
mocked.mockResolvedValue(null)
49-
await expect(resolveBazelBinary(undefined)).rejects.toThrow(
50-
/Could not find bazelisk or bazel/,
77+
it('finds tools in a quoted PATH entry', async () => {
78+
const bazel = install('bazel')
79+
const resolved = await resolveBazelBinary(
80+
undefined,
81+
getBazelEnvironment({ PATH: `"${cwd}"` }),
82+
os.tmpdir(),
5183
)
84+
expect(statSync(resolved).ino).toBe(statSync(bazel).ino)
85+
})
86+
87+
it('reports missing tools', async () => {
88+
await expect(
89+
resolveBazelBinary(undefined, getBazelEnvironment({ PATH: cwd }), cwd),
90+
).rejects.toThrow(/Could not find bazelisk or bazel/)
91+
await expect(
92+
resolveBazelBinary(path.join(cwd, 'missing'), getBazelEnvironment(), cwd),
93+
).rejects.toThrow(/--bazel path does not exist/)
5294
})
5395
})

0 commit comments

Comments
 (0)