Skip to content

Commit

Permalink
[kbn/optimizer] fix optimizerCache creation (#103190)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
3 people committed Jun 28, 2021
1 parent 0faaada commit c5ed0bd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 45 deletions.
1 change: 0 additions & 1 deletion packages/kbn-optimizer/src/optimizer/cache_keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ describe('getOptimizerCacheKey()', () => {

await expect(getOptimizerCacheKey(config)).resolves.toMatchInlineSnapshot(`
Object {
"bootstrap": "<bootstrap cache>",
"deletedPaths": Array [
"/foo/bar/c",
],
Expand Down
36 changes: 11 additions & 25 deletions packages/kbn-optimizer/src/optimizer/cache_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Path from 'path';
import Fs from 'fs';
import { promisify } from 'util';

import Chalk from 'chalk';
import execa from 'execa';
Expand All @@ -23,8 +22,7 @@ import { getMtimes } from './get_mtimes';
import { getChanges } from './get_changes';
import { OptimizerConfig } from './optimizer_config';

const OPTIMIZER_DIR = Path.dirname(require.resolve('../../package.json'));
const RELATIVE_DIR = Path.relative(REPO_ROOT, OPTIMIZER_DIR);
const RELATIVE_DIR = 'packages/kbn-optimizer';

export function diffCacheKey(expected?: unknown, actual?: unknown) {
const expectedJson = jsonStable(expected, {
Expand Down Expand Up @@ -114,17 +112,12 @@ export function reformatJestDiff(diff: string | null) {

export interface OptimizerCacheKey {
readonly lastCommit: string | undefined;
readonly bootstrap: string | undefined;
readonly workerConfig: CacheableWorkerConfig;
readonly deletedPaths: string[];
readonly modifiedTimes: Record<string, number>;
}

async function getLastCommit() {
if (!Fs.existsSync(Path.join(REPO_ROOT, '.git'))) {
return undefined;
}

const { stdout } = await execa(
'git',
['log', '-n', '1', '--pretty=format:%H', '--', RELATIVE_DIR],
Expand All @@ -136,25 +129,19 @@ async function getLastCommit() {
return stdout.trim() || undefined;
}

async function getBootstrapCacheKey() {
try {
return await promisify(Fs.readFile)(
Path.resolve(OPTIMIZER_DIR, 'target/.bootstrap-cache'),
'utf8'
);
} catch (error) {
if (error?.code !== 'ENOENT') {
throw error;
}
return undefined;
export async function getOptimizerCacheKey(config: OptimizerConfig): Promise<OptimizerCacheKey> {
if (!Fs.existsSync(Path.resolve(REPO_ROOT, '.git'))) {
return {
lastCommit: undefined,
modifiedTimes: {},
workerConfig: config.getCacheableWorkerConfig(),
deletedPaths: [],
};
}
}

export async function getOptimizerCacheKey(config: OptimizerConfig) {
const [changes, lastCommit, bootstrap] = await Promise.all([
getChanges(OPTIMIZER_DIR),
const [changes, lastCommit] = await Promise.all([
getChanges(RELATIVE_DIR),
getLastCommit(),
getBootstrapCacheKey(),
] as const);

const deletedPaths: string[] = [];
Expand All @@ -165,7 +152,6 @@ export async function getOptimizerCacheKey(config: OptimizerConfig) {

const cacheKeys: OptimizerCacheKey = {
lastCommit,
bootstrap,
deletedPaths,
modifiedTimes: {} as Record<string, number>,
workerConfig: config.getCacheableWorkerConfig(),
Expand Down
22 changes: 12 additions & 10 deletions packages/kbn-optimizer/src/optimizer/get_changes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
*/

jest.mock('execa');
jest.mock('fs');

import { getChanges } from './get_changes';
import { REPO_ROOT, createAbsolutePathSerializer } from '@kbn/dev-utils';

const execa: jest.Mock = jest.requireMock('execa');

expect.addSnapshotSerializer(createAbsolutePathSerializer());

it('parses git ls-files output', async () => {
expect.assertions(4);

jest.requireMock('fs').existsSync.mockImplementation(() => true);

execa.mockImplementation((cmd, args, options) => {
expect(cmd).toBe('git');
expect(args).toEqual(['ls-files', '-dmt', '--', '/foo/bar/x']);
expect(args).toEqual(['ls-files', '-dmt', '--', 'foo/bar/x']);
expect(options).toEqual({
cwd: '/foo/bar/x',
cwd: REPO_ROOT,
});

return {
Expand All @@ -37,12 +37,14 @@ it('parses git ls-files output', async () => {
};
});

await expect(getChanges('/foo/bar/x')).resolves.toMatchInlineSnapshot(`
const changes = await getChanges('foo/bar/x');

expect(changes).toMatchInlineSnapshot(`
Map {
"/foo/bar/x/kbn-optimizer/package.json" => "modified",
"/foo/bar/x/kbn-optimizer/src/common/bundle.ts" => "modified",
"/foo/bar/x/kbn-optimizer/src/common/bundles.ts" => "deleted",
"/foo/bar/x/kbn-optimizer/src/get_bundle_definitions.test.ts" => "deleted",
<absolute path>/kbn-optimizer/package.json => "modified",
<absolute path>/kbn-optimizer/src/common/bundle.ts => "modified",
<absolute path>/kbn-optimizer/src/common/bundles.ts => "deleted",
<absolute path>/kbn-optimizer/src/get_bundle_definitions.test.ts => "deleted",
}
`);
});
15 changes: 6 additions & 9 deletions packages/kbn-optimizer/src/optimizer/get_changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,27 @@
import Path from 'path';

import execa from 'execa';
import fs from 'fs';

import { REPO_ROOT } from '@kbn/dev-utils';

export type Changes = Map<string, 'modified' | 'deleted'>;

/**
* get the changes in all the context directories (plugin public paths)
*/
export async function getChanges(dir: string) {
export async function getChanges(relativeDir: string) {
const changes: Changes = new Map();

if (!fs.existsSync(Path.join(dir, '.git'))) {
return changes;
}

const { stdout } = await execa('git', ['ls-files', '-dmt', '--', dir], {
cwd: dir,
const { stdout } = await execa('git', ['ls-files', '-dmt', '--', relativeDir], {
cwd: REPO_ROOT,
});

const output = stdout.trim();

if (output) {
for (const line of output.split('\n')) {
const [tag, ...pathParts] = line.trim().split(' ');
const path = Path.resolve(dir, pathParts.join(' '));
const path = Path.resolve(REPO_ROOT, pathParts.join(' '));
switch (tag) {
case 'M':
case 'C':
Expand Down

0 comments on commit c5ed0bd

Please sign in to comment.