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

Prettier Plugin not respecting config when called via CLI #16

Closed
1 task done
fbartho opened this issue May 17, 2022 · 3 comments
Closed
1 task done

Prettier Plugin not respecting config when called via CLI #16

fbartho opened this issue May 17, 2022 · 3 comments
Assignees

Comments

@fbartho
Copy link
Collaborator

fbartho commented May 17, 2022

Your Environment

  • Prettier version: 2.6.2
  • node version: 16.13.2
  • package manager: yarn@1.22.18
  • IDE: VSCode 1.67.1

Describe the bug

In our repo, when we use a CLI command to rewrite all files, it successfully executes prettier, and maybe some of the plugin, but not all the configuration is preserved. Effectively it looks like it ignores the importOrder setting from the config.

To Reproduce

(Using the config provided below)

With this test file, already formatted via prettier, but without the prettier-plugin-sort-imports installed.

testFile.ts
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/naming-convention */

import { DateTime } from "luxon";
import { Inject, Service } from "typedi";

import EmailService from "./email";
import {
	CareNotesDatabase,
	OfficeStaffDatabase,
	ShiftsDatabase,
	OfficeReport,
	OfficeStaffContactInfo,
	OfficeStaffRoles,
	AgencyConfigDatabase,
} from "../database";

import { EmailRecipient, Shift, TriggerProp } from "../types";
import SMSService from "./sms";
import { logger } from "@hca/logger";
import MESSAGES, { MESSAGE_TRIGGER_PROPS, MESSAGE_TYPES } from "../constants/messages";
import snakecase from "lodash.snakecase";

export interface OfficeReportMessageData {
	careNoteDocId: string;
}

Run yarn prettier --write testFile.ts to see:

CLI-formatted:
/* eslint-disable @typescript-eslint/no-unused-vars */

/* eslint-disable @typescript-eslint/naming-convention */
import MESSAGES, { MESSAGE_TRIGGER_PROPS, MESSAGE_TYPES } from "../constants/messages";

import snakecase from "lodash.snakecase";
import { DateTime } from "luxon";
import { Inject, Service } from "typedi";

import { logger } from "@hca/logger";

import {
	AgencyConfigDatabase,
	CareNotesDatabase,
	OfficeReport,
	OfficeStaffContactInfo,
	OfficeStaffDatabase,
	OfficeStaffRoles,
	ShiftsDatabase,
} from "../database";
import { EmailRecipient, Shift, TriggerProp } from "../types";
import EmailService from "./email";
import SMSService from "./sms";

export interface OfficeReportMessageData {
	careNoteDocId: string;
}

Trigger IDE to invoke prettier to auto-format to see:

Editor Formatted
/* eslint-disable @typescript-eslint/no-unused-vars */

/* eslint-disable @typescript-eslint/naming-convention */
import snakecase from "lodash.snakecase";
import { DateTime } from "luxon";
import { Inject, Service } from "typedi";

import { logger } from "@hca/logger";

import MESSAGES, { MESSAGE_TRIGGER_PROPS, MESSAGE_TYPES } from "../constants/messages";
import {
	AgencyConfigDatabase,
	CareNotesDatabase,
	OfficeReport,
	OfficeStaffContactInfo,
	OfficeStaffDatabase,
	OfficeStaffRoles,
	ShiftsDatabase,
} from "../database";
import { EmailRecipient, Shift, TriggerProp } from "../types";
import EmailService from "./email";
import SMSService from "./sms";

export interface OfficeReportMessageData {
	careNoteDocId: string;
}

Expected behavior

I would expect the IDE-prettier-invoked output to be the final result, since that matches my importOrder configuration.

Given that it does make changes to the layout when invoking it via CLI with the plugin installed, I think the plugin isn't respecting the options from the configuration file when invoked via CLI. I have verified that prettier is indeed discovering the plugin via CLI --support-info and the above technique.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

module.exports = {
	overrides: [
		{
			// prettier will strip newlines out of package.json files unless you tell it to use the json parser
			files: ["package.json", "**/package.json"],
			options: { parser: "json" },
		},
	],
	printWidth: 100,
	semi: true,
	singleQuote: false, // Single quotes are common in text-strings
	trailingComma: "all", // Improves refactoring / minimizes git-conflicts
	tabWidth: 2,
	useTabs: true,
	// @ianvs/prettier-plugin-sort-imports
	importOrder: ["^@hca", "^[.]+"],
	importOrderBuiltinModulesToTop: true,
	importOrderCaseInsensitive: true,
	importOrderParserPlugins: ["typescript", "jsx", "decorators-legacy"],
	importOrderSeparation: true,
	importOrderSortSpecifiers: true,
	// @prettier/plugin-xml
	xmlWhitespaceSensitivity: "ignore",
};

Error log
N/A

Contribute to @ianvs/prettier-plugin-sort-imports

  • I'm willing to fix this bug 🥇
@fbartho fbartho changed the title Prettier Plugin not activating when called via CLI Prettier Plugin respecting-config when called via CLI May 17, 2022
@IanVS
Copy link
Owner

IanVS commented May 17, 2022

This is really strange, I run the cli on my own code and do not have this problem. Do you think you could make a minimal reproduction of this in a new repo that you could share? If nothing else, that might help narrow down why this is happening.

@fbartho
Copy link
Collaborator Author

fbartho commented May 17, 2022

I have verified that the preprocessor from the plugin for typescript is indeed being invoked and it is receiving the correct config options.

fbartho added a commit to fbartho/prettier-plugin-sort-imports that referenced this issue May 17, 2022
Relates to IanVS#16

Looks like a combination of config options are conflicting?

Not sure why it triggers on CLI vs API for me, but the run_spec is able to reproduce it in this repo.
@fbartho fbartho changed the title Prettier Plugin respecting-config when called via CLI Prettier Plugin not respecting config when called via CLI May 17, 2022
@fbartho
Copy link
Collaborator Author

fbartho commented May 17, 2022

@IanVS I have manually constructed a failing snapshot for this #17 -- if you look at the provided prettier.config.js, you can see the importOrder would expect to match the output in the snapshot (and that's what's generated when my IDE-VSCode reformats).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants