Skip to content
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
11 changes: 11 additions & 0 deletions bin/plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ 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 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
plugin's own `packageManager: pnpm@...` pin is removed for the same reason.

```
getCorePlugins.sh
updateCorePlugins.sh
Expand Down
45 changes: 24 additions & 21 deletions bin/plugins/checkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import childProcess from 'node:child_process';
import log4js from 'log4js';
import path from 'node:path';
import semver from "semver";
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/.
Expand All @@ -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'],
};
Expand Down Expand Up @@ -246,11 +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.
'eslint': {ver: '^8.57.1', overwrite: false},
'eslint-config-etherpad': {ver: '^5.0.0', 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,
Expand Down Expand Up @@ -292,19 +291,21 @@ 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}`);
}
} else {
await checkFile('bin/plugins/lib/eslintrc.cjs', '.eslintrc.cjs', false);
// 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 {usesFlatConfig} = syncEslintConfig(
pluginPath, await fsp.readFile('bin/plugins/lib/eslint.config.mjs', 'utf8'), autoFix, logger);
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.
if (dropPnpmPin(parsedPackageJSON, autoFix, logger) && autoFix) {
await writePackageJson(parsedPackageJSON);
}

if (checkEntries(parsedPackageJSON, {
Expand Down Expand Up @@ -517,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.
Expand Down
4 changes: 4 additions & 0 deletions bin/plugins/lib/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions bin/plugins/lib/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import etherpad from 'eslint-config-etherpad/flat/plugin.js';

export default etherpad;
9 changes: 0 additions & 9 deletions bin/plugins/lib/eslintrc.cjs

This file was deleted.

21 changes: 18 additions & 3 deletions bin/plugins/lib/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,34 @@ 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:
node-version: 26
- 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
Expand All @@ -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
3 changes: 3 additions & 0 deletions bin/plugins/lib/npmpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
137 changes: 137 additions & 0 deletions bin/plugins/pluginLint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// 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';
import semver from 'semver';

// 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,
// 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 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 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 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'}`);
if (autoFix) fs.rmSync(path.join(pluginPath, f));
result.removed.push(f);
};

if (flat.length > 0) {
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;
}

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 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).
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;
};
Loading
Loading