Skip to content

Commit

Permalink
fix: fail if eslint is not properly configured or installed instead o…
Browse files Browse the repository at this point in the history
…f ignoring the errors
  • Loading branch information
zaripych committed Jan 28, 2024
1 parent a78e6f4 commit ee14579
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
14 changes: 14 additions & 0 deletions .changeset/little-pumpkins-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'refactor-bot': patch
---

fix: fail if eslint is not properly configured or installed instead of ignoring
the errors

If eslint is not properly configured or installed, the refactor bot would ignore
the errors because it would fail to analyze `stderr` of the `eslint` command.

It now properly fails with a message that explains the problem.

This should lead to better outcomes when configuring the refactor bot for the
first time.
9 changes: 9 additions & 0 deletions packages/refactor-bot/src/eslint/autoFixIssues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';

import { makeCachedFunction } from '../cache/makeCachedFunction';
import { spawnResult } from '../child-process/spawnResult';
import { logger } from '../logger/logger';
import { determinePackageManager } from '../package-manager/determinePackageManager';
import { runPackageManagerScript } from '../package-manager/runPackageManagerScript';

Expand Down Expand Up @@ -115,6 +116,14 @@ export const autoFixIssuesContents = makeCachedFunction({
writeToStdin(),
]);

if (result.stderr) {
logger.error('Failed to auto-fix issues', result.stderr);

return {
contents: opts.fileContents,
};
}

const [first] = z
.array(
z
Expand Down
26 changes: 11 additions & 15 deletions packages/refactor-bot/src/package-manager/runCheckCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import assert from 'assert';
import { realpath } from 'fs/promises';
import { basename, isAbsolute, normalize, relative, sep } from 'path';

import { ConfigurationError } from '../errors/configurationError';
import { logger } from '../logger/logger';
import { escapeRegExp } from '../utils/escapeRegExp';
import { ensureHasOneElement } from '../utils/hasOne';
import { UnreachableError } from '../utils/UnreachableError';
import { runPackageManagerScript } from './runPackageManagerScript';

type Issue = {
Expand Down Expand Up @@ -117,7 +117,7 @@ export async function runCheckCommandWithParser(opts: {
packageManager: 'npm' | 'yarn' | 'pnpm';
script: {
args: [string, ...string[]];
parse: 'stdout' | 'stderr';
failOnStderr?: boolean;
supportsFileFiltering: boolean;
};
outputParser: (output: string) => Issue[];
Expand All @@ -131,17 +131,6 @@ export async function runCheckCommandWithParser(opts: {
logOnError: undefined,
});

const chooseOutput = () => {
switch (opts.script.parse) {
case 'stderr':
return stderr;
case 'stdout':
return stdout;
default:
throw new UnreachableError(opts.script.parse);
}
};

const parentDirectoryRegex = new RegExp(
`^.*${escapeRegExp(sep + normalize(basename(opts.location) + sep))}`,
'g'
Expand All @@ -151,9 +140,15 @@ export async function runCheckCommandWithParser(opts: {
() => opts.location
);

if (stderr && opts.script.failOnStderr) {
logger.error(stderr);

throw new ConfigurationError(`Failed when running "${args.join(' ')}"`);
}

return {
args,
issues: opts.outputParser(chooseOutput()).map((data) => {
issues: opts.outputParser(stdout).map((data) => {
const issue = data.issue.replaceAll(parentDirectoryRegex, '');
return {
...data,
Expand All @@ -178,6 +173,7 @@ export async function runCheckCommand(opts: {
const script = {
...opts.script,
args: ensureHasOneElement(opts.script.args.concat([])),
failOnStderr: true,
};

let parser: ((output: string) => Issue[]) | undefined = undefined;
Expand All @@ -199,6 +195,7 @@ export async function runCheckCommand(opts: {
if (opts.filePaths) {
script.args.push('--findRelatedTests', ...opts.filePaths);
}
script.failOnStderr = false;
break;

default:
Expand All @@ -212,7 +209,6 @@ export async function runCheckCommand(opts: {
...opts,
script: {
...script,
parse: 'stdout',
supportsFileFiltering: script.args[0] !== 'tsc',
},
outputParser: parser,
Expand Down

0 comments on commit ee14579

Please sign in to comment.