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

[7.x] chore(NA): ensure scripts run with --preserve-symlinks (#94046) #94636

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion packages/elastic-eslint-config-kibana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"bugs": {
"url": "https://github.com/elastic/kibana/tree/master/packages/elastic-eslint-config-kibana"
},
"homepage": "https://github.com/elastic/kibana/tree/master/packages/elastic-eslint-config-kibana"
"homepage": "https://github.com/elastic/kibana/tree/master/packages/elastic-eslint-config-kibana",
"dependencies": {
"@kbn/dev-utils": "link:../kbn-dev-utils"
}
}
4 changes: 2 additions & 2 deletions packages/elastic-eslint-config-kibana/react.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const semver = require('semver')
const PKG = require('../../package.json')
const semver = require('semver');
const { kibanaPackageJson: PKG } = require('@kbn/dev-utils');

module.exports = {
plugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/elastic-eslint-config-kibana/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// as this package was moved from typescript-eslint-parser to @typescript-eslint/parser

const semver = require('semver');
const PKG = require('../../package.json');
const { kibanaPackageJson: PKG } = require('@kbn/dev-utils');

const eslintConfigPrettierTypescriptEslintRules = require('eslint-config-prettier/@typescript-eslint').rules;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import Fs from 'fs';
import Path from 'path';

import normalizePath from 'normalize-path';
Expand Down Expand Up @@ -48,7 +49,7 @@ NOTE:
node scripts/build_plugin_list_docs

You can update the template within ${normalizePath(
Path.relative(REPO_ROOT, Path.resolve(__dirname, __filename))
Path.relative(REPO_ROOT, Fs.realpathSync(Path.resolve(__dirname, __filename)))
)}

////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ it('builds expected bundles, saves bundle counts to metadata', async () => {

expect(bar.cache.getReferencedFiles()).toMatchInlineSnapshot(`
Array [
<absolute path>/node_modules/@kbn/optimizer/postcss.config.js,
<absolute path>/node_modules/css-loader/package.json,
<absolute path>/node_modules/style-loader/package.json,
<absolute path>/packages/kbn-optimizer/postcss.config.js,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/kibana.json,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.scss,
<absolute path>/packages/kbn-optimizer/src/__fixtures__/__tmp__/mock_repo/plugins/bar/public/index.ts,
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-optimizer/src/optimizer/observe_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function usingWorkerProc<T>(
(): ProcResource => {
const proc = execa.node(require.resolve('../worker/run_worker'), [], {
nodeOptions: [
...process.execArgv,
...(inspectFlag && config.inspectWorkers
? [`${inspectFlag}=${inspectPortCounter++}`]
: []),
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
alias: {
core_app_image_assets: Path.resolve(worker.repoRoot, 'src/core/public/core_app/images'),
},
symlinks: false,
},

performance: {
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-pm/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {

resolve: {
extensions: ['.ts', '.js'],
symlinks: false,
},

module: {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-test/src/functional_tests/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { REPO_ROOT } from '@kbn/utils';
import { resolve, relative } from 'path';

// resolve() treats relative paths as relative to process.cwd(),
Expand All @@ -16,7 +17,7 @@ function resolveRelative(path) {

export const KIBANA_EXEC = 'node';
export const KIBANA_EXEC_PATH = resolveRelative('scripts/kibana');
export const KIBANA_ROOT = resolve(__dirname, '../../../../../');
export const KIBANA_ROOT = REPO_ROOT;
export const KIBANA_FTR_SCRIPT = resolve(KIBANA_ROOT, 'scripts/functional_test_runner');
export const PROJECT_ROOT = resolve(__dirname, '../../../../../../');
export const FUNCTIONAL_CONFIG_PATH = resolve(KIBANA_ROOT, 'test/functional/config');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ function collectCliArgs(config, { installDir, extraKbnOpts }) {
const buildArgs = config.get('kbnTestServer.buildArgs') || [];
const sourceArgs = config.get('kbnTestServer.sourceArgs') || [];
const serverArgs = config.get('kbnTestServer.serverArgs') || [];
const execArgv = process.execArgv || [];

return pipe(
serverArgs,
(args) => (installDir ? args.filter((a) => a !== '--oss') : args),
(args) => (installDir ? [...buildArgs, ...args] : [KIBANA_EXEC_PATH, ...sourceArgs, ...args]),
(args) =>
installDir
? [...buildArgs, ...args]
: [...execArgv, KIBANA_EXEC_PATH, ...sourceArgs, ...args],
(args) => args.concat(extraKbnOpts || [])
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-test/src/legacy_es/es_test_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* Side Public License, v 1.
*/

import { kibanaPackageJson as pkg } from '@kbn/dev-utils';
import url, { format as formatUrl } from 'url';
import pkg from '../../../../package.json';
import { adminTestUser } from '../kbn';

export const esTestConfig = new (class EsTestConfig {
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-test/src/mocha/junit_report_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { REPO_ROOT } from '@kbn/utils';
import { dirname, relative } from 'path';
import { writeFileSync, mkdirSync } from 'fs';
import { inspect } from 'util';
Expand All @@ -21,7 +22,7 @@ const dateNow = Date.now.bind(Date);
export function setupJUnitReportGeneration(runner, options = {}) {
const {
reportName = 'Unnamed Mocha Tests',
rootDirectory = dirname(require.resolve('../../../../package.json')),
rootDirectory = REPO_ROOT,
getTestMetadata = () => ({}),
} = options;

Expand Down
1 change: 1 addition & 0 deletions packages/kbn-ui-shared-deps/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ exports.getWebpackConfig = ({ dev = false } = {}) => ({
moment: MOMENT_SRC,
},
extensions: ['.js', '.ts'],
symlinks: false,
},

optimization: {
Expand Down
3 changes: 1 addition & 2 deletions packages/kbn-utils/src/repo_root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import Path from 'path';
import Fs from 'fs';

import loadJsonFile from 'load-json-file';

Expand All @@ -31,7 +30,7 @@ const findKibanaPackageJson = () => {
// search for the kibana directory, since this file is moved around it might
// not be where we think but should always be a relatively close parent
// of this directory
const startDir = Fs.realpathSync(__dirname);
const startDir = __dirname;
const { root: rootDir } = Path.parse(startDir);
let cursor = startDir;
while (true) {
Expand Down
1 change: 1 addition & 0 deletions scripts/build_kibana_platform_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');
require('@kbn/optimizer').runKbnOptimizerCli({
defaultLimitsPath: require.resolve('../packages/kbn-optimizer/limits.yml'),
});
1 change: 1 addition & 0 deletions scripts/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');
require('@kbn/test').runJest();
1 change: 1 addition & 0 deletions scripts/jest_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');
process.argv.push('--runInBand');

require('@kbn/test').runJest('jest.config.integration.js');
1 change: 1 addition & 0 deletions scripts/kbn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');
require('../src/setup_node_env/node_version_validator');
require('../packages/kbn-pm/cli');
1 change: 1 addition & 0 deletions scripts/precommit_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');
require('@kbn/optimizer').registerNodeAutoTranspilation();
require('../src/dev/run_precommit_hook');
2 changes: 2 additions & 0 deletions scripts/test_hardening.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

require('../src/setup_node_env/ensure_node_preserve_symlinks');

var execFileSync = require('child_process').execFileSync;
var path = require('path');
var syncGlob = require('glob').sync;
Expand Down
5 changes: 4 additions & 1 deletion src/dev/cli_dev_mode/dev_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ describe('#run$', () => {
"ELASTIC_APM_SERVICE_NAME": "kibana",
"isDevCliChild": "true",
},
"nodeOptions": Array [],
"nodeOptions": Array [
"--preserve-symlinks-main",
"--preserve-symlinks",
],
"stdio": "pipe",
},
],
Expand Down
63 changes: 63 additions & 0 deletions src/setup_node_env/ensure_node_preserve_symlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

(function () {
var cp = require('child_process');

var preserveSymlinksOption = '--preserve-symlinks';
var preserveSymlinksMainOption = '--preserve-symlinks-main';
var nodeOptions = (process && process.env && process.env.NODE_OPTIONS) || [];
var nodeExecArgv = (process && process.execArgv) || [];

var isPreserveSymlinksPresent =
nodeOptions.includes(preserveSymlinksOption) || nodeExecArgv.includes(preserveSymlinksOption);
var isPreserveSymlinksMainPresent =
nodeOptions.includes(preserveSymlinksMainOption) ||
nodeExecArgv.includes(preserveSymlinksMainOption);

if (isPreserveSymlinksPresent && isPreserveSymlinksMainPresent) {
return;
}

var nodeArgv = (process && process.argv) || [];
var isFirstArgNode = nodeArgv.length > 0 && nodeArgv[0].includes('node') ? nodeArgv[0] : null;
if (!isFirstArgNode) {
return;
}

var missingNodeArgs = [];
if (!isPreserveSymlinksMainPresent) {
missingNodeArgs.push(preserveSymlinksMainOption);
}

if (!isPreserveSymlinksPresent) {
missingNodeArgs.push(preserveSymlinksOption);
}

var nodeArgs = nodeExecArgv.concat(missingNodeArgs);
var restArgs = nodeArgv.length >= 2 ? nodeArgv.slice(1, nodeArgv.length) : [];

var getExitCodeFromSpawnResult = function (spawnResult) {
if (spawnResult.status !== null) {
return spawnResult.status;
}

if (spawnResult.signal !== null) {
return 128 + spawnResult.signal;
}

if (spawnResult.error) {
return 1;
}

return 0;
};

var spawnResult = cp.spawnSync(nodeArgv[0], nodeArgs.concat(restArgs), { stdio: 'inherit' });
process.exit(getExitCodeFromSpawnResult(spawnResult));
})();
2 changes: 2 additions & 0 deletions src/setup_node_env/no_transpilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

require('./ensure_node_preserve_symlinks');

// The following require statements MUST be executed before any others - BEGIN
require('./exit_on_warning');
require('./harden');
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
// Resolve modules in the same way as Node.js. Aka make `require` works the
// same in TypeScript as it does in Node.js.
"moduleResolution": "node",
// Do not resolve the real path of symlinks
"preserveSymlinks": true,
// "resolveJsonModule" allows for importing, extracting types from and generating .json files.
"resolveJsonModule": true,
// Disallow inconsistently-cased references to the same file.
Expand Down
4 changes: 1 addition & 3 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"kbn": "node ../scripts/kbn",
"kbn:bootstrap": "node plugins/canvas/scripts/storybook --clean",
"start": "node ../scripts/kibana --dev",
"build": "../node_modules/.bin/gulp build",
"testonly": "echo 'Deprecated, use `yarn test`'",
"test": "../node_modules/.bin/gulp test",
"build": "node --preserve-symlinks ../node_modules/.bin/gulp build",
"test:jest": "node ../scripts/jest"
},
"kibana": {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/apm/e2e/cypress/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module.exports = {
resolve: {
extensions: ['.ts', '.js'],
symlinks: false,
},
node: { fs: 'empty', child_process: 'empty', readline: 'empty' },
module: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/shareable_runtime/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
core_app_image_assets: path.resolve(KIBANA_ROOT, 'src/core/public/core_app/images'),
},
extensions: ['.js', '.json', '.ts', '.tsx', '.scss'],
symlinks: false,
},
module: {
rules: [
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ module.exports = async ({ config: storybookConfig }) => {
'../tasks/mocks/uiAbsoluteToParsedUrl'
),
},
symlinks: false,
},
};

Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/canvas/storybook/webpack.dll.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module.exports = {
alias: {
ui: path.resolve(KIBANA_ROOT, 'src/legacy/ui/public'),
},
symlinks: false,
},
module: {
rules: [
Expand Down
16 changes: 10 additions & 6 deletions x-pack/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ async function copySourceAndBabelify() {
}

async function buildCanvasShareableRuntime() {
await execa(process.execPath, ['plugins/canvas/scripts/shareable_runtime'], {
cwd: XPACK_DIR,
stdio: ['ignore', 'inherit', 'inherit'],
// @ts-ignore Incorrect @types - execa supports `buffer`
buffer: false,
});
await execa(
process.execPath,
['--preserve-symlinks', 'plugins/canvas/scripts/shareable_runtime'],
{
cwd: XPACK_DIR,
stdio: ['ignore', 'inherit', 'inherit'],
// @ts-ignore Incorrect @types - execa supports `buffer`
buffer: false,
}
);
}

async function generateNoticeText() {
Expand Down