forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
541 lines (480 loc) · 13.8 KB
/
Copy pathutil.js
File metadata and controls
541 lines (480 loc) · 13.8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// Copyright 2018-2026 the Deno authors. MIT license.
// deno-lint-ignore-file no-console
import {
dirname,
extname,
fromFileUrl,
join,
resolve,
SEPARATOR,
toFileUrl,
} from "@std/path";
import { existsSync } from "@std/fs";
export { dirname, extname, fromFileUrl, join, resolve, SEPARATOR, toFileUrl };
export { existsSync, expandGlobSync, walk } from "@std/fs";
export { TextLineStream } from "@std/streams/text-line-stream";
export { delay } from "@std/async/delay";
export { parse as parseJSONC } from "@std/jsonc/parse";
import { createHash } from "node:crypto";
// [toolName] --version output
const versions = {
"dlint": "dlint 0.73.0",
};
const compressed = new Set(["ld64.lld", "rcodesign"]);
export const ROOT_PATH = dirname(dirname(fromFileUrl(import.meta.url)));
let isKnownJjRepo = undefined;
function isJjRepo() {
if (isKnownJjRepo !== undefined) {
return isKnownJjRepo;
}
isKnownJjRepo = existsSync(join(ROOT_PATH, ".jj"));
return isKnownJjRepo;
}
async function getFilesFromGit(baseDir, args) {
const { success, stdout } = await new Deno.Command("git", {
stderr: "inherit",
args,
}).output();
const output = new TextDecoder().decode(stdout);
if (!success) {
throw new Error("gitLsFiles failed");
}
const files = output
.split("\0")
.filter((line) => line.length > 0)
.map((filePath) => {
try {
return Deno.realPathSync(join(baseDir, filePath));
} catch {
return null;
}
})
.filter((filePath) => filePath !== null);
return files;
}
// Translate a single git pathspec into an equivalent jj `glob:` pattern. git
// pathspec wildcards match across `/` (fnmatch without FNM_PATHNAME) and a
// trailing directory matches everything under it, but jj's `glob:` `*` stops at
// `/`. So map the directory-recursive forms to `**`.
function normalizeGitGlob(glob) {
// `*.ext` matches at any depth in git.
const extMatch = glob.match(/^\*(\..+)/);
if (extMatch) {
return "**/*" + extMatch[1];
}
// A trailing directory (`foo/`) is a recursive prefix match.
if (glob.endsWith("/")) {
return glob + "**";
}
// A trailing `foo/*` matches recursively in git (the `*` crosses `/`).
if (glob.endsWith("/*")) {
return glob.slice(0, -1) + "**";
}
return glob;
}
function gitGlobsToJjFileset(patterns) {
if (patterns.length === 0) {
return ['glob:"*"'];
}
let output = "";
const negative = [];
/** @type {string[]} */
const positive = [];
for (let i = 0; i < patterns.length; i++) {
if (patterns[i].startsWith(":!:")) {
negative.push(patterns[i].replace(/^:!:/, ""));
} else {
positive.push(patterns[i]);
}
}
for (let i = 0; i < positive.length; i++) {
const glob = normalizeGitGlob(positive[i]);
if (i === 0) {
output += `(glob-i:"${glob}"`;
} else {
output += ' | glob-i:"' + glob + '"';
}
}
output += ")";
if (negative.length) {
output += " ~ ";
}
for (let i = 0; i < negative.length; i++) {
const glob = normalizeGitGlob(negative[i]);
if (i === 0) {
output += `(glob-i:"${glob}"`;
} else {
output += ' | glob-i:"' + glob + '"';
}
}
if (negative.length) {
output += ")";
}
return [output];
}
async function getFilesFromJj(baseDir, patterns) {
baseDir = Deno.realPathSync(baseDir);
const jjPatterns = gitGlobsToJjFileset(patterns);
const cmd = [
"file",
"list",
...jjPatterns,
];
const { success, stdout } = await new Deno.Command(
"jj",
{
stderr: "inherit",
args: cmd,
cwd: baseDir,
},
).output();
const output = new TextDecoder().decode(stdout);
if (!success) {
throw new Error("getFilesFromJj failed");
}
const files = output.split("\n").filter((line) => line.length > 0)
.map((filePath) => {
try {
return Deno.realPathSync(join(baseDir, filePath));
} catch {
return null;
}
})
.filter((filePath) => filePath !== null);
return files;
}
export function gitLsFiles(baseDir, patterns) {
if (isJjRepo()) {
return getFilesFromJj(baseDir, patterns);
}
baseDir = Deno.realPathSync(baseDir);
const cmd = [
"-C",
baseDir,
"ls-files",
"-z",
"--exclude-standard",
"--cached",
"--modified",
"--others",
"--",
...patterns,
];
return getFilesFromGit(baseDir, cmd);
}
/** List all files staged for commit */
function gitStaged(baseDir, patterns) {
baseDir = Deno.realPathSync(baseDir);
const cmd = [
"-C",
baseDir,
"diff",
"--staged",
"--diff-filter=ACMR",
"--name-only",
"-z",
"--",
...patterns,
];
return getFilesFromGit(baseDir, cmd);
}
/**
* Recursively list all files in (a subdirectory of) a git worktree.
* * Optionally, glob patterns may be specified to e.g. only list files with a
* certain extension.
* * Untracked files are included, unless they're listed in .gitignore.
* * Directory names themselves are not listed (but the files inside are).
* * Submodules and their contents are ignored entirely.
* * This function fails if the query matches no files.
*
* If --staged argument was provided when program is run
* only staged sources will be returned.
*/
export async function getSources(baseDir, patterns) {
const stagedOnly = Deno.args.includes("--staged");
if (stagedOnly) {
return await gitStaged(baseDir, patterns);
} else {
return await gitLsFiles(baseDir, patterns);
}
}
export function buildMode() {
if (Deno.args.includes("--release")) {
return "release";
}
return "debug";
}
export function buildPath() {
return join(ROOT_PATH, "target", buildMode());
}
const platformDirName = {
"windows": "win",
"darwin": "mac",
"linux": "linux64",
}[Deno.build.os];
const executableSuffix = Deno.build.os === "windows" ? ".exe" : "";
async function sanityCheckPrebuiltFile(toolPath) {
const stat = await Deno.stat(toolPath);
if (stat.size < PREBUILT_MINIMUM_SIZE) {
throw new Error(
`File size ${stat.size} is less than expected minimum file size ${PREBUILT_MINIMUM_SIZE}`,
);
}
const file = await Deno.open(toolPath, { read: true });
const buffer = new Uint8Array(1024);
let n = 0;
while (n < 1024) {
n += await file.read(buffer.subarray(n));
}
// Mac: OK
if (buffer[0] == 0xcf && buffer[1] == 0xfa) {
return;
}
// Windows OK
if (buffer[0] == "M".charCodeAt(0) && buffer[1] == "Z".charCodeAt(0)) {
return;
}
// Linux OK
if (
buffer[0] == 0x7f && buffer[1] == "E".charCodeAt(0) &&
buffer[2] == "L".charCodeAt(0) && buffer[3] == "F".charCodeAt(0)
) {
return;
}
throw new Error(`Invalid executable (header was ${buffer.subarray(0, 16)}`);
}
export async function getPrebuilt(toolName) {
const toolPath = getPrebuiltToolPath(toolName);
try {
await sanityCheckPrebuiltFile(toolPath);
const versionOk = await verifyVersion(toolName, toolPath);
if (!versionOk) {
throw new Error("Version mismatch");
}
} catch {
await downloadPrebuilt(toolName);
}
return toolPath;
}
const PREBUILT_PATH = join(ROOT_PATH, "third_party", "prebuilt");
const PREBUILT_TOOL_DIR = join(PREBUILT_PATH, platformDirName);
const PREBUILT_MINIMUM_SIZE = 16 * 1024;
const DOWNLOAD_TASKS = {};
export function getPrebuiltToolPath(toolName) {
return join(PREBUILT_TOOL_DIR, toolName + executableSuffix);
}
const commitId = "e593815dcfe5d260b27e6556cc9e44dcfcaeda3d";
const downloadUrl =
`https://raw.githubusercontent.com/denoland/deno_third_party/${commitId}/prebuilt/${platformDirName}`;
export async function downloadPrebuilt(toolName) {
// Ensure only one download per tool happens at a time
if (DOWNLOAD_TASKS[toolName]) {
return await DOWNLOAD_TASKS[toolName].promise;
}
const downloadDeferred = DOWNLOAD_TASKS[toolName] = Promise.withResolvers();
console.error("Downloading prebuilt tool:", toolName);
const toolPath = getPrebuiltToolPath(toolName);
const tempFile = `${toolPath}.temp`;
try {
await Deno.mkdir(PREBUILT_TOOL_DIR, { recursive: true });
let url = `${downloadUrl}/${toolName}${executableSuffix}`;
if (compressed.has(toolName)) {
url += ".gz";
}
const headers = new Headers();
if (Deno.env.has("GITHUB_TOKEN")) {
headers.append("authorization", `Bearer ${Deno.env.get("GITHUB_TOKEN")}`);
}
const resp = await fetch(url, { headers });
if (!resp.ok) {
throw new Error(`Non-successful response from ${url}: ${resp.status}`);
}
const file = await Deno.open(tempFile, {
create: true,
write: true,
mode: 0o755,
});
if (compressed.has(toolName)) {
await resp.body.pipeThrough(new DecompressionStream("gzip")).pipeTo(
file.writable,
);
} else {
await resp.body.pipeTo(file.writable);
}
console.error("Checking prebuilt tool:", toolName);
await sanityCheckPrebuiltFile(tempFile);
if (!await verifyVersion(toolName, tempFile)) {
throw new Error(
"Didn't get the correct version of the tool after downloading.",
);
}
console.error("Successfully downloaded:", toolName);
try {
// necessary on Windows it seems
await Deno.remove(toolPath);
} catch {
// ignore
}
await Deno.rename(tempFile, toolPath);
} catch (e) {
downloadDeferred.reject(e);
throw e;
}
downloadDeferred.resolve(null);
}
export async function verifyVersion(toolName, toolPath) {
const requiredVersion = versions[toolName];
if (!requiredVersion) {
return true;
}
try {
const cmd = new Deno.Command(toolPath, {
args: ["--version"],
stdout: "piped",
stderr: "inherit",
});
const output = await cmd.output();
const version = new TextDecoder().decode(output.stdout).trim();
return version == requiredVersion;
} catch (e) {
console.error(e);
return false;
}
}
/// INPUT HASHING
/** A streaming hasher for computing a combined hash of multiple inputs.
* Mirrors the Rust InputHasher API in tests/util/lib/hash.rs. */
export class InputHasher {
#hash;
constructor() {
this.#hash = createHash("sha256");
}
/** Create a hasher pre-seeded with the current CLI args. */
static newWithCliArgs() {
const hasher = new InputHasher();
for (const arg of Deno.args) {
hasher.writeSync(arg);
}
return hasher;
}
/** Write raw string data into the hash. */
writeSync(data) {
this.#hash.update(data);
}
/** Hash a single file's contents (streamed). Skips if file doesn't exist. */
async hashFile(path) {
try {
const file = await Deno.open(path);
for await (const chunk of file.readable) {
this.#hash.update(chunk);
}
} catch {
// skip if file doesn't exist
}
return this;
}
/** Recursively hash all file contents in a directory (sorted for
* determinism). Skips if directory doesn't exist. */
async hashDir(path) {
const entries = [];
collectEntriesRecursive(path, entries);
entries.sort();
for (const entryPath of entries) {
// hash the relative path for determinism
if (entryPath.startsWith(path)) {
this.writeSync(entryPath.slice(path.length));
}
try {
const file = await Deno.open(entryPath);
for await (const chunk of file.readable) {
this.#hash.update(chunk);
}
} catch {
// skip unreadable files
}
}
return this;
}
/** Finalize the hash and return a hex string. */
finish() {
return this.#hash.digest("hex");
}
}
function collectEntriesRecursive(dir, out) {
let entries;
try {
entries = Deno.readDirSync(dir);
} catch {
return;
}
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory) {
collectEntriesRecursive(fullPath, out);
} else {
out.push(fullPath);
}
}
}
const HASHY_URL = "https://hashy.deno.deno.net";
/**
* Check if tests can be skipped on CI by comparing input hashes
* against the hashy service.
*
* `name` is used for the hash key and log messages (e.g. "wpt").
* `configure` receives an InputHasher to add whatever files/dirs are relevant.
*
* Returns `{ skip: boolean, commit: () => Promise<void> }`.
* Call `commit()` after tests pass to mark the hash as known-good.
*/
export async function checkCiHash(name, configure) {
const noop = { skip: false, commit: async () => {} };
if (!Deno.env.get("CI")) {
return noop;
}
const start = performance.now();
const hasher = InputHasher.newWithCliArgs();
await configure(hasher);
const hash = await hasher.finish();
const key = `${name}_${hash}`;
const elapsed = Math.round(performance.now() - start);
console.log(`ci hash took ${elapsed}ms`);
const commitFn = async () => {
try {
await fetch(`${HASHY_URL}/hashes/${key}`, {
method: "PUT",
signal: AbortSignal.timeout(5000),
});
console.log(`hashy: committed hash ${key}`);
} catch {
console.log(`hashy: failed to commit hash ${key}`);
}
};
// On main/tag builds, always run tests but still commit on success
// to seed the cache for PR builds.
if (isMainOrTag()) {
console.log(
`hashy: main/tag build, running tests (will commit on success)`,
);
return { skip: false, commit: commitFn };
}
try {
const res = await fetch(`${HASHY_URL}/hashes/${key}`, {
signal: AbortSignal.timeout(5000),
});
if (res.ok) {
console.log(`hashy: ${name} hash found (${key}), skipping`);
return { skip: true, commit: async () => {} };
}
} catch {
// service unreachable — run tests
console.log(`hashy: failed to check hash, running tests`);
return noop;
}
console.log(`hashy: ${name} hash not found (${key}), will run tests`);
return { skip: false, commit: commitFn };
}
function isMainOrTag() {
const ref = Deno.env.get("GITHUB_REF") ?? "";
return ref === "refs/heads/main" || ref.startsWith("refs/tags/");
}