Skip to content

Commit 4cc885d

Browse files
jfblaaclaude
andauthored
feat(scan): scan Bazel targets with --bazel (#1788)
* feat(scan): scan Bazel targets with --bazel Scans only the Bazel targets the patterns after `--` name, from the workspace root, in place of manifest discovery. Coana resolves the patterns as `bazel build` takes them and generates the Socket facts; reachability analyses the JVM, Python and JavaScript targets. Facts generation passes Coana the run's `--purpose`, as every other build does, and a reachability run compiles the targets. * fix(scan): keep the Bazel workspace root inside the working directory Refuse a --bazel workspace root outside the working directory, as --reach does for its target, and point at --cwd. The repeat-command hint now names the org with --org and includes --bazel. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(changelog): drop the Bazel path entry --bazel is hidden, so the release notes leave it out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * style(scan): sort the scan options after the v1.x merge Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8c7a4e7 commit 4cc885d

11 files changed

Lines changed: 623 additions & 90 deletions

File tree

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { InputError } from '../../../utils/errors.mts'
1818
* Resolve the bazel binary to invoke for `socket manifest bazel`.
1919
*
2020
* Resolution order:
21-
* 1. If `explicit` is provided, return it iff it exists on disk; else throw.
21+
* 1. SOCKET_CLI_BAZEL_PATH, else `explicit`: return it iff it exists on disk;
22+
* else throw.
2223
* 2. Look up `bazelisk` on PATH (preferred — respects `.bazelversion`).
2324
* 3. Fall back to `bazel` on PATH.
2425
* 4. If neither is found, throw InputError with install instructions.
@@ -27,12 +28,18 @@ export async function resolveBazelBinary(
2728
explicit: string | undefined,
2829
env: NodeJS.ProcessEnv,
2930
cwd: string,
31+
// The option that names `explicit`; null for a command with none.
32+
explicitFlag: string | null = '--bazel',
3033
): Promise<string> {
34+
// As the other build tools take theirs from SOCKET_CLI_<TOOL>_PATH, ahead of
35+
// a named binary.
36+
const orFlag = explicitFlag ? ` or pass ${explicitFlag}` : ''
37+
const fromEnv = env['SOCKET_CLI_BAZEL_PATH']
38+
const override = fromEnv || explicit
3139
if (isSea()) {
32-
const override = explicit ?? env['SOCKET_CLI_BAZEL_PATH']
3340
if (!override) {
3441
throw new InputError(
35-
'Bazel is not bundled with this standalone Socket CLI. Set SOCKET_CLI_BAZEL_PATH or pass --bazel with an installed native Bazel executable.',
42+
`Bazel is not bundled with this standalone Socket CLI. Set SOCKET_CLI_BAZEL_PATH${orFlag} with an installed native Bazel executable.`,
3643
)
3744
}
3845
const resolved = path.resolve(cwd, override)
@@ -61,11 +68,13 @@ export async function resolveBazelBinary(
6168
}
6269
return bin
6370
}
64-
if (explicit) {
65-
const resolved = path.resolve(cwd, explicit)
71+
if (override) {
72+
const named =
73+
fromEnv || !explicitFlag ? 'SOCKET_CLI_BAZEL_PATH' : explicitFlag
74+
const resolved = path.resolve(cwd, override)
6675
if (!existsSync(resolved)) {
6776
throw new InputError(
68-
`--bazel path does not exist: ${resolved}. Install bazelisk or bazel, or pass an existing path via --bazel.`,
77+
`${named} path does not exist: ${resolved}. Install bazelisk or bazel, or name an existing path with ${named}.`,
6978
)
7079
}
7180
return resolveBazelExecutable(resolved)
@@ -82,6 +91,6 @@ export async function resolveBazelBinary(
8291
throw new InputError(
8392
'Could not find bazelisk or bazel on PATH. ' +
8493
'Install bazelisk (recommended; https://github.com/bazelbuild/bazelisk) ' +
85-
'or bazel, or pass --bazel <path>.',
94+
`or bazel, or name its path with ${explicitFlag ? `${explicitFlag} or ` : ''}SOCKET_CLI_BAZEL_PATH.`,
8695
)
8796
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ describe('resolveBazelBinary', () => {
5353
expect(statSync(resolved).ino).toBe(statSync(bin).ino)
5454
})
5555

56+
it('takes SOCKET_CLI_BAZEL_PATH ahead of an explicit path', async () => {
57+
const fromEnv = install('from-env')
58+
const resolved = await resolveBazelBinary(
59+
install('selected'),
60+
getBazelEnvironment({ SOCKET_CLI_BAZEL_PATH: fromEnv }),
61+
cwd,
62+
)
63+
expect(statSync(resolved).ino).toBe(statSync(fromEnv).ino)
64+
})
65+
5666
it('prefers Bazelisk over Bazel', async () => {
5767
const bazelisk = install('bazelisk')
5868
install('bazel')
@@ -91,5 +101,25 @@ describe('resolveBazelBinary', () => {
91101
await expect(
92102
resolveBazelBinary(path.join(cwd, 'missing'), getBazelEnvironment(), cwd),
93103
).rejects.toThrow(/--bazel path does not exist/)
104+
await expect(
105+
resolveBazelBinary(
106+
undefined,
107+
getBazelEnvironment({
108+
SOCKET_CLI_BAZEL_PATH: path.join(cwd, 'missing'),
109+
}),
110+
cwd,
111+
),
112+
).rejects.toThrow(/SOCKET_CLI_BAZEL_PATH path does not exist/)
113+
})
114+
115+
it('names only SOCKET_CLI_BAZEL_PATH for a command with no option naming Bazel', async () => {
116+
const missing = resolveBazelBinary(
117+
undefined,
118+
getBazelEnvironment({ PATH: cwd }),
119+
cwd,
120+
null,
121+
)
122+
await expect(missing).rejects.toThrow(/with SOCKET_CLI_BAZEL_PATH\.$/)
123+
await expect(missing).rejects.not.toThrow(/--bazel/)
94124
})
95125
})

‎src/commands/manifest/generate_auto_manifest.mts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
socketFactsRunOptions,
88
} from './generate-recursive-manifests.mts'
99
import { handleManifestConda } from './handle-manifest-conda.mts'
10-
import { runManifestFacts } from './run-manifest-facts.mts'
10+
import { AUTO_MANIFEST_HINTS, runManifestFacts } from './run-manifest-facts.mts'
1111
import {
1212
ENVIRONMENT_YML,
1313
REQUIREMENTS_TXT,
@@ -27,12 +27,6 @@ import type {
2727
import type { BuildTool } from './scripts/build-tool.mts'
2828
import type { OutputKind } from '../../types.mts'
2929

30-
// Auto-manifest reads every build option from the root socket.json.
31-
const AUTO_MANIFEST_HINTS: Omit<BuildOptionHints, keyof CommandHints> = {
32-
setupCommand: 'socket manifest setup',
33-
socketJson: SOCKET_JSON,
34-
}
35-
3630
export type GenerateAutoManifestResult = {
3731
generatedFiles: string[]
3832
}

0 commit comments

Comments
 (0)