Skip to content

Commit

Permalink
unify validation logic and handle all logging there to avoid test cha…
Browse files Browse the repository at this point in the history
…nges
  • Loading branch information
spalger committed Sep 22, 2020
1 parent 92c0da3 commit c2b46d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
2 changes: 0 additions & 2 deletions packages/kbn-pm/src/commands/bootstrap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ test('handles dependencies of dependencies', async () => {
info [foo] running yarn,
"",
"",
succ yarn.lock analysis completed without any issues,
]
`);
});
Expand Down Expand Up @@ -179,7 +178,6 @@ test('does not run installer if no deps in package', async () => {
info [kibana] running yarn,
"",
"",
succ yarn.lock analysis completed without any issues,
]
`);
});
Expand Down
6 changes: 2 additions & 4 deletions packages/kbn-pm/src/commands/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ICommand } from './';
import { getAllChecksums } from '../utils/project_checksums';
import { BootstrapCacheFile } from '../utils/bootstrap_cache_file';
import { readYarnLock } from '../utils/yarn_lock';
import { assertNoProductionLodash3, assertSingleLodashV4 } from '../utils/validate_yarn_lock';
import { validateYarnLock } from '../utils/validate_yarn_lock';

export const BootstrapCommand: ICommand = {
description: 'Install dependencies and crosslink projects',
Expand Down Expand Up @@ -58,9 +58,7 @@ export const BootstrapCommand: ICommand = {

const yarnLock = await readYarnLock(kbn);

await assertSingleLodashV4(kbn, yarnLock);
await assertNoProductionLodash3(kbn, yarnLock);
log.success('yarn.lock analysis completed without any issues');
await validateYarnLock(kbn, yarnLock);

await linkProjectExecutables(projects, projectGraph);

Expand Down
77 changes: 40 additions & 37 deletions packages/kbn-pm/src/utils/validate_yarn_lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,74 @@ import { Kibana } from './kibana';
import { YarnLock } from './yarn_lock';
import { log } from './log';

export async function assertSingleLodashV4(kbn: Kibana, yarnLock: YarnLock) {
export async function validateYarnLock(kbn: Kibana, yarnLock: YarnLock) {
// look through all of the packages in the yarn.lock file to see if
// we have accidentally installed multiple lodash v4 versions
const lodash4Versions = new Set<string>();
const lodash4Reqs = new Set<string>();
for (const [req, dep] of Object.entries(yarnLock)) {
const chunks = req.split('@');
if (chunks[0] === 'lodash' && dep.version.startsWith('4.')) {
if (req.startsWith('lodash@') && dep.version.startsWith('4.')) {
lodash4Reqs.add(req);
lodash4Versions.add(dep.version);
}
}

if (lodash4Versions.size === 1) {
return;
}

for (const req of lodash4Reqs) {
delete yarnLock[req];
}
// if we find more than one lodash v4 version installed then delete
// lodash v4 requests from the yarn.lock file and prompt the user to
// retry bootstrap so that a single v4 version will be installed
if (lodash4Versions.size > 1) {
for (const req of lodash4Reqs) {
delete yarnLock[req];
}

await writeFile(kbn.getAbsolute('yarn.lock'), stringifyLockfile(yarnLock), 'utf8');
await writeFile(kbn.getAbsolute('yarn.lock'), stringifyLockfile(yarnLock), 'utf8');

log.error(dedent`
log.error(dedent`
Multiple version of lodash v4 were detected, so they have been removed
from the yarn.lock file. Please rerun yarn kbn bootstrap to coalese the
lodash versions installed.
Multiple version of lodash v4 were detected, so they have been removed
from the yarn.lock file. Please rerun yarn kbn bootstrap to coalese the
lodash versions installed.
If you still see this error when you re-bootstrap then you might need
to force a new dependency to use the latest version of lodash via the
"resolutions" field in package.json.
If you still see this error when you re-bootstrap then you might need
to force a new dependency to use the latest version of lodash via the
"resolutions" field in package.json.
If you have questions about this please reach out to the operations team.
If you have questions about this please reach out to the operations team.
`);
`);

process.exit(1);
}
process.exit(1);
}

export async function assertNoProductionLodash3(kbn: Kibana, yarnLock: YarnLock) {
// look through all the dependencies of production packages and production
// dependencies of those packages to determine if we're shipping any versions
// of lodash v3 in the distributable
const prodDependencies = kbn.resolveAllProductionDependencies(yarnLock, log);

const lodash3Versions = new Set<string>();
for (const dep of prodDependencies.values()) {
if (dep.name === 'lodash' && dep.version.startsWith('3.')) {
lodash3Versions.add(dep.version);
}
}

if (!lodash3Versions.size) {
return;
}
// if any lodash v3 packages were found we abort and tell the user to fix things
if (lodash3Versions.size) {
log.error(dedent`
log.error(dedent`
Due to changes in the yarn.lock file and/or package.json files a version of
lodash 3 is now included in the production dependencies. To reduce the size of
our distributable and especially our front-end bundles we have decided to
prevent adding any new instances of lodash 3.
Due to changes in the yarn.lock file and/or package.json files a version of
lodash 3 is now included in the production dependencies. To reduce the size of
our distributable and especially our front-end bundles we have decided to
prevent adding any new instances of lodash 3.
Please inspect the changes to yarn.lock or package.json files to identify where
the lodash 3 version is coming from and remove it.
Please inspect the changes to yarn.lock or package.json files to identify where
the lodash 3 version is coming from and remove it.
If you have questions about this please reack out to the operations team.
If you have questions about this please reack out to the operations team.
`);

`);
process.exit(1);
}

process.exit(1);
log.success('yarn.lock analysis completed without any issues');
}

0 comments on commit c2b46d7

Please sign in to comment.