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

[interpreter/loadPlugins] avoid deleting globals added by plugins #27171

Merged
merged 8 commits into from
Dec 28, 2018
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
1 change: 1 addition & 0 deletions packages/kbn-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "6.20.0",
"css-loader": "1.0.0",
"copy-webpack-plugin": "^4.6.0",
"del": "^3.0.0",
"getopts": "^2.2.3",
"pegjs": "0.9.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/kbn-interpreter/src/plugin/functions/browser/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 '../common/register';
8 changes: 1 addition & 7 deletions packages/kbn-interpreter/src/server/server_registries.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ export const populateServerRegistries = types => {
const remainingTypes = types;
const populatedTypes = {};

const globalKeys = Object.keys(global);

const loadType = () => {
const type = remainingTypes.pop();
getPluginPaths(type).then(paths => {
Expand All @@ -60,11 +58,7 @@ export const populateServerRegistries = types => {
require(path);
});

Object.keys(global).forEach(key => {
if (!globalKeys.includes(key)) {
delete global[key];
}
});
delete global.canvas;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some documentation to this logic, to make it clear why we're adding, registering and then deleting the global?

Are there any other globals we should consider deleting? Forgive the terminology, but perhaps a registry of globals to delete would help, even if it's a bit derivative...?

Copy link
Contributor

Choose a reason for hiding this comment

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

We add the global for plugins to use. They all use canvas.register, so the global needs to exist when the code is run (ie. when require(path) is run).

My understanding is that it's removed to avoid complaints from our test runners, which check for added globals in the node runtime and fail if any are unexpectedly added.


populatedTypes[type] = registries[type];
if (remainingTypes.length) loadType();
Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-interpreter/tasks/build/__fixtures__/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-disable */
import util from 'util';
console.log(util.format('hello world'));
43 changes: 43 additions & 0 deletions packages/kbn-interpreter/tasks/build/server_code_transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.
*/

const { extname } = require('path');

const { transform } = require('babel-core');

exports.createServerCodeTransformer = (sourceMaps) => {
return (content, path) => {
switch (extname(path)) {
case '.js':
const { code = '' } = transform(content.toString('utf8'), {
filename: path,
ast: false,
code: true,
sourceMaps: sourceMaps ? 'inline' : false,
babelrc: false,
presets: [require.resolve('@kbn/babel-preset/webpack_preset')],
});

return code;

default:
return content.toString('utf8');
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { readFileSync } from 'fs';
import { resolve } from 'path';
import { createServerCodeTransformer } from './server_code_transformer';

const JS_FIXTURE_PATH = resolve(__dirname, '__fixtures__/sample.js');
const JS_FIXTURE = readFileSync(JS_FIXTURE_PATH);

describe('js support', () => {
it('transpiles js file', () => {
const transformer = createServerCodeTransformer();
expect(transformer(JS_FIXTURE, JS_FIXTURE_PATH)).toMatchInlineSnapshot(`
"'use strict';

var _util = require('util');

var _util2 = _interopRequireDefault(_util);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

console.log(_util2.default.format('hello world')); /* eslint-disable */"
`);
});

it('throws errors for js syntax errors', () => {
const transformer = createServerCodeTransformer();
expect(() => transformer(Buffer.from(`export default 'foo`), JS_FIXTURE_PATH)).toThrowError(
/Unterminated string constant/
);
});
});
15 changes: 14 additions & 1 deletion packages/kbn-interpreter/tasks/build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/

const { resolve } = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const { createServerCodeTransformer } = require('./server_code_transformer');

const {
PLUGIN_SOURCE_DIR,
PLUGIN_BUILD_DIR,
Expand All @@ -31,7 +35,7 @@ module.exports = function ({ sourceMaps }, { watch }) {
mode: 'none',
entry: {
'types/all': resolve(PLUGIN_SOURCE_DIR, 'types/register.js'),
'functions/common/all': resolve(PLUGIN_SOURCE_DIR, 'functions/common/register.js'),
'functions/browser/all': resolve(PLUGIN_SOURCE_DIR, 'functions/browser/register.js'),
},

// there were problems with the node and web targets since this code is actually
Expand Down Expand Up @@ -95,6 +99,15 @@ module.exports = function ({ sourceMaps }, { watch }) {
stats: 'errors-only',

plugins: [
new CopyWebpackPlugin([
{
from: resolve(PLUGIN_SOURCE_DIR, 'functions/common'),
to: resolve(PLUGIN_BUILD_DIR, 'functions/common'),
ignore: '**/__tests__/**',
transform: createServerCodeTransformer(sourceMaps)
},
]),

function LoaderFailHandlerPlugin() {
if (!watch) {
return;
Expand Down
2 changes: 2 additions & 0 deletions src/dev/mocha/run_mocha_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function runMochaCli() {

// check that we aren't leaking any globals
process.argv.push('--check-leaks');
// prevent globals injected from canvas plugins from triggering leak check
process.argv.push('--globals', 'core,regeneratorRuntime,_');
Copy link
Contributor

Choose a reason for hiding this comment

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

Kind of fragile, but at least future globals will break the CI.

I do wonder, is there a way to remove that lodash global?


// ensure that mocha requires the setup_node_env script
process.argv.push('--require', require.resolve('../../setup_node_env'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { functions } from './functions';
const basePath = chrome.getBasePath();

const types = {
commonFunctions: functionsRegistry,
browserFunctions: functionsRegistry,
types: typesRegistry
};
Expand Down
5 changes: 2 additions & 3 deletions src/setup_node_env/babel_register/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ var ignore = [
// is `x-pack` and `b` is not `node_modules`
/\/node_modules\/(?!x-pack\/(?!node_modules)([^\/]+))([^\/]+\/[^\/]+)/,

// ignore paths matching `/canvas/canvas_plugin/{a}/{b}` unless
// `a` is `functions` and `b` is `server`
/\/canvas\/canvas_plugin\/(?!functions\/server)([^\/]+\/[^\/]+)/,
// ignore paths matching `/canvas/canvas_plugin/`
/\/canvas\/canvas_plugin\//,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

By sharing the server_code_transformer with @kbn/interpreter I was able to pre-transpile the server code in the canvas_plugin_src and remove this customization.

];

if (global.__BUILT_WITH_BABEL__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { functions as commonFunctions } from '../common';
import { browser } from './browser';
import { location } from './location';
import { urlparam } from './urlparam';
import { markdown } from './markdown';

export const functions = [browser, location, urlparam, markdown];
export const functions = [browser, location, urlparam, markdown, ...commonFunctions];
21 changes: 18 additions & 3 deletions x-pack/plugins/canvas/tasks/helpers/webpack.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const {
createServerCodeTransformer,
} = require('@kbn/interpreter/tasks/build/server_code_transformer');
Copy link
Contributor

Choose a reason for hiding this comment

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

The interpreter seems like a weird place to import build configuration from. It's also weird that it's importing something that wouldn't be part of the published package.

Do you think putting this in another package (currently existing or not) would make more sense?

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 really don't want to have hundreds of packages in two years, so no to another package. I think this is fine, we don't publish these packages and if we want to we can figure something out then.


const sourceDir = path.resolve(__dirname, '../../canvas_plugin_src');
const buildDir = path.resolve(__dirname, '../../canvas_plugin');
Expand All @@ -25,7 +28,6 @@ export function getWebpackConfig({ devtool, watch } = {}) {
'uis/datasources/all': path.join(sourceDir, 'uis/datasources/register.js'),
'uis/arguments/all': path.join(sourceDir, 'uis/arguments/register.js'),
'functions/browser/all': path.join(sourceDir, 'functions/browser/register.js'),
'functions/common/all': path.join(sourceDir, 'functions/common/register.js'),
'templates/all': path.join(sourceDir, 'templates/register.js'),
'uis/tags/all': path.join(sourceDir, 'uis/tags/register.js'),
},
Expand Down Expand Up @@ -74,8 +76,21 @@ export function getWebpackConfig({ devtool, watch } = {}) {
},
new CopyWebpackPlugin([
{
from: `${sourceDir}/functions/server/`,
to: `${buildDir}/functions/server/`,
from: path.resolve(sourceDir, 'functions/server'),
to: path.resolve(buildDir, 'functions/server'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
{
from: path.resolve(sourceDir, 'functions/common'),
to: path.resolve(buildDir, 'functions/common'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
{
from: path.resolve(sourceDir, 'lib'),
to: path.resolve(buildDir, 'lib'),
transform: createServerCodeTransformer(!!devtool),
ignore: '**/__tests__/**',
},
]),
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5986,6 +5986,20 @@ copy-webpack-plugin@^4.5.2:
p-limit "^1.0.0"
serialize-javascript "^1.4.0"

copy-webpack-plugin@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-4.6.0.tgz#e7f40dd8a68477d405dd1b7a854aae324b158bae"
integrity sha512-Y+SQCF+0NoWQryez2zXn5J5knmr9z/9qSQt7fbL78u83rxmigOy8X5+BFn8CFSuX+nKT8gpYwJX68ekqtQt6ZA==
dependencies:
cacache "^10.0.4"
find-cache-dir "^1.0.0"
globby "^7.1.1"
is-glob "^4.0.0"
loader-utils "^1.1.0"
minimatch "^3.0.4"
p-limit "^1.0.0"
serialize-javascript "^1.4.0"

core-js@^1.0.0:
version "1.2.7"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-1.2.7.tgz#652294c14651db28fa93bd2d5ff2983a4f08c636"
Expand Down