From 3b3d3ab64e57f12d6d760f3525b0ecb2e9e4ab3f Mon Sep 17 00:00:00 2001 From: Dmitrii Filippov Date: Tue, 22 Dec 2020 17:38:50 +0100 Subject: [PATCH] Fix breaking changes introduced by Eslint v7 Change-Id: I84c3f4c333e9bbe0087738746e879494b90445f1 --- tools/js/BUILD | 2 +- tools/js/eslint-chdir.js | 30 ++++++++++++++++++++++++++++++ tools/js/eslint.bzl | 4 +++- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tools/js/eslint-chdir.js diff --git a/tools/js/BUILD b/tools/js/BUILD index fedaf7ffc4..1a272e238f 100644 --- a/tools/js/BUILD +++ b/tools/js/BUILD @@ -1 +1 @@ -exports_files(["run_npm_binary.py"]) +exports_files(["run_npm_binary.py", "eslint-chdir.js"]) diff --git a/tools/js/eslint-chdir.js b/tools/js/eslint-chdir.js new file mode 100644 index 0000000000..5aea7043d8 --- /dev/null +++ b/tools/js/eslint-chdir.js @@ -0,0 +1,30 @@ +/** + * @license + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed 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. + */ + +// Eslint 7 introduced a breaking change - it uses the current workdir instead +// of the configuration file directory for resolving relative paths: +// https://eslint.org/docs/user-guide/migrating-to-7.0.0#base-path-change +// This file is loaded before the eslint and sets the current directory +// back to the location of configuration file. + +const path = require('path'); +const configParamIndex = + process.argv.findIndex(arg => arg === '-c' || arg === '---config'); +if (configParamIndex >= 0 && configParamIndex + 1 < process.argv.length) { + const dirName = path.dirname(process.argv[configParamIndex + 1]); + process.chdir(dirName); +} diff --git a/tools/js/eslint.bzl b/tools/js/eslint.bzl index 586b1c590a..b32e2bc918 100644 --- a/tools/js/eslint.bzl +++ b/tools/js/eslint.bzl @@ -57,9 +57,11 @@ def eslint(name, plugins, srcs, config, ignore, extensions = [".js"], data = []) config, ignore, "//tools/js/eslint-rules:eslint-rules-srcs", + "//tools/js:eslint-chdir.js", eslint_rules_toplevel_file, ] + plugins + data common_templated_args = [ + "--node_options=--require=$$(rlocation $(rootpath //tools/js:eslint-chdir.js))", "--ext", ",".join(extensions), "-c", @@ -85,7 +87,7 @@ def eslint(name, plugins, srcs, config, ignore, extensions = [".js"], data = []) "*_test_require_patch.js", "--ignore-pattern", "*_test_loader.js", - native.package_name(), + "./", # Relative to the config file location ], # Should not run sandboxed. tags = [