Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lockfile symlinks #55440

Merged
merged 9 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/elastic-datemath/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-babel-code-parser/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-babel-preset/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-dev-utils/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-es/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-eslint-import-resolver-kibana/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-eslint-plugin-eslint/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-i18n/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-interpreter/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-plugin-generator/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-plugin-helpers/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-pm/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-spec-to-console/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-storybook/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-test/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-ui-framework/yarn.lock
1 change: 1 addition & 0 deletions packages/kbn-utility-types/yarn.lock
21 changes: 21 additions & 0 deletions scripts/check_lockfile_symlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

require('../src/setup_node_env');
require('../src/dev/run_check_lockfile_symlinks');
149 changes: 149 additions & 0 deletions src/dev/precommit_hook/check_lockfile_symlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { dirname } from 'path';
import { existsSync, lstatSync, readFileSync } from 'fs';

import { createFailError } from '@kbn/dev-utils';
import { matchesAnyGlob } from '../globs';

import { LOCKFILE_GLOBS, MANIFEST_GLOBS, IGNORE_FILE_GLOBS } from './lockfile_check_config';

function listPaths(paths) {
return paths.map(path => ` - ${path}`).join('\n');
}

async function checkProhibitedLockfiles(log, files) {
jportner marked this conversation as resolved.
Show resolved Hide resolved
jportner marked this conversation as resolved.
Show resolved Hide resolved
const errorPaths = [];

files
.filter(file => matchesAnyGlob(file.getRelativePath(), LOCKFILE_GLOBS))
.forEach(file => {
const path = file.getRelativePath();
const parent = dirname(path);
const stats = lstatSync(path);
if (!stats.isSymbolicLink() && parent !== '.') {
errorPaths.push(path);
}
});

if (errorPaths.length) {
throw createFailError(
`These directories MUST NOT have a 'yarn.lock' file:\n${listPaths(errorPaths)}`
);
}
}

async function checkSuperfluousSymlinks(log, files) {
const errorPaths = [];

files
.filter(file => matchesAnyGlob(file.getRelativePath(), LOCKFILE_GLOBS))
.forEach(file => {
const path = file.getRelativePath();
const parent = dirname(path);
const stats = lstatSync(path);
if (!stats.isSymbolicLink()) {
return;
}

const manifestPath = `${parent}/package.json`;
if (!existsSync(manifestPath)) {
log.warning(
`No manifest found at '${manifestPath}', but found an adjacent 'yarn.lock' symlink.`
);
errorPaths.push(path);
return;
}

try {
const manifest = readFileSync(manifestPath);
try {
const json = JSON.parse(manifest);
if (!json.dependencies || !Object.keys(json.dependencies).length) {
log.warning(
`Manifest at '${manifestPath}' has no dependencies, but found an adjacent 'yarn.lock' symlink.`
);
errorPaths.push(path);
}
} catch (err) {
log.warning(
`Could not parse JSON at '${manifestPath}', but found an adjacent 'yarn.lock' symlink.`
);
errorPaths.push(path);
}
} catch (err) {
log.warning(
`Could not read manifest at '${manifestPath}', but found an adjacent 'yarn.lock' symlink.`
);
errorPaths.push(path);
}
});

if (errorPaths.length) {
throw createFailError(
`These directories MUST NOT have a 'yarn.lock' symlink:\n${listPaths(errorPaths)}`
);
}
}

async function checkMissingSymlinks(log, files) {
const errorPaths = [];

files
.filter(file => matchesAnyGlob(file.getRelativePath(), MANIFEST_GLOBS))
.forEach(file => {
const path = file.getRelativePath();
const parent = dirname(path);
const lockfilePath = `${parent}/yarn.lock`;
if (existsSync(lockfilePath)) {
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to check if the file is a symlink, because if it's not, it will throw an error during checkProhibitedLockfiles. So, we assume if a lockfile exists here, it's a symlink and we don't need to proceed.

I thought about using fs.realpathSync to check and ensure that the symlink is pointing to the appropriate yarn.lock file in the project root directory, but it seemed unnecessary. Or, if we decide it is necessary, maybe we should put it in a separate method to ensure that the symlinks that exist are set appropriately.


try {
const manifest = readFileSync(path);
try {
const json = JSON.parse(manifest);
if (json.dependencies && Object.keys(json.dependencies).length) {
log.warning(
`Manifest at '${path}' has dependencies, but did not find an adjacent 'yarn.lock' symlink.`
);
errorPaths.push(`${parent}/yarn.lock`);
}
} catch (err) {
log.warning(`Could not parse JSON at '${path}'.`);
}
} catch (err) {
log.warning(`Could not read manifest at '${path}'.`);
}
});

if (errorPaths.length) {
throw createFailError(
`These directories MUST have a 'yarn.lock' symlink:\n${listPaths(errorPaths)}`
);
}
}

export async function checkLockfileSymlinks(log, files) {
const filtered = files.filter(file => !matchesAnyGlob(file.getRelativePath(), IGNORE_FILE_GLOBS));
await checkProhibitedLockfiles(log, filtered);
await checkSuperfluousSymlinks(log, filtered);
await checkMissingSymlinks(log, filtered);
}
1 change: 1 addition & 0 deletions src/dev/precommit_hook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
*/

export { checkFileCasing } from './check_file_casing';
export { checkLockfileSymlinks } from './check_lockfile_symlinks';
export { getFilesForCommit } from './get_files_for_commit';
31 changes: 31 additions & 0 deletions src/dev/precommit_hook/lockfile_check_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export const LOCKFILE_GLOBS = ['**/yarn.lock'];

export const MANIFEST_GLOBS = ['**/package.json'];

export const IGNORE_FILE_GLOBS = [
// tests aren't used in production, ignore them
'**/test/**/*',
// fixtures aren't used in production, ignore them
'**/*fixtures*/**/*',
// cypress isn't used in production, ignore it
'x-pack/legacy/plugins/apm/cypress/*',
];
45 changes: 45 additions & 0 deletions src/dev/run_check_lockfile_symlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import globby from 'globby';

import { run } from '@kbn/dev-utils';
import { File } from './file';
import { REPO_ROOT } from './constants';
import { checkLockfileSymlinks } from './precommit_hook';

run(async ({ log }) => {
const paths = await globby(['**/yarn.lock', '**/package.json'], {
cwd: REPO_ROOT,
nodir: true,
gitignore: true,
ignore: [
// the gitignore: true option makes sure that we don't
// include files from node_modules in the result, but it still
// loads all of the files from node_modules before filtering
// so it's still super slow. This prevents loading the files
// and still relies on gitignore to to final ignores
'**/node_modules',
],
});

const files = paths.map(path => new File(path));

await checkLockfileSymlinks(log, files);
});
8 changes: 7 additions & 1 deletion src/dev/run_precommit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { run, combineErrors } from '@kbn/dev-utils';
import * as Eslint from './eslint';
import * as Sasslint from './sasslint';
import { getFilesForCommit, checkFileCasing } from './precommit_hook';
import { getFilesForCommit, checkFileCasing, checkLockfileSymlinks } from './precommit_hook';

run(
async ({ log, flags }) => {
Expand All @@ -33,6 +33,12 @@ run(
errors.push(error);
}

try {
await checkLockfileSymlinks(log, files);
} catch (error) {
errors.push(error);
}

for (const Linter of [Eslint, Sasslint]) {
const filesToLint = Linter.pickFilesToLint(log, files);
if (filesToLint.length > 0) {
Expand Down
11 changes: 11 additions & 0 deletions tasks/config/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ module.exports = function(grunt) {
],
}),

// used by the test tasks
// runs the check_lockfile_symlinks script to ensure manifests with non-dev dependencies have adjacent lockfile symlinks
checkLockfileSymlinks: scriptWithGithubChecks({
title: 'Check lockfile symlinks',
cmd: NODE,
args: [
'scripts/check_lockfile_symlinks',
'--quiet', // only log errors, not warnings
],
}),

// used by the test tasks
// runs the check_core_api_changes script to ensure API changes are explictily accepted
checkCoreApiChanges: scriptWithGithubChecks({
Expand Down
1 change: 1 addition & 0 deletions tasks/jenkins.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function(grunt) {
'run:typeCheck',
'run:i18nCheck',
'run:checkFileCasing',
'run:checkLockfileSymlinks',
'run:licenses',
'run:verifyDependencyVersions',
'run:verifyNotice',
Expand Down
3 changes: 0 additions & 3 deletions x-pack/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,3 @@
!/legacy/plugins/infra/**/target
.cache
!/legacy/plugins/siem/**/target

# We don't want any yarn.lock files in here
/yarn.lock
watson marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/infra/yarn.lock
1 change: 1 addition & 0 deletions x-pack/legacy/plugins/siem/yarn.lock
1 change: 1 addition & 0 deletions x-pack/plugins/endpoint/yarn.lock
1 change: 1 addition & 0 deletions x-pack/yarn.lock