Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: handle --output-file for empty output when saving to disk #17957

Merged
merged 4 commits into from Jan 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/src/use/migrate-to-9.0.0.md
Expand Up @@ -30,6 +30,7 @@ The lists below are ordered roughly by the number of users each change is expect
* [Stricter `/* exported */` parsing](#exported-parsing)
* [`"eslint:recommended"` and `"eslint:all"` strings no longer accepted in flat config](#string-config)
* [`varsIgnorePattern` option of `no-unused-vars` no longer applies to catch arguments](#vars-ignore-pattern)
* [`--output-file` now writes a file to disk even with an empty output](#output-file)

### Breaking changes for plugin developers

Expand Down Expand Up @@ -299,6 +300,14 @@ try {

**Related issue(s):** [#17540](https://github.com/eslint/eslint/issues/17540)

## <a name="output-file"></a> `--output-file` now writes a file to disk even with an empty output

Prior to ESLint v9.0.0, the `--output-file` flag would skip writing a file to disk if the output was empty. However, in ESLint v9.0.0, `--output-file` now consistently writes a file to disk, even when the output is empty. This update ensures a more consistent and reliable behavior for `--output-file`.

**To address:** Review your usage of the `--output-file` flag, especially if your processes depend on the file's presence or absence based on output content. If necessary, update your scripts or configurations to accommodate this change.

**Related Issues(s):** [#17660](https://github.com/eslint/eslint/issues/17660)

## <a name="removed-context-methods"></a> Removed multiple `context` methods

ESLint v9.0.0 removes multiple deprecated methods from the `context` object and moves them onto the `SourceCode` object:
Expand Down
30 changes: 14 additions & 16 deletions lib/cli.js
Expand Up @@ -285,25 +285,23 @@ async function printResults(engine, results, format, outputFile, resultsMeta) {

const output = await formatter.format(results, resultsMeta);

if (output) {
if (outputFile) {
const filePath = path.resolve(process.cwd(), outputFile);
if (outputFile) {
const filePath = path.resolve(process.cwd(), outputFile);

if (await isDirectory(filePath)) {
log.error("Cannot write to output file path, it is a directory: %s", outputFile);
return false;
}
if (await isDirectory(filePath)) {
log.error("Cannot write to output file path, it is a directory: %s", outputFile);
return false;
}

try {
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, output);
} catch (ex) {
log.error("There was a problem writing the output file:\n%s", ex);
return false;
}
} else {
log.info(output);
try {
await mkdir(path.dirname(filePath), { recursive: true });
await writeFile(filePath, output);
} catch (ex) {
log.error("There was a problem writing the output file:\n%s", ex);
return false;
}
} else if (output) {
log.info(output);
}

return true;
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -1006,6 +1006,19 @@ describe("cli", () => {
assert.isTrue(log.info.notCalled);
});

// https://github.com/eslint/eslint/issues/17660
it(`should write the file and create dirs if they don't exist even when output is empty with configType:${configType}`, async () => {
const filePath = getFixturePath("single-quoted.js");
const code = `${flag} --rule 'quotes: [1, single]' --o tests/output/eslint-output.txt ${filePath}`;

// TODO: fix this test to: await cli.execute(code, null, useFlatConfig);
await cli.execute(code, "var a = 'b'", useFlatConfig);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved

assert.isTrue(fs.existsSync("tests/output/eslint-output.txt"));
assert.strictEqual(fs.readFileSync("tests/output/eslint-output.txt", "utf8"), "");
assert.isTrue(log.info.notCalled);
});

it(`should return an error if the path is a directory with configType:${configType}`, async () => {
const filePath = getFixturePath("single-quoted.js");
const code = `${flag} --rule 'quotes: [1, double]' --o tests/output ${filePath}`;
Expand Down