|
| 1 | +const { join, dirname, extname } = require("path"); |
| 2 | +const { readFileSync } = require("fs"); |
| 3 | + |
| 4 | +// to require jest-haste-map we need to hop from jest-cli => @jest/core => jest-haste-map in the virtual store |
| 5 | +const jestCliPackage = dirname(require.resolve("jest-cli/package.json")); |
| 6 | +const jestConfigPackage = dirname( |
| 7 | + require.resolve(join(jestCliPackage, "../@jest/core/package.json")), |
| 8 | +); |
| 9 | +const HasteMap = require( |
| 10 | + join(jestConfigPackage, "../../jest-haste-map"), |
| 11 | +).default; |
| 12 | +const fastPath = require( |
| 13 | + join(jestConfigPackage, "../../jest-haste-map/build/lib/fast_path.js"), |
| 14 | +); |
| 15 | + |
| 16 | +// The path to the rules_jest files list file |
| 17 | +const WORKSPACE_RUNFILES = join( |
| 18 | + process.env.TEST_SRCDIR, |
| 19 | + process.env.TEST_WORKSPACE, |
| 20 | +); |
| 21 | +const BAZEL_FILELIST_JSON_FULL_PATH = join( |
| 22 | + WORKSPACE_RUNFILES, |
| 23 | + global.BAZEL_FILELIST_JSON_SHORT_PATH, |
| 24 | +); |
| 25 | + |
| 26 | +/** |
| 27 | + * Extend the standard jest HasteMap to use rules_jest |
| 28 | + */ |
| 29 | +module.exports = class BazelHasteMap extends HasteMap { |
| 30 | + constructor(options) { |
| 31 | + super({ |
| 32 | + // Override some default HasteMap options. |
| 33 | + // These are HasteMap.Options, not Config.Options.haste |
| 34 | + dependencyExtractor: null, |
| 35 | + computeDependencies: false, |
| 36 | + resetCache: true, |
| 37 | + |
| 38 | + ...options, |
| 39 | + }); |
| 40 | + |
| 41 | + // Override the jest HasteMap._crawl() to not invoke `find` or `fs.*` |
| 42 | + this._crawl = this.bazelCrawl.bind(this); |
| 43 | + |
| 44 | + // Override & remove the HastMap._persist() method to disable persisting the cache due to: |
| 45 | + // - when `config.cacheDirectory` is not persisted across jest_test invocations caching is ineffective |
| 46 | + // - when extending `HasteMap` the `BazelHasteMap` construction does not invoke the standard `HasteMap` factory |
| 47 | + // logic including `await HasteMap.setupCachePath()` which is required for `._persist()` to work. |
| 48 | + this._persist = function bazelNoopPersist() {}; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * A rules_jest replacement for the standard jest `crawl` function. |
| 53 | + * |
| 54 | + * Modified to: |
| 55 | + * - use the rules_jest replacement for `find` to avoid walking the filesystem |
| 56 | + * - disable or skip all jest caching that is not applicable in bazel |
| 57 | + * - assume rules_jest did all directory+symlink filtering to avoid stat() calls |
| 58 | + * |
| 59 | + * See https://github.com/jestjs/jest/blob/v29.7.0/packages/jest-haste-map/src/index.ts#L760-L773 |
| 60 | + */ |
| 61 | + async bazelCrawl(hasteMap /*: InternalHasteMap*/) { |
| 62 | + const ignore = this._ignore.bind(this); |
| 63 | + const { extensions, rootDir, enableSymlinks, roots } = this._options; |
| 64 | + |
| 65 | + return new Promise(function bazelCrawlExecutor(resolve) { |
| 66 | + function findComplete(files) { |
| 67 | + const filesMap = new Map(); |
| 68 | + for (const file of files) { |
| 69 | + const relativeFilePath = fastPath.relative(rootDir, file); |
| 70 | + filesMap.set(relativeFilePath, [ |
| 71 | + "", // ID |
| 72 | + 0, // MTIME |
| 73 | + 0, // SIZE |
| 74 | + 0, // VISITED |
| 75 | + "", // DEPENDENCIES |
| 76 | + null, // SHA1 (disabled by default by rules_jest) |
| 77 | + ]); |
| 78 | + } |
| 79 | + hasteMap.files = filesMap; |
| 80 | + |
| 81 | + resolve({ |
| 82 | + hasteMap: hasteMap, |
| 83 | + removedFiles: new Map(), |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + find(roots, extensions, ignore, enableSymlinks, findComplete); |
| 88 | + }); |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * A rules_jest replacement for standard jest fs `find` function. |
| 94 | + * |
| 95 | + * Differences from standard `find`: |
| 96 | + * - all fs info read from the BAZEL_FILELIST_JSON_FULL_PATH file outputted by the jest rule, no fs operations/traversal |
| 97 | + * |
| 98 | + * See: https://github.com/jestjs/jest/blob/v29.7.0/packages/jest-haste-map/src/crawlers/node.ts#L59-L131 |
| 99 | + */ |
| 100 | +function find(roots, extensions, ignore, enableSymlinks, callback) { |
| 101 | + const files = JSON.parse( |
| 102 | + readFileSync(BAZEL_FILELIST_JSON_FULL_PATH, { encoding: "utf8" }), |
| 103 | + ); |
| 104 | + |
| 105 | + // TODO: exclude those not in `roots`? |
| 106 | + |
| 107 | + const result = []; |
| 108 | + |
| 109 | + for (const file of files) { |
| 110 | + const ext = extname(file).slice(1); |
| 111 | + if (!extensions.includes(ext)) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + const f = join(WORKSPACE_RUNFILES, file); |
| 116 | + if (ignore(f)) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + result.push(f); |
| 121 | + } |
| 122 | + |
| 123 | + callback(result); |
| 124 | +} |
0 commit comments