Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/commands/manifest/bazel/bin-detect.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { InputError } from '../../../utils/errors.mts'
* Resolve the bazel binary to invoke for `socket manifest bazel`.
*
* Resolution order:
* 1. If `explicit` is provided, return it iff it exists on disk; else throw.
* 1. SOCKET_CLI_BAZEL_PATH, else `explicit`: return it iff it exists on disk;
* else throw.
* 2. Look up `bazelisk` on PATH (preferred — respects `.bazelversion`).
* 3. Fall back to `bazel` on PATH.
* 4. If neither is found, throw InputError with install instructions.
Expand All @@ -27,12 +28,18 @@ export async function resolveBazelBinary(
explicit: string | undefined,
env: NodeJS.ProcessEnv,
cwd: string,
// The option that names `explicit`; null for a command with none.
explicitFlag: string | null = '--bazel',
): Promise<string> {
// As the other build tools take theirs from SOCKET_CLI_<TOOL>_PATH, ahead of
// a named binary.
const orFlag = explicitFlag ? ` or pass ${explicitFlag}` : ''
const fromEnv = env['SOCKET_CLI_BAZEL_PATH']
const override = fromEnv || explicit
if (isSea()) {
const override = explicit ?? env['SOCKET_CLI_BAZEL_PATH']
if (!override) {
throw new InputError(
'Bazel is not bundled with this standalone Socket CLI. Set SOCKET_CLI_BAZEL_PATH or pass --bazel with an installed native Bazel executable.',
`Bazel is not bundled with this standalone Socket CLI. Set SOCKET_CLI_BAZEL_PATH${orFlag} with an installed native Bazel executable.`,
)
}
const resolved = path.resolve(cwd, override)
Expand Down Expand Up @@ -61,11 +68,13 @@ export async function resolveBazelBinary(
}
return bin
}
if (explicit) {
const resolved = path.resolve(cwd, explicit)
if (override) {
const named =
fromEnv || !explicitFlag ? 'SOCKET_CLI_BAZEL_PATH' : explicitFlag
const resolved = path.resolve(cwd, override)
if (!existsSync(resolved)) {
throw new InputError(
`--bazel path does not exist: ${resolved}. Install bazelisk or bazel, or pass an existing path via --bazel.`,
`${named} path does not exist: ${resolved}. Install bazelisk or bazel, or name an existing path with ${named}.`,
)
}
return resolveBazelExecutable(resolved)
Expand All @@ -82,6 +91,6 @@ export async function resolveBazelBinary(
throw new InputError(
'Could not find bazelisk or bazel on PATH. ' +
'Install bazelisk (recommended; https://github.com/bazelbuild/bazelisk) ' +
'or bazel, or pass --bazel <path>.',
`or bazel, or name its path with ${explicitFlag ? `${explicitFlag} or ` : ''}SOCKET_CLI_BAZEL_PATH.`,
)
}
30 changes: 30 additions & 0 deletions src/commands/manifest/bazel/bin-detect.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('resolveBazelBinary', () => {
expect(statSync(resolved).ino).toBe(statSync(bin).ino)
})

it('takes SOCKET_CLI_BAZEL_PATH ahead of an explicit path', async () => {
const fromEnv = install('from-env')
const resolved = await resolveBazelBinary(
install('selected'),
getBazelEnvironment({ SOCKET_CLI_BAZEL_PATH: fromEnv }),
cwd,
)
expect(statSync(resolved).ino).toBe(statSync(fromEnv).ino)
})

it('prefers Bazelisk over Bazel', async () => {
const bazelisk = install('bazelisk')
install('bazel')
Expand Down Expand Up @@ -91,5 +101,25 @@ describe('resolveBazelBinary', () => {
await expect(
resolveBazelBinary(path.join(cwd, 'missing'), getBazelEnvironment(), cwd),
).rejects.toThrow(/--bazel path does not exist/)
await expect(
resolveBazelBinary(
undefined,
getBazelEnvironment({
SOCKET_CLI_BAZEL_PATH: path.join(cwd, 'missing'),
}),
cwd,
),
).rejects.toThrow(/SOCKET_CLI_BAZEL_PATH path does not exist/)
})

it('names only SOCKET_CLI_BAZEL_PATH for a command with no option naming Bazel', async () => {
const missing = resolveBazelBinary(
undefined,
getBazelEnvironment({ PATH: cwd }),
cwd,
null,
)
await expect(missing).rejects.toThrow(/with SOCKET_CLI_BAZEL_PATH\.$/)
await expect(missing).rejects.not.toThrow(/--bazel/)
})
})
8 changes: 1 addition & 7 deletions src/commands/manifest/generate_auto_manifest.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
socketFactsRunOptions,
} from './generate-recursive-manifests.mts'
import { handleManifestConda } from './handle-manifest-conda.mts'
import { runManifestFacts } from './run-manifest-facts.mts'
import { AUTO_MANIFEST_HINTS, runManifestFacts } from './run-manifest-facts.mts'
import {
ENVIRONMENT_YML,
REQUIREMENTS_TXT,
Expand All @@ -27,12 +27,6 @@ import type {
import type { BuildTool } from './scripts/build-tool.mts'
import type { OutputKind } from '../../types.mts'

// Auto-manifest reads every build option from the root socket.json.
const AUTO_MANIFEST_HINTS: Omit<BuildOptionHints, keyof CommandHints> = {
setupCommand: 'socket manifest setup',
socketJson: SOCKET_JSON,
}

export type GenerateAutoManifestResult = {
generatedFiles: string[]
}
Expand Down
Loading
Loading