diff --git a/.changeset/green-stingrays-dress.md b/.changeset/green-stingrays-dress.md new file mode 100644 index 0000000000..eb89bb558e --- /dev/null +++ b/.changeset/green-stingrays-dress.md @@ -0,0 +1,5 @@ +--- +"@sanity/plugin-kit": patch +--- + +Fix `verify-package`/`verify-studio` import checks to fall back when `@typescript-eslint` fails to load with TypeScript 7. diff --git a/packages/@sanity/plugin-kit/src/dependencies/import-linter.ts b/packages/@sanity/plugin-kit/src/dependencies/import-linter.ts index d2d63b91c9..2e9c99c638 100644 --- a/packages/@sanity/plugin-kit/src/dependencies/import-linter.ts +++ b/packages/@sanity/plugin-kit/src/dependencies/import-linter.ts @@ -11,44 +11,69 @@ const removedImportSuffix = `imports where removed in Sanity v3. Please refer to export async function validateImports({basePath}: {basePath: string}): Promise { log.debug('Running ESLint with Sanity Studio import hints...') + const eslintConfig: ESLint.Options['overrideConfig'] = { + ignorePatterns: ['node_modules'], + parserOptions: { + ecmaVersion: 'latest', + sourceType: 'module', + }, + rules: { + 'no-restricted-imports': [ + 'error', + { + patterns: [ + ...mergedPackages.map((packageName) => ({ + group: [`${packageName}*`], + message: `Use sanity instead of ${packageName}.`, + })), + { + group: ['config:*'], + message: `config: imports are no longer supported. Please see the new plugin API for alternatives: ${urls.migrationGuideStudio}`, + }, + { + group: ['part:*'], + message: `part: ${removedImportSuffix}`, + }, + { + group: ['all:part:*'], + message: `all:part: ${removedImportSuffix}`, + }, + { + group: ['sanity:*'], + message: `sanity: ${removedImportSuffix}`, + }, + ], + }, + ], + }, + } + const eslint = new ESLint({ cwd: basePath, - overrideConfig: { - ignorePatterns: ['node_modules'], - rules: { - 'no-restricted-imports': [ - 'error', - { - patterns: [ - ...mergedPackages.map((packageName) => ({ - group: [`${packageName}*`], - message: `Use sanity instead of ${packageName}.`, - })), - { - group: ['config:*'], - message: `config: imports are no longer supported. Please see the new plugin API for alternatives: ${urls.migrationGuideStudio}`, - }, - { - group: ['part:*'], - message: `part: ${removedImportSuffix}`, - }, - { - group: ['all:part:*'], - message: `all:part: ${removedImportSuffix}`, - }, - { - group: ['sanity:*'], - message: `sanity: ${removedImportSuffix}`, - }, - ], - }, - ], - }, - }, + overrideConfig: eslintConfig, }) try { - const results = await eslint.lintFiles([path.join(basePath, '**/*.{js,jsx,ts,tsx}')]) + let results + try { + results = await eslint.lintFiles([path.join(basePath, '**/*.{js,jsx,ts,tsx}')]) + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + const shouldRetryWithoutUserConfig = + message.includes('@typescript-eslint') && message.includes("reading 'Cjs'") + + if (!shouldRetryWithoutUserConfig) { + throw error + } + + log.debug('Retrying ESLint import check without user config: %s', message) + const fallbackEslint = new ESLint({ + cwd: basePath, + useEslintrc: false, + overrideConfig: eslintConfig, + }) + results = await fallbackEslint.lintFiles([path.join(basePath, '**/*.{js,jsx,ts,tsx}')]) + } const onlyImportErrors = results .map((r) => {