forked from googleapis/release-please
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbazel-query.ts
More file actions
175 lines (156 loc) · 5.26 KB
/
bazel-query.ts
File metadata and controls
175 lines (156 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {execFileSync} from 'child_process';
import {Logger} from './logger';
/**
* Parse the output of a bazel query command into a list of local package paths.
*
* Bazel query output contains lines like:
* //path/to/package:target_name
* @external_dep//path:target
*
* This function:
* 1. Filters out external dependencies (lines starting with `@`)
* 2. Extracts the package path from local targets (`//path/to/package:target` -> `path/to/package`)
* 3. Deduplicates paths
* 4. Optionally excludes a given path (e.g., the package's own path)
*
* @param output Raw stdout from a bazel query command
* @param excludePath Optional path to exclude from results (e.g., the package's own path)
* @returns Array of unique local package paths
*/
export function parseBazelQueryOutput(
output: string,
excludePath?: string
): string[] {
const lines = output.split('\n').filter(line => line.trim().length > 0);
const paths = new Set<string>();
for (const line of lines) {
const trimmed = line.trim();
// Skip external dependencies (start with @)
if (trimmed.startsWith('@')) {
continue;
}
// Match local targets: //path/to/package:target or //path/to/package
const match = trimmed.match(/^\/\/([^:]*)/);
if (!match) {
continue;
}
const packagePath = match[1];
// Skip empty paths (e.g., //:target refers to the root)
if (!packagePath) {
continue;
}
// Normalize: remove trailing slashes
const normalized = packagePath.replace(/\/+$/, '');
if (!normalized) {
continue;
}
// Exclude the package's own path if specified
if (excludePath && normalized === excludePath.replace(/\/+$/, '')) {
continue;
}
paths.add(normalized);
}
return Array.from(paths).sort();
}
/**
* Build the default bazel query expression for a given package path.
*
* @param packagePath The package path (e.g., "apps/my-app")
* @returns The bazel query expression (passed to `bazel query`)
*/
export function buildBazelQueryExpression(packagePath: string): string {
return `deps(//${packagePath})`;
}
/**
* Resolve the bazel query expression from the config value.
*
* - If the config value is `true`, build the default expression from the package path.
* - If the config value is a string:
* - If it starts with `bazel query`, treat it as a full command and extract the query expression
* - Otherwise, treat it as a query expression directly
*
* @param configValue The `bazel-deps-query` config value (boolean or string)
* @param packagePath The package path from the config key
* @returns The resolved bazel query expression
*/
export function resolveBazelQuery(
configValue: boolean | string,
packagePath: string
): string {
if (configValue === true) {
return buildBazelQueryExpression(packagePath);
}
// A boolean `false` behaves like "disabled".
if (configValue === false) {
return '';
}
const trimmed = configValue.trim();
const bazelQueryPrefix = /^bazel\s+query\s+/;
if (bazelQueryPrefix.test(trimmed)) {
let expr = trimmed.replace(bazelQueryPrefix, '').trim();
// If the expression is quoted, strip surrounding quotes.
if (
(expr.startsWith('"') && expr.endsWith('"')) ||
(expr.startsWith("'") && expr.endsWith("'"))
) {
expr = expr.slice(1, -1);
}
return expr;
}
return trimmed;
}
/**
* Execute a bazel query expression and return the parsed local package paths.
*
* This uses `execFileSync` (no shell) to reduce the risk of command injection.
*
* @param queryExpression The query expression to execute (e.g., "deps(//combined-service)")
* @param excludePath Optional path to exclude from results
* @param logger Optional logger instance
* @returns Array of unique local package paths
*/
export function runBazelQuery(
queryExpression: string,
excludePath?: string,
logger?: Logger
): string[] {
logger?.info(`Running bazel deps query: bazel query '${queryExpression}'`);
try {
const output = execFileSync('bazel', ['query', queryExpression], {
encoding: 'utf-8',
timeout: 120000, // 2 minute timeout
stdio: ['pipe', 'pipe', 'pipe'],
});
const paths = parseBazelQueryOutput(output, excludePath);
logger?.info(
`Bazel deps query resolved ${
paths.length
} additional paths: ${JSON.stringify(paths)}`
);
return paths;
} catch (err) {
const error = err as Error & {stderr?: string};
logger?.error(
`Failed to run bazel deps query "${queryExpression}": ${error.message}`
);
if (error.stderr) {
logger?.error(`stderr: ${error.stderr}`);
}
throw new Error(
`Failed to execute bazel-deps-query "${queryExpression}": ${error.message}`
);
}
}