diff --git a/packages/rstack/src/cli/commands.ts b/packages/rstack/src/cli/commands.ts index 4368dbea..891f67d5 100644 --- a/packages/rstack/src/cli/commands.ts +++ b/packages/rstack/src/cli/commands.ts @@ -1,4 +1,5 @@ import { join, resolve } from 'node:path'; +import { pathToFileURL } from 'node:url'; import { getConfigState } from '../config.ts'; import { insertConfigArg, parseArgs, parseCliArgs } from './args.ts'; import { hasHelpFlag, printCommandHelp } from './help.ts'; @@ -138,6 +139,8 @@ async function runRspressCLI(args: string[]): Promise { } } +const RSLINT_CONFIG_PATH = join(import.meta.dirname, 'rslintConfig.js'); + async function runRslintCLI(args: string[]): Promise { if (hasHelpFlag(args)) { return printCommandHelp('lint'); @@ -146,11 +149,7 @@ async function runRslintCLI(args: string[]): Promise { const argv = [ process.execPath, 'rslint', - ...insertConfigArg( - args, - '--config', - join(import.meta.dirname, 'rslintConfig.js'), - ), + ...insertConfigArg(args, '--config', RSLINT_CONFIG_PATH), ]; const { runCLI } = await import('@rslint/core'); @@ -177,11 +176,16 @@ async function runCheckCLI(args: string[]): Promise { return; } + // Rslint loads its one-shot config through Node's module cache. Import the + // same URL to read the Rstack config exported for the following fmt phase. + const { loadedConfig } = (await import( + pathToFileURL(RSLINT_CONFIG_PATH).href + )) as typeof import('../rslintConfig.ts'); const { runFmtCLI } = await import( /* rspackChunkName: 'fmt' */ '../fmt/cli.ts' ); - await runFmtCLI(['--check']); + await runFmtCLI(['--check'], { loadedConfig }); } export async function setupCommands(): Promise { diff --git a/packages/rstack/src/fmt/cli.ts b/packages/rstack/src/fmt/cli.ts index 7b13f498..0eb4e626 100644 --- a/packages/rstack/src/fmt/cli.ts +++ b/packages/rstack/src/fmt/cli.ts @@ -3,7 +3,7 @@ import { performance } from 'node:perf_hooks'; import { color, logger } from 'rslog'; import { parseArgs } from '../cli/args.ts'; import { printCommandHelp } from '../cli/help.ts'; -import { loadRstackConfig } from '../config.ts'; +import { loadRstackConfig, type LoadedRstackConfig } from '../config.ts'; import { ensureProjectCacheDir } from '../projectCache.ts'; import { fmtCacheFileName } from './cacheStore.ts'; import { resolveFmtConfig } from './config.ts'; @@ -29,6 +29,11 @@ interface ParsedFmtCLIArgs { lsp: boolean; } +type RunFmtCLIOptions = { + /** Rstack config already loaded by the lint phase of `rs check`. */ + loadedConfig?: LoadedRstackConfig; +}; + const parseMaxWorkers = (value: string | undefined): number | undefined => { if (value === undefined) { return undefined; @@ -255,8 +260,12 @@ const logFmtResult = ( } }; -const loadFmtConfig = async (cwd: string): Promise => { - const { configs, filePath } = await loadRstackConfig({ cwd }); +const loadFmtConfig = async ( + cwd: string, + loadedConfig?: LoadedRstackConfig, +): Promise => { + const { configs, filePath } = + loadedConfig ?? (await loadRstackConfig({ cwd })); return resolveFmtConfig({ definition: configs.fmt, @@ -265,7 +274,10 @@ const loadFmtConfig = async (cwd: string): Promise => { }); }; -const runFmtCLI = async (args: string[]): Promise => { +const runFmtCLI = async ( + args: string[], + { loadedConfig }: RunFmtCLIOptions = {}, +): Promise => { const cwd = process.cwd(); const startTime = performance.now(); @@ -337,7 +349,7 @@ const runFmtCLI = async (args: string[]): Promise => { } } - const config = await loadFmtConfig(cwd); + const config = await loadFmtConfig(cwd, loadedConfig); const files = await discoverFmtFiles({ cwd, patterns, diff --git a/packages/rstack/src/rslintConfig.ts b/packages/rstack/src/rslintConfig.ts index 50f13c20..2a0cb25f 100644 --- a/packages/rstack/src/rslintConfig.ts +++ b/packages/rstack/src/rslintConfig.ts @@ -1,7 +1,10 @@ -import { loadRstackConfig } from './config.ts'; +import { loadRstackConfig, type LoadedRstackConfig } from './config.ts'; import type { RslintConfig } from '@rslint/core'; -const { configs } = await loadRstackConfig(); +// Expose the loaded config so `rs check` can pass it to fmt instead of loading +// and executing the Rstack config a second time. +export const loadedConfig: LoadedRstackConfig = await loadRstackConfig(); +const { configs } = loadedConfig; const lintDefinition = configs.lint ?? []; let lintConfig: RslintConfig;