From 035f921b533371643bd672d34112e45795dd19d2 Mon Sep 17 00:00:00 2001 From: John McLear Date: Sun, 29 Nov 2026 14:32:27 +0000 Subject: [PATCH 1/2] fix(plugins): keep checkPlugin from undoing the ESLint 9 migration The 28 Nov sync put .eslintrc.cjs back into ep_hljs and ep_dynamic_default_content, dropped their TypeScript 7 Dependabot ignore, and wrote a lint.yml whose `version: 10` clashes with a plugin packageManager pin ("Multiple versions of pnpm specified"). - Replace lib/eslintrc.cjs with lib/eslint.config.mjs. A plugin with a flat config keeps it and loses any leftover .eslintrc.*; one with only the stock .eslintrc.cjs, or none, gets the flat config and ESLint 9. A customised .eslintrc.* is only warned about. - Default devDependencies are eslint ^9.39.5 and eslint-config-etherpad ^5.0.4. - lib/dependabot.yml keeps typescript below 7. - lint.yml takes pnpm from core's packageManager (sparse checkout of core's package.json), like the test workflows, so no template pins a pnpm version. checkPlugin drops a plugin's own pnpm packageManager pin. - ep_dynamic_default_content no longer needs its own lint.yml. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TcYAoDMav4BPPE9JrMtSvw --- bin/plugins/README.md | 10 +++ bin/plugins/checkPlugin.ts | 43 ++++++---- bin/plugins/lib/dependabot.yml | 4 + bin/plugins/lib/eslint.config.mjs | 3 + bin/plugins/lib/eslintrc.cjs | 9 --- bin/plugins/lib/lint.yml | 21 ++++- bin/plugins/lib/npmpublish.yml | 3 + bin/plugins/pluginLint.ts | 108 +++++++++++++++++++++++++ src/tests/backend/specs/checkPlugin.ts | 100 +++++++++++++++++++++++ 9 files changed, 272 insertions(+), 29 deletions(-) create mode 100644 bin/plugins/lib/eslint.config.mjs delete mode 100644 bin/plugins/lib/eslintrc.cjs create mode 100644 bin/plugins/pluginLint.ts diff --git a/bin/plugins/README.md b/bin/plugins/README.md index bf1da7992b7..f8ec7069c71 100755 --- a/bin/plugins/README.md +++ b/bin/plugins/README.md @@ -70,6 +70,16 @@ listed in `SKIP_REPOS` in the workflow. checkPlugin does not bump the plugin version; the plugin's `npmpublish.yml` does that when the push lands. +Plugins lint with ESLint 9 and a flat config. checkPlugin keeps a plugin's own +`eslint.config.*`, deletes a leftover `.eslintrc.*` next to it (ESLint 9 +ignores those), and moves a plugin that only has the stock `.eslintrc.cjs`, or +no config at all, to `lib/eslint.config.mjs`. A customised `.eslintrc.*` gets a +warning and has to be migrated by hand. + +Every workflow template gets pnpm from core's `packageManager` field, so plugin +CI runs the same pnpm as core and no template sets a pnpm `version:`. A +plugin's own `packageManager: pnpm@...` pin is removed for the same reason. + ``` getCorePlugins.sh updateCorePlugins.sh diff --git a/bin/plugins/checkPlugin.ts b/bin/plugins/checkPlugin.ts index d05c8a119cc..fd951e8b8b2 100644 --- a/bin/plugins/checkPlugin.ts +++ b/bin/plugins/checkPlugin.ts @@ -21,6 +21,7 @@ import childProcess from 'node:child_process'; import log4js from 'log4js'; import path from 'node:path'; import semver from "semver"; +import {FLAT_CONFIG_DEV_DEPS, dropPnpmPin, syncEslintConfig} from './pluginLint'; // Template files that some plugins deliberately customise. checkPlugin leaves // these alone instead of overwriting them with the copy in bin/plugins/lib/. @@ -44,8 +45,8 @@ const KEEP_FILES: {[plugin: string]: string[]} = { ep_who_did_what: ['.github/workflows/frontend-tests.yml', '.github/workflows/backend-tests.yml'], // Installs ep_file_menu_toolbar, whose Edit menu this plugin extends. ep_copy_paste_select_all: ['.github/workflows/frontend-tests.yml'], - // Runs core from the workspace root and pins pnpm via packageManager. - ep_dynamic_default_content: ['.github/workflows/frontend-tests.yml', '.github/workflows/lint.yml'], + // Runs core from the workspace root. + ep_dynamic_default_content: ['.github/workflows/frontend-tests.yml'], // Builds the admin SPA for the /admin access specs. ep_openid_connect: ['.github/workflows/backend-tests.yml'], }; @@ -248,9 +249,10 @@ log4js.configure({ await updateDeps(parsedPackageJSON, 'devDependencies', { // Match the plugin fleet, but never rewrite an existing pin: some plugins // are already on newer majors (e.g. eslint 10), and changing the - // TypeScript version can break plugin code. - 'eslint': {ver: '^8.57.1', overwrite: false}, - 'eslint-config-etherpad': {ver: '^5.0.0', overwrite: false}, + // TypeScript version can break plugin code. A plugin moved to the flat + // config below gets at least these versions. + 'eslint': {ver: FLAT_CONFIG_DEV_DEPS.eslint, overwrite: false}, + 'eslint-config-etherpad': {ver: FLAT_CONFIG_DEV_DEPS['eslint-config-etherpad'], overwrite: false}, 'typescript': {ver: '~6.0.3', overwrite: false}, // These were moved to eslint-config-etherpad's dependencies so they can be removed: '@typescript-eslint/eslint-plugin': null, @@ -292,19 +294,26 @@ log4js.configure({ delete parsedPackageJSON.eslintConfig; await writePackageJson(parsedPackageJSON); } - if (files.includes('.eslintrc.js')) { - const [from, to] = [`${pluginPath}/.eslintrc.js`, `${pluginPath}/.eslintrc.cjs`]; - if (!files.includes('.eslintrc.cjs')) { - if (autoFix) { - await fsp.rename(from, to); - } else { - logger.warn(`please rename ${from} to ${to}`); - } - } else { - logger.error(`both ${from} and ${to} exist; delete ${from}`); + // ESLint 9 only reads flat configs (eslint.config.*). Never write a legacy + // .eslintrc.cjs: ESLint 9 ignores it, and it confuses anyone reading the + // plugin. + const {wroteFlatConfig} = syncEslintConfig( + pluginPath, await fsp.readFile('bin/plugins/lib/eslint.config.mjs', 'utf8'), autoFix, logger); + if (wroteFlatConfig) { + // lib/eslint.config.mjs needs ESLint 9 and eslint-config-etherpad 5.0.4. + const devDeps = parsedPackageJSON.devDependencies || {}; + const want: {[pkg: string]: string} = {}; + for (const [pkg, ver] of Object.entries(FLAT_CONFIG_DEV_DEPS)) { + const min = semver.minVersion(ver)!; + const got = devDeps[pkg] && semver.validRange(devDeps[pkg]) ? semver.minVersion(devDeps[pkg]) : null; + if (got == null || semver.lt(got, min)) want[pkg] = ver; } - } else { - await checkFile('bin/plugins/lib/eslintrc.cjs', '.eslintrc.cjs', false); + await updateDeps(parsedPackageJSON, 'devDependencies', want); + } + + // Plugin CI installs core's pnpm (see lib/*.yml), so drop a plugin's own pin. + if (dropPnpmPin(parsedPackageJSON, autoFix, logger) && autoFix) { + await writePackageJson(parsedPackageJSON); } if (checkEntries(parsedPackageJSON, { diff --git a/bin/plugins/lib/dependabot.yml b/bin/plugins/lib/dependabot.yml index 1c4bd803fdc..b1468f79c28 100644 --- a/bin/plugins/lib/dependabot.yml +++ b/bin/plugins/lib/dependabot.yml @@ -9,3 +9,7 @@ updates: schedule: interval: "daily" versioning-strategy: "increase" + ignore: + # typescript-eslint has no release that supports TypeScript 7 yet. + - dependency-name: "typescript" + versions: [">=7"] diff --git a/bin/plugins/lib/eslint.config.mjs b/bin/plugins/lib/eslint.config.mjs new file mode 100644 index 00000000000..9857937c404 --- /dev/null +++ b/bin/plugins/lib/eslint.config.mjs @@ -0,0 +1,3 @@ +import etherpad from 'eslint-config-etherpad/flat/plugin.js'; + +export default etherpad; diff --git a/bin/plugins/lib/eslintrc.cjs b/bin/plugins/lib/eslintrc.cjs deleted file mode 100644 index 6c5a92652e1..00000000000 --- a/bin/plugins/lib/eslintrc.cjs +++ /dev/null @@ -1,9 +0,0 @@ -'use strict'; - -// This is a workaround for https://github.com/eslint/eslint/issues/3458 -require('eslint-config-etherpad/patch/modern-module-resolution'); - -module.exports = { - root: true, - extends: 'etherpad/plugin', -}; diff --git a/bin/plugins/lib/lint.yml b/bin/plugins/lib/lint.yml index f38a74a05ff..debb25be707 100644 --- a/bin/plugins/lib/lint.yml +++ b/bin/plugins/lib/lint.yml @@ -7,13 +7,24 @@ jobs: lint: name: ESLint runs-on: ubuntu-latest - # `eslint .` executes .eslintrc.cjs and every installed ESLint plugin, so + # `eslint .` executes eslint.config.mjs and every installed ESLint plugin, so # don't hand it the caller's contents:write / id-token:write token. permissions: contents: read steps: - name: Check out plugin repository uses: actions/checkout@v7 + with: + path: plugin + # Only core's package.json is needed: its packageManager field sets the + # pnpm version, the same one the backend and frontend test workflows use. + - name: Check out Etherpad core's package.json + uses: actions/checkout@v7 + with: + repository: ether/etherpad + path: core + sparse-checkout: /package.json + sparse-checkout-cone-mode: false - uses: actions/setup-node@v7 name: Install Node.js with: @@ -21,7 +32,9 @@ jobs: - uses: pnpm/action-setup@v6.1.1 name: Install pnpm with: - version: 10 + # No `version:`: pnpm comes from core's packageManager, so it follows + # core and never clashes with a version input. + package_json_file: ./core/package.json run_install: false - name: Get pnpm store directory shell: bash @@ -30,10 +43,12 @@ jobs: name: Setup pnpm cache with: path: ${{ env.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('plugin/pnpm-lock.yaml') }} restore-keys: | ${{ runner.os }}-pnpm-store- - name: Install dependencies + working-directory: plugin run: pnpm install --ignore-scripts - name: Run ESLint + working-directory: plugin run: pnpm run lint diff --git a/bin/plugins/lib/npmpublish.yml b/bin/plugins/lib/npmpublish.yml index efe554a7996..08c530ef5b8 100644 --- a/bin/plugins/lib/npmpublish.yml +++ b/bin/plugins/lib/npmpublish.yml @@ -38,6 +38,9 @@ jobs: - uses: pnpm/action-setup@v6.1.1 name: Install pnpm with: + # No `version:`: the core checkout above puts core's package.json at + # the workspace root, so pnpm comes from core's packageManager, like + # every other plugin workflow. run_install: false - name: Get pnpm store directory shell: bash diff --git a/bin/plugins/pluginLint.ts b/bin/plugins/pluginLint.ts new file mode 100644 index 00000000000..a01fe4da562 --- /dev/null +++ b/bin/plugins/pluginLint.ts @@ -0,0 +1,108 @@ +// Helpers for the parts of checkPlugin.ts that decide a plugin's ESLint config +// and pnpm pin. They only touch the files they are asked about, so the backend +// tests can run them against a scratch directory without installing anything. + +import fs from 'node:fs'; +import path from 'node:path'; + +// File names ESLint 9 reads as a flat config. +export const FLAT_CONFIG_NAMES = [ + 'eslint.config.js', 'eslint.config.mjs', 'eslint.config.cjs', + 'eslint.config.ts', 'eslint.config.mts', 'eslint.config.cts', +]; + +// Legacy (eslintrc) config file names. ESLint 9 ignores all of them. +export const LEGACY_CONFIG_NAMES = [ + '.eslintrc', '.eslintrc.js', '.eslintrc.cjs', '.eslintrc.json', '.eslintrc.yml', '.eslintrc.yaml', +]; + +// The .eslintrc.cjs that checkPlugin used to write into every plugin. A plugin +// whose legacy config is exactly this can be moved to the flat config with no +// loss; anything else was customised and has to be migrated by hand. +export const STOCK_LEGACY_CONFIG = `'use strict'; + +// This is a workaround for https://github.com/eslint/eslint/issues/3458 +require('eslint-config-etherpad/patch/modern-module-resolution'); + +module.exports = { + root: true, + extends: 'etherpad/plugin', +}; +`; + +// devDependencies a plugin needs for the flat config in lib/eslint.config.mjs. +export const FLAT_CONFIG_DEV_DEPS = { + 'eslint': '^9.39.5', + 'eslint-config-etherpad': '^5.0.4', +}; + +type Logger = {info: (m: string) => void, warn: (m: string) => void}; + +export type EslintConfigResult = { + // Files that were (or, without autofix, would be) removed. + removed: string[], + // True when eslint.config.mjs was (or would be) written from the template. + wroteFlatConfig: boolean, +}; + +const normalize = (s: string) => s.replace(/\r\n/g, '\n').trim(); + +// Make sure the plugin uses an ESLint flat config: +// - a plugin that already has one keeps it, and any leftover legacy config is +// removed because ESLint 9 ignores it; +// - a plugin with only the stock legacy config, or none at all, gets +// lib/eslint.config.mjs; +// - a customised legacy config is left alone with a warning. +// The caller bumps the ESLint devDependencies when wroteFlatConfig is true. +export const syncEslintConfig = ( + pluginPath: string, flatTemplate: string, autoFix: boolean, logger: Logger, +): EslintConfigResult => { + const exists = (f: string) => fs.existsSync(path.join(pluginPath, f)); + const flat = FLAT_CONFIG_NAMES.filter(exists); + const legacy = LEGACY_CONFIG_NAMES.filter(exists); + const result: EslintConfigResult = {removed: [], wroteFlatConfig: false}; + + const remove = (f: string, why: string) => { + logger.warn(`${f} ${why}; ${autoFix ? 'removing it' : 'please remove it'}`); + if (autoFix) fs.rmSync(path.join(pluginPath, f)); + result.removed.push(f); + }; + + if (flat.length > 0) { + for (const f of legacy) remove(f, `is ignored because ${flat[0]} exists`); + return result; + } + + const custom = legacy.filter((f) => normalize( + fs.readFileSync(path.join(pluginPath, f), 'utf8')) !== normalize(STOCK_LEGACY_CONFIG)); + if (custom.length > 0) { + logger.warn(`${custom.join(', ')} is a customised legacy ESLint config; ` + + 'move it to eslint.config.mjs by hand (ESLint 9 ignores it)'); + return result; + } + + for (const f of legacy) remove(f, 'is the stock legacy ESLint config'); + logger.warn(`eslint.config.mjs not found; ${autoFix ? 'adding' : 'please add'} the template copy`); + if (autoFix) fs.writeFileSync(path.join(pluginPath, 'eslint.config.mjs'), flatTemplate); + result.wroteFlatConfig = true; + return result; +}; + +// Plugin CI takes its pnpm version from core's package.json, so a plugin's own +// pnpm pin only makes the plugin run an older (or different) pnpm than core. +// Returns true if the pin was found (and, with autoFix, removed from pkg). +export const dropPnpmPin = (pkg: any, autoFix: boolean, logger: Logger): boolean => { + let found = false; + if (typeof pkg.packageManager === 'string' && pkg.packageManager.startsWith('pnpm@')) { + logger.warn(`package.json pins ${pkg.packageManager} via packageManager; plugin CI uses ` + + `core's pnpm, so ${autoFix ? 'removing it' : 'please remove it'}`); + if (autoFix) delete pkg.packageManager; + found = true; + } + if (pkg.devEngines?.packageManager?.name === 'pnpm') { + // devEngines can carry other settings, so only point it out. + logger.warn('package.json pins pnpm via devEngines.packageManager; plugin CI uses ' + + "core's pnpm, please remove it by hand"); + } + return found; +}; diff --git a/src/tests/backend/specs/checkPlugin.ts b/src/tests/backend/specs/checkPlugin.ts index 1074787b9db..43adfd984e1 100644 --- a/src/tests/backend/specs/checkPlugin.ts +++ b/src/tests/backend/specs/checkPlugin.ts @@ -9,7 +9,11 @@ import {strict as assert} from 'assert'; import * as childProcess from 'child_process'; import * as fs from 'fs'; +import * as os from 'os'; import * as path from 'path'; +import { + STOCK_LEGACY_CONFIG, dropPnpmPin, syncEslintConfig, +} from '../../../../bin/plugins/pluginLint'; const REPO_ROOT = path.resolve(__dirname, '..', '..', '..', '..'); const BIN_DIR = path.join(REPO_ROOT, 'bin'); @@ -59,3 +63,99 @@ describe(__filename, function () { assert.equal(git(remote, 'rev-parse', 'main'), before); }); }); + +// The plugin fleet is on ESLint 9 with a flat config (eslint.config.mjs). The +// daily sync must not put back the legacy .eslintrc.cjs, the TypeScript 7 +// Dependabot ignore, or a pnpm setup that clashes with the plugin. +describe(`${__filename} templates`, function () { + const LIB = path.join(REPO_ROOT, 'bin', 'plugins', 'lib'); + const read = (f: string) => fs.readFileSync(path.join(LIB, f), 'utf8'); + const silent = {info: () => {}, warn: () => {}}; + let dir: string; + + beforeEach(function () { + dir = fs.mkdtempSync(path.join(os.tmpdir(), 'checkPlugin-eslint-')); + }); + + afterEach(function () { + fs.rmSync(dir, {recursive: true, force: true}); + }); + + it('ships a flat ESLint config and no legacy one', function () { + assert.match(read('eslint.config.mjs'), /eslint-config-etherpad\/flat\/plugin\.js/); + assert.equal(fs.existsSync(path.join(LIB, 'eslintrc.cjs')), false); + }); + + it('dependabot.yml keeps TypeScript below 7', function () { + assert.match(read('dependabot.yml'), + /- dependency-name: "typescript"\s+versions: \[">=7"\]/); + }); + + it('no workflow pins a pnpm version', function () { + for (const f of fs.readdirSync(LIB).filter((f) => f.endsWith('.yml'))) { + const yml = read(f); + const blocks = yml.split(/\n(?=\s*-)/).filter((b) => b.includes('pnpm/action-setup')); + for (const b of blocks) { + // `version:` together with a packageManager field fails with + // "Multiple versions of pnpm specified". + assert.doesNotMatch(b, /^\s*version:/m, `${f} pins pnpm:\n${b}`); + } + } + }); + + it('lint.yml takes pnpm from core, not from the plugin', function () { + const yml = read('lint.yml'); + assert.match(yml, /repository: ether\/etherpad\n\s+path: core\n/); + assert.match(yml, /package_json_file: \.\/core\/package\.json/); + assert.match(yml, /working-directory: plugin\n\s+run: pnpm run lint/); + }); + + it('keeps an existing flat config and removes a leftover .eslintrc.cjs', function () { + fs.writeFileSync(path.join(dir, 'eslint.config.mjs'), 'export default [];\n'); + fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG); + const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); + assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: false}); + assert.equal(fs.readFileSync(path.join(dir, 'eslint.config.mjs'), 'utf8'), + 'export default [];\n'); + assert.equal(fs.existsSync(path.join(dir, '.eslintrc.cjs')), false); + }); + + it('moves a stock legacy config to the flat config', function () { + fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG); + const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); + assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: true}); + assert.equal(fs.readFileSync(path.join(dir, 'eslint.config.mjs'), 'utf8'), + read('eslint.config.mjs')); + assert.equal(fs.existsSync(path.join(dir, '.eslintrc.cjs')), false); + }); + + it('adds the flat config to a plugin with no ESLint config', function () { + const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); + assert.deepEqual(res, {removed: [], wroteFlatConfig: true}); + assert.deepEqual(fs.readdirSync(dir), ['eslint.config.mjs']); + }); + + it('leaves a customised legacy config alone', function () { + const custom = `${STOCK_LEGACY_CONFIG}module.exports.rules = {};\n`; + fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), custom); + const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); + assert.deepEqual(res, {removed: [], wroteFlatConfig: false}); + assert.deepEqual(fs.readdirSync(dir), ['.eslintrc.cjs']); + }); + + it('changes nothing without autofix', function () { + fs.writeFileSync(path.join(dir, 'eslint.config.mjs'), 'export default [];\n'); + fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG); + syncEslintConfig(dir, read('eslint.config.mjs'), false, silent); + assert.deepEqual(fs.readdirSync(dir).sort(), ['.eslintrc.cjs', 'eslint.config.mjs']); + }); + + it('drops a plugin pnpm pin', function () { + const pkg: any = {name: 'ep_x', packageManager: 'pnpm@10.33.0'}; + assert.equal(dropPnpmPin(pkg, true, silent), true); + assert.equal('packageManager' in pkg, false); + const yarn: any = {packageManager: 'yarn@4.0.0'}; + assert.equal(dropPnpmPin(yarn, true, silent), false); + assert.equal(yarn.packageManager, 'yarn@4.0.0'); + }); +}); From 0a7a22fbc27c74062a37ab2e52a3dc96c5c47d2c Mon Sep 17 00:00:00 2001 From: John McLear Date: Sun, 29 Nov 2026 14:39:05 +0000 Subject: [PATCH 2/2] fix(plugins): only move plugins to ESLint 9 when they lint with a flat config Review follow-ups: - Decide the ESLint config before touching eslint devDependencies. A plugin with only a customised .eslintrc.* is no longer given ESLint 9, which would ignore its config. - A plugin that already has a flat config gets at least eslint ^9.39.5 and eslint-config-etherpad ^5.0.4, so removing its legacy config cannot leave it on ESLint 8. - Next to a flat config, only the stock .eslintrc.cjs is deleted; a customised one is kept with a warning. - autofix runs `pnpm install --no-frozen-lockfile`, so the lockfile follows the package.json changes even where CI makes frozen the default. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TcYAoDMav4BPPE9JrMtSvw --- bin/plugins/README.md | 9 ++--- bin/plugins/checkPlugin.ts | 32 +++++++----------- bin/plugins/pluginLint.ts | 47 +++++++++++++++++++++----- src/tests/backend/specs/checkPlugin.ts | 30 +++++++++++++--- 4 files changed, 81 insertions(+), 37 deletions(-) diff --git a/bin/plugins/README.md b/bin/plugins/README.md index f8ec7069c71..c90d62011bd 100755 --- a/bin/plugins/README.md +++ b/bin/plugins/README.md @@ -71,10 +71,11 @@ checkPlugin does not bump the plugin version; the plugin's `npmpublish.yml` does that when the push lands. Plugins lint with ESLint 9 and a flat config. checkPlugin keeps a plugin's own -`eslint.config.*`, deletes a leftover `.eslintrc.*` next to it (ESLint 9 -ignores those), and moves a plugin that only has the stock `.eslintrc.cjs`, or -no config at all, to `lib/eslint.config.mjs`. A customised `.eslintrc.*` gets a -warning and has to be migrated by hand. +`eslint.config.*`, deletes a leftover stock `.eslintrc.cjs` next to it (ESLint 9 +ignores it), and moves a plugin that only has the stock `.eslintrc.cjs`, or no +config at all, to `lib/eslint.config.mjs`. A plugin on a flat config gets at +least eslint 9 and eslint-config-etherpad 5.0.4. A customised `.eslintrc.*` is +never deleted: it gets a warning and has to be migrated by hand. Every workflow template gets pnpm from core's `packageManager` field, so plugin CI runs the same pnpm as core and no template sets a pnpm `version:`. A diff --git a/bin/plugins/checkPlugin.ts b/bin/plugins/checkPlugin.ts index fd951e8b8b2..bd6a73eda5a 100644 --- a/bin/plugins/checkPlugin.ts +++ b/bin/plugins/checkPlugin.ts @@ -21,7 +21,7 @@ import childProcess from 'node:child_process'; import log4js from 'log4js'; import path from 'node:path'; import semver from "semver"; -import {FLAT_CONFIG_DEV_DEPS, dropPnpmPin, syncEslintConfig} from './pluginLint'; +import {dropPnpmPin, flatConfigDevDepBumps, syncEslintConfig} from './pluginLint'; // Template files that some plugins deliberately customise. checkPlugin leaves // these alone instead of overwriting them with the copy in bin/plugins/lib/. @@ -247,12 +247,9 @@ log4js.configure({ const parsedPackageJSON = JSON.parse(packageJSON); await updateDeps(parsedPackageJSON, 'devDependencies', { - // Match the plugin fleet, but never rewrite an existing pin: some plugins - // are already on newer majors (e.g. eslint 10), and changing the - // TypeScript version can break plugin code. A plugin moved to the flat - // config below gets at least these versions. - 'eslint': {ver: FLAT_CONFIG_DEV_DEPS.eslint, overwrite: false}, - 'eslint-config-etherpad': {ver: FLAT_CONFIG_DEV_DEPS['eslint-config-etherpad'], overwrite: false}, + // Never rewrite an existing pin: changing the TypeScript version can + // break plugin code. eslint and eslint-config-etherpad are handled with + // the ESLint config below. 'typescript': {ver: '~6.0.3', overwrite: false}, // These were moved to eslint-config-etherpad's dependencies so they can be removed: '@typescript-eslint/eslint-plugin': null, @@ -297,18 +294,13 @@ log4js.configure({ // ESLint 9 only reads flat configs (eslint.config.*). Never write a legacy // .eslintrc.cjs: ESLint 9 ignores it, and it confuses anyone reading the // plugin. - const {wroteFlatConfig} = syncEslintConfig( + const {usesFlatConfig} = syncEslintConfig( pluginPath, await fsp.readFile('bin/plugins/lib/eslint.config.mjs', 'utf8'), autoFix, logger); - if (wroteFlatConfig) { - // lib/eslint.config.mjs needs ESLint 9 and eslint-config-etherpad 5.0.4. - const devDeps = parsedPackageJSON.devDependencies || {}; - const want: {[pkg: string]: string} = {}; - for (const [pkg, ver] of Object.entries(FLAT_CONFIG_DEV_DEPS)) { - const min = semver.minVersion(ver)!; - const got = devDeps[pkg] && semver.validRange(devDeps[pkg]) ? semver.minVersion(devDeps[pkg]) : null; - if (got == null || semver.lt(got, min)) want[pkg] = ver; - } - await updateDeps(parsedPackageJSON, 'devDependencies', want); + if (usesFlatConfig) { + // A flat config needs ESLint 9 and eslint-config-etherpad 5.0.4. Newer + // pins (e.g. eslint 10) are kept. + await updateDeps(parsedPackageJSON, 'devDependencies', + flatConfigDevDepBumps(parsedPackageJSON.devDependencies)); } // Plugin CI installs core's pnpm (see lib/*.yml), so drop a plugin's own pin. @@ -526,7 +518,9 @@ log4js.configure({ // Install dependencies so we can run ESLint. This should also create or update package-lock.json // if autoFix is enabled. - const npmInstall = `pnpm install`; + // pnpm defaults to --frozen-lockfile under CI, which would fail after the + // autofixes above changed package.json, so let autofix update the lockfile. + const npmInstall = autoFix ? 'pnpm install --no-frozen-lockfile' : 'pnpm install'; execSync(npmInstall, {stdio: 'inherit'}); // Create the ep_etherpad-lite symlink if necessary. This must be done after running `npm install` // because that command nukes the symlink. diff --git a/bin/plugins/pluginLint.ts b/bin/plugins/pluginLint.ts index a01fe4da562..ece3d194a6f 100644 --- a/bin/plugins/pluginLint.ts +++ b/bin/plugins/pluginLint.ts @@ -4,6 +4,7 @@ import fs from 'node:fs'; import path from 'node:path'; +import semver from 'semver'; // File names ESLint 9 reads as a flat config. export const FLAT_CONFIG_NAMES = [ @@ -43,24 +44,32 @@ export type EslintConfigResult = { removed: string[], // True when eslint.config.mjs was (or would be) written from the template. wroteFlatConfig: boolean, + // True when the plugin lints with a flat config afterwards, so it needs at + // least FLAT_CONFIG_DEV_DEPS. False while a customised legacy config waits + // to be migrated by hand: moving that plugin to ESLint 9 would stop it + // reading its own config. + usesFlatConfig: boolean, }; const normalize = (s: string) => s.replace(/\r\n/g, '\n').trim(); // Make sure the plugin uses an ESLint flat config: -// - a plugin that already has one keeps it, and any leftover legacy config is -// removed because ESLint 9 ignores it; +// - a plugin that already has one keeps it, and a leftover stock legacy config +// is removed because ESLint 9 ignores it; // - a plugin with only the stock legacy config, or none at all, gets // lib/eslint.config.mjs; -// - a customised legacy config is left alone with a warning. -// The caller bumps the ESLint devDependencies when wroteFlatConfig is true. +// - a customised legacy config is never removed, only warned about. +// The caller raises the ESLint devDependencies when usesFlatConfig is true. export const syncEslintConfig = ( pluginPath: string, flatTemplate: string, autoFix: boolean, logger: Logger, ): EslintConfigResult => { const exists = (f: string) => fs.existsSync(path.join(pluginPath, f)); const flat = FLAT_CONFIG_NAMES.filter(exists); const legacy = LEGACY_CONFIG_NAMES.filter(exists); - const result: EslintConfigResult = {removed: [], wroteFlatConfig: false}; + const custom = legacy.filter((f) => normalize( + fs.readFileSync(path.join(pluginPath, f), 'utf8')) !== normalize(STOCK_LEGACY_CONFIG)); + const stock = legacy.filter((f) => !custom.includes(f)); + const result: EslintConfigResult = {removed: [], wroteFlatConfig: false, usesFlatConfig: false}; const remove = (f: string, why: string) => { logger.warn(`${f} ${why}; ${autoFix ? 'removing it' : 'please remove it'}`); @@ -69,25 +78,45 @@ export const syncEslintConfig = ( }; if (flat.length > 0) { - for (const f of legacy) remove(f, `is ignored because ${flat[0]} exists`); + for (const f of stock) remove(f, `is the stock legacy ESLint config and ${flat[0]} exists`); + if (custom.length > 0) { + logger.warn(`${custom.join(', ')} is a customised legacy ESLint config that ESLint 9 ` + + `ignores; move any rules it still needs into ${flat[0]} and delete it by hand`); + } + result.usesFlatConfig = true; return result; } - const custom = legacy.filter((f) => normalize( - fs.readFileSync(path.join(pluginPath, f), 'utf8')) !== normalize(STOCK_LEGACY_CONFIG)); if (custom.length > 0) { logger.warn(`${custom.join(', ')} is a customised legacy ESLint config; ` + 'move it to eslint.config.mjs by hand (ESLint 9 ignores it)'); return result; } - for (const f of legacy) remove(f, 'is the stock legacy ESLint config'); + for (const f of stock) remove(f, 'is the stock legacy ESLint config'); logger.warn(`eslint.config.mjs not found; ${autoFix ? 'adding' : 'please add'} the template copy`); if (autoFix) fs.writeFileSync(path.join(pluginPath, 'eslint.config.mjs'), flatTemplate); result.wroteFlatConfig = true; + result.usesFlatConfig = true; return result; }; +// The devDependencies a flat-config plugin is missing or has below +// FLAT_CONFIG_DEV_DEPS. Ranges that are not valid semver are left alone. +export const flatConfigDevDepBumps = (devDeps: {[pkg: string]: string} = {}) => { + const want: {[pkg: string]: string} = {}; + for (const [pkg, ver] of Object.entries(FLAT_CONFIG_DEV_DEPS)) { + const got = devDeps[pkg]; + if (got == null) { + want[pkg] = ver; + } else if (semver.validRange(got) != null && + semver.lt(semver.minVersion(got)!, semver.minVersion(ver)!)) { + want[pkg] = ver; + } + } + return want; +}; + // Plugin CI takes its pnpm version from core's package.json, so a plugin's own // pnpm pin only makes the plugin run an older (or different) pnpm than core. // Returns true if the pin was found (and, with autoFix, removed from pkg). diff --git a/src/tests/backend/specs/checkPlugin.ts b/src/tests/backend/specs/checkPlugin.ts index 43adfd984e1..c6a75e7d9fe 100644 --- a/src/tests/backend/specs/checkPlugin.ts +++ b/src/tests/backend/specs/checkPlugin.ts @@ -12,7 +12,7 @@ import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { - STOCK_LEGACY_CONFIG, dropPnpmPin, syncEslintConfig, + STOCK_LEGACY_CONFIG, dropPnpmPin, flatConfigDevDepBumps, syncEslintConfig, } from '../../../../bin/plugins/pluginLint'; const REPO_ROOT = path.resolve(__dirname, '..', '..', '..', '..'); @@ -114,7 +114,7 @@ describe(`${__filename} templates`, function () { fs.writeFileSync(path.join(dir, 'eslint.config.mjs'), 'export default [];\n'); fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG); const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); - assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: false}); + assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: false, usesFlatConfig: true}); assert.equal(fs.readFileSync(path.join(dir, 'eslint.config.mjs'), 'utf8'), 'export default [];\n'); assert.equal(fs.existsSync(path.join(dir, '.eslintrc.cjs')), false); @@ -123,7 +123,7 @@ describe(`${__filename} templates`, function () { it('moves a stock legacy config to the flat config', function () { fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG); const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); - assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: true}); + assert.deepEqual(res, {removed: ['.eslintrc.cjs'], wroteFlatConfig: true, usesFlatConfig: true}); assert.equal(fs.readFileSync(path.join(dir, 'eslint.config.mjs'), 'utf8'), read('eslint.config.mjs')); assert.equal(fs.existsSync(path.join(dir, '.eslintrc.cjs')), false); @@ -131,7 +131,7 @@ describe(`${__filename} templates`, function () { it('adds the flat config to a plugin with no ESLint config', function () { const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); - assert.deepEqual(res, {removed: [], wroteFlatConfig: true}); + assert.deepEqual(res, {removed: [], wroteFlatConfig: true, usesFlatConfig: true}); assert.deepEqual(fs.readdirSync(dir), ['eslint.config.mjs']); }); @@ -139,10 +139,30 @@ describe(`${__filename} templates`, function () { const custom = `${STOCK_LEGACY_CONFIG}module.exports.rules = {};\n`; fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), custom); const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); - assert.deepEqual(res, {removed: [], wroteFlatConfig: false}); + // Not on a flat config, so the caller must not move it to ESLint 9. + assert.deepEqual(res, {removed: [], wroteFlatConfig: false, usesFlatConfig: false}); assert.deepEqual(fs.readdirSync(dir), ['.eslintrc.cjs']); }); + it('keeps a customised legacy config next to a flat config', function () { + const custom = `${STOCK_LEGACY_CONFIG}module.exports.rules = {};\n`; + fs.writeFileSync(path.join(dir, 'eslint.config.mjs'), 'export default [];\n'); + fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), custom); + const res = syncEslintConfig(dir, read('eslint.config.mjs'), true, silent); + assert.deepEqual(res, {removed: [], wroteFlatConfig: false, usesFlatConfig: true}); + assert.equal(fs.readFileSync(path.join(dir, '.eslintrc.cjs'), 'utf8'), custom); + }); + + it('raises ESLint to 9 for a flat config but keeps newer pins', function () { + assert.deepEqual(flatConfigDevDepBumps({'eslint': '^8.57.1'}), + {'eslint': '^9.39.5', 'eslint-config-etherpad': '^5.0.4'}); + assert.deepEqual(flatConfigDevDepBumps({ + 'eslint': '^10.15.0', 'eslint-config-etherpad': '^5.0.4', + }), {}); + assert.deepEqual(flatConfigDevDepBumps(undefined), + {'eslint': '^9.39.5', 'eslint-config-etherpad': '^5.0.4'}); + }); + it('changes nothing without autofix', function () { fs.writeFileSync(path.join(dir, 'eslint.config.mjs'), 'export default [];\n'); fs.writeFileSync(path.join(dir, '.eslintrc.cjs'), STOCK_LEGACY_CONFIG);