forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox-plugin-bazel.ts
More file actions
190 lines (175 loc) · 6.68 KB
/
sandbox-plugin-bazel.ts
File metadata and controls
190 lines (175 loc) · 6.68 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
/**
* Forked from https://github.com/aspect-build/rules_esbuild/blob/e4e49d3354cbf7087c47ac9c5f2e6fe7f5e398d3/esbuild/private/plugins/bazel-sandbox.js
*/
import type { OnResolveResult, Plugin, PluginBuild, ResolveOptions } from 'esbuild';
import { stat } from 'node:fs/promises';
import path, { join } from 'node:path';
export interface CreateBazelSandboxPluginOptions {
bindir: string;
execroot: string;
runfiles?: string;
}
// Under Bazel, esbuild will follow symlinks out of the sandbox when the sandbox
// is enabled. See https://github.com/aspect-build/rules_esbuild/issues/58. This
// plugin using a separate resolver to detect if the the resolution has left the
// execroot (which is the root of the sandbox when sandboxing is enabled) and
// patches the resolution back into the sandbox.
export function createBazelSandboxPlugin({
bindir,
execroot,
runfiles,
}: CreateBazelSandboxPluginOptions): Plugin {
return {
name: 'bazel-sandbox',
setup(build) {
build.onResolve({ filter: /./ }, async ({ path: importPath, ...otherOptions }) => {
// NB: these lines are to prevent infinite recursion when we call `build.resolve`.
if (otherOptions.pluginData) {
if (otherOptions.pluginData.executedSandboxPlugin) {
return;
}
} else {
otherOptions.pluginData = {};
}
otherOptions.pluginData.executedSandboxPlugin = true;
return await resolveInExecroot({
build,
bindir,
execroot,
runfiles,
importPath,
otherOptions,
});
});
},
};
}
interface ResolveInExecrootOptions {
build: PluginBuild;
bindir: string;
execroot: string;
runfiles?: string;
importPath: string;
otherOptions: ResolveOptions;
}
const EXTERNAL_PREFIX = 'external/';
const NON_EXTERNAL_PREFIX = '_main/';
function runfilesRelativePath(filePath: string): string {
// Normalize to relative path without leading slash.
if (filePath.startsWith('/')) {
filePath = filePath.substring(1);
}
// Remove the EXTERNAL_PREFIX if present.
if (filePath.startsWith(EXTERNAL_PREFIX)) {
filePath = filePath.substring(EXTERNAL_PREFIX.length);
} else if (!filePath.startsWith(NON_EXTERNAL_PREFIX)) {
// If the file is not in an external repository, the file will be under
// `_main` within the runfiles tree.
filePath = NON_EXTERNAL_PREFIX + filePath;
}
return filePath;
}
async function resolveInExecroot({
build,
bindir,
execroot,
runfiles,
importPath,
otherOptions,
}: ResolveInExecrootOptions): Promise<OnResolveResult> {
const result = await build.resolve(importPath, otherOptions);
if (result.errors && result.errors.length) {
// There was an error resolving, just return the error as-is.
return result;
}
if (
!result.path.startsWith('.') &&
!result.path.startsWith('/') &&
!result.path.startsWith('\\')
) {
// Not a relative or absolute path. Likely a module resolution that is
// marked "external"
return result;
}
// If esbuild attempts to leave the execroot, map the path back into the
// execroot.
if (!result.path.startsWith(execroot)) {
// If it tried to leave bazel-bin, error out completely.
if (!result.path.includes(bindir)) {
throw new Error(
`Error: esbuild resolved a path outside of BAZEL_BINDIR (${bindir}): ${result.path}`,
);
}
// Get the path under the bindir for the file. This allows us to map into
// the execroot or the runfiles directory (if present).
// Example:
// bindir = bazel-out/<arch>/bin
// result.path = <base>/execroot/bazel-out/<arch>/bin/external/repo+/path/file.ts
// binDirRelativePath = external/repo+/path/file.ts
const binDirRelativePath = result.path.substring(
result.path.indexOf(bindir) + bindir.length + 1,
);
// We usually remap into the bindir. However, when sources are provided
// as `data` (runfiles), they will be in the runfiles root instead. The
// runfiles path is absolute and under the bindir, so we don't need to
// join anything to it. The execroot does not include the bindir, so there
// we add it again after previously removing it from the result path.
const remapBase = runfiles ?? path.join(execroot, bindir);
// The path relative to the remapBase also differs between runfiles and
// bindir. External repositories appear under `external/repo+` in the
// bindir, whereas they are directly under `repo+` in the runfiles tree.
// Non-external files appear under a special `_main` directory in the
// runfiles tree, but not in the bindir. These differences need to be
// accounted for by removing a potential `external/` prefix or adding a
// `_main` prefix when mapping into runfiles.
const remapBaseRelativePath = runfiles
? runfilesRelativePath(binDirRelativePath)
: binDirRelativePath;
// Join the paths back together. The results will look slightly different
// between runfiles and bindir, but this is intentional.
// Source path:
// <bin>/external/repo+/path/file.ts
// Example in bindir:
// <sandbox-bin>/external/repo+/path/file.ts
// Example in runfiles:
// <sandbox-bin>/path/bin.runfiles/repo+/path/file.ts
const correctedPath = join(remapBase, remapBaseRelativePath);
if (process.env.JS_BINARY__LOG_DEBUG) {
// eslint-disable-next-line no-console
console.error(
`DEBUG: [bazel-sandbox] correcting resolution ${result.path} that left the sandbox to ${correctedPath}.`,
);
}
result.path = correctedPath;
// Fall back to `.js` file if resolved `.ts` file does not exist in the
// changed path.
//
// It's possible that a `.ts` file exists outside the sandbox and esbuild
// resolves it. It is not guaranteed that the sandbox also contains the same
// file. One example might be that the build depends on a compiled version
// of the file and the sandbox will only contain the corresponding `.js` and
// `.d.ts` files.
if (result.path.endsWith('.ts')) {
try {
await stat(result.path);
} catch (e: unknown) {
const jsPath = result.path.slice(0, -3) + '.js';
if (process.env.JS_BINARY__LOG_DEBUG) {
// eslint-disable-next-line no-console
console.error(
`DEBUG: [bazel-sandbox] corrected resolution ${result.path} does not exist in the sandbox, trying ${jsPath}.`,
);
}
result.path = jsPath;
}
}
}
return result;
}