| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {AbsolutePath, RepoRelativePath} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {Repository} from './Repository'; |
| b69ab31 | | | 10 | import type {RepositoryContext} from './serverTypes'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {GeneratedStatus} from 'isl/src/types'; |
| b69ab31 | | | 13 | import {promises as fs} from 'node:fs'; |
| b69ab31 | | | 14 | import pathMod from 'node:path'; |
| b69ab31 | | | 15 | import {LRU} from 'shared/LRU'; |
| b69ab31 | | | 16 | import {group} from 'shared/utils'; |
| b69ab31 | | | 17 | import {Internal} from './Internal'; |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | export const GENERATED_TAG = '@' + 'generated'; |
| b69ab31 | | | 20 | export const PARTIALLY_GENERATED_TAG = '@' + 'partially-generated'; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const defaultGeneratedPathRegex = |
| b69ab31 | | | 23 | Internal.generatedFilesRegex ?? |
| b69ab31 | | | 24 | /(yarn\.lock$|package-lock\.json$|node_modules\/.*$|\.py[odc]$|\.class$|\.[oa]$|\.so$|Gemfile\.lock$|go\.sum$|Cargo\.lock$|\.dll$|\.exe$|\.pdb$|composer\.lock$|Podfile\.lock$)/; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | async function getGeneratedFilePathRegex( |
| b69ab31 | | | 27 | repo: Repository, |
| b69ab31 | | | 28 | ctx: RepositoryContext, |
| b69ab31 | | | 29 | ): Promise<RegExp> { |
| b69ab31 | | | 30 | const configuredPathRegex = await repo.getConfig(ctx, 'isl.generated-files-regex'); |
| b69ab31 | | | 31 | let regex = defaultGeneratedPathRegex; |
| b69ab31 | | | 32 | if (configuredPathRegex) { |
| b69ab31 | | | 33 | try { |
| b69ab31 | | | 34 | regex = new RegExp(configuredPathRegex); |
| b69ab31 | | | 35 | } catch (err) { |
| b69ab31 | | | 36 | repo.initialConnectionContext.logger.error( |
| b69ab31 | | | 37 | 'Configured generated files regex is invalid', |
| b69ab31 | | | 38 | err, |
| b69ab31 | | | 39 | ); |
| b69ab31 | | | 40 | } |
| b69ab31 | | | 41 | } |
| b69ab31 | | | 42 | return regex; |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | function readFilesLookingForGeneratedTag( |
| b69ab31 | | | 46 | cwd: AbsolutePath, |
| b69ab31 | | | 47 | files: Array<string>, |
| b69ab31 | | | 48 | ): Promise<Array<[RepoRelativePath, GeneratedStatus]>> { |
| b69ab31 | | | 49 | return Promise.all( |
| b69ab31 | | | 50 | files.map(async (path): Promise<[RepoRelativePath, GeneratedStatus]> => { |
| b69ab31 | | | 51 | let chunk; |
| b69ab31 | | | 52 | try { |
| b69ab31 | | | 53 | const f = await fs.open(pathMod.join(cwd, path)); |
| b69ab31 | | | 54 | chunk = await f.read({length: 1024}); |
| b69ab31 | | | 55 | f.close(); |
| b69ab31 | | | 56 | } catch (e) { |
| b69ab31 | | | 57 | // e.g. missing files considered Manual. This can happen when queries files in non-head commits. |
| b69ab31 | | | 58 | // More accurate would be to `sl cat`, but that's expensive. |
| b69ab31 | | | 59 | return [path, GeneratedStatus.Manual]; |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | if (chunk.buffer.includes(GENERATED_TAG)) { |
| b69ab31 | | | 62 | return [path, GeneratedStatus.Generated]; |
| b69ab31 | | | 63 | } else if (chunk.buffer.includes(PARTIALLY_GENERATED_TAG)) { |
| b69ab31 | | | 64 | return [path, GeneratedStatus.PartiallyGenerated]; |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | return [path, GeneratedStatus.Manual]; |
| b69ab31 | | | 67 | }), |
| b69ab31 | | | 68 | ); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | export class GeneratedFilesDetector { |
| b69ab31 | | | 72 | // We assume the same file path doesn't switch generated status, so we can cache aggressively. |
| b69ab31 | | | 73 | private cache = new LRU<RepoRelativePath, GeneratedStatus>(1500); |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | /** |
| b69ab31 | | | 76 | * Given a list of files, return an object mapping path to Generated Status. |
| b69ab31 | | | 77 | * Files are determined to be generated by looking in the first 512 bytes for @ + generated, |
| b69ab31 | | | 78 | * or partially generated by looking for @ + partially-generated. |
| b69ab31 | | | 79 | */ |
| b69ab31 | | | 80 | public async queryFilesGenerated( |
| b69ab31 | | | 81 | repo: Repository, |
| b69ab31 | | | 82 | ctx: RepositoryContext, |
| b69ab31 | | | 83 | root: AbsolutePath, |
| b69ab31 | | | 84 | files: Array<RepoRelativePath>, |
| b69ab31 | | | 85 | ): Promise<Record<RepoRelativePath, GeneratedStatus>> { |
| b69ab31 | | | 86 | if (files.length === 0) { |
| b69ab31 | | | 87 | return {}; |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | const t1 = performance.now(); |
| b69ab31 | | | 90 | const {logger} = ctx; |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | const regex = await getGeneratedFilePathRegex(repo, ctx); |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | const results = group( |
| b69ab31 | | | 95 | files, |
| b69ab31 | | | 96 | // (1) try the cache |
| b69ab31 | | | 97 | // (2) test if it matches the generated file regex, if so, it's generated |
| b69ab31 | | | 98 | // (3) if the regex fails or its not in cache, we need to try reading the file |
| b69ab31 | | | 99 | file => |
| b69ab31 | | | 100 | this.cache.get(file) ?? |
| b69ab31 | | | 101 | (regex.test(file) ? GeneratedStatus.Generated : undefined) ?? |
| b69ab31 | | | 102 | 'notCached', |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | const needsCheck = results.notCached ?? []; |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | const checkResult = |
| b69ab31 | | | 108 | needsCheck.length === 0 ? [] : await readFilesLookingForGeneratedTag(root, needsCheck); |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | const remaining = new Set(needsCheck); |
| b69ab31 | | | 111 | for (const [path, st] of checkResult) { |
| b69ab31 | | | 112 | this.cache.set(path, st); |
| b69ab31 | | | 113 | remaining.delete(path); |
| b69ab31 | | | 114 | } |
| b69ab31 | | | 115 | |
| b69ab31 | | | 116 | const t2 = performance.now(); |
| b69ab31 | | | 117 | logger.info( |
| b69ab31 | | | 118 | `Generated file query took ${Math.floor((10 * (t2 - t1)) / 10)}ms for ${ |
| b69ab31 | | | 119 | files.length |
| b69ab31 | | | 120 | } files. (${needsCheck.length} not cached)`, |
| b69ab31 | | | 121 | ); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | return Object.fromEntries([ |
| b69ab31 | | | 124 | ...(results[GeneratedStatus.Manual]?.map(p => [p, GeneratedStatus.Manual]) ?? []), |
| b69ab31 | | | 125 | ...(results[GeneratedStatus.Generated]?.map(p => [p, GeneratedStatus.Generated]) ?? []), |
| b69ab31 | | | 126 | ...(results[GeneratedStatus.PartiallyGenerated]?.map(p => [ |
| b69ab31 | | | 127 | p, |
| b69ab31 | | | 128 | GeneratedStatus.PartiallyGenerated, |
| b69ab31 | | | 129 | ]) ?? []), |
| b69ab31 | | | 130 | ...checkResult, |
| b69ab31 | | | 131 | ]); |
| b69ab31 | | | 132 | } |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | public clear() { |
| b69ab31 | | | 135 | this.cache.clear(); |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | } |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | export const generatedFilesDetector = new GeneratedFilesDetector(); |