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

Admin-Generator: Fix problems when formatting Source Code #3566

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

manuelblum
Copy link
Collaborator

depends on: #3564

Description

Fix eslint formatting problems in Admin Generator

While formatting generated code during writing to the disk, ESLint fails with an error, which will lead to the fact that not ESLint-formatted code will be generated by admin-generator. This produces invalid code and will fail in lint step.

  1. writing (not formatted file to filesystem) - this is required, because eslint searches in the file directory if the file is present (keyword: project service) - if not present it will fail with an error and will not format the provided source code.
  2. formatting file with eslint
  3. writing formatted source code to the file system

This fix is necessary, because if not fixed not valid source code will be generated.
Screenshot 2025-03-03 at 07 58 58

Further information

@manuelblum manuelblum force-pushed the com-1685-fix-eslint-formatting-issue-if-file-does-not-exist branch from 409b0dd to b163a16 Compare March 3, 2025 13:49
@manuelblum manuelblum changed the title Admin-Generator: Fix problems when formatting Source Code. Admin-Generator: Fix problems when formatting Source Code Mar 3, 2025
@nsams
Copy link
Member

nsams commented Mar 3, 2025

I'm sorry but I don't understand the problem. I don't know what you mean by that "keyword: project service"

Is this a new problem you have with all generated files? Or this this a problem you run into when the generator generates (for whatever reason) invalid code?

@manuelblum
Copy link
Collaborator Author

I'm sorry but I don't understand the problem. I don't know what you mean by that "keyword: project service"

Is this a new problem you have with all generated files? Or this this a problem you run into when the generator generates (for whatever reason) invalid code?

Let me explain how I understood the problem:

eslint.lintText in packages/admin-generator/src/commands/generate/writeGenerated.ts fails with the following error only on some Files ( i have no real clue why it only affects some files)

Screenshot 2025-03-04 at 08 45 32

As far as I see it happens only when the problematic file is not available on the disk.

That's why I introduced the workaround that the file (with generated source code, but not formatted) will be written to the disk at the given path before linting. If the file is available on the disk, the lintText succeeds.

@johnnyomair johnnyomair requested a review from nsams March 4, 2025 08:07
@nsams
Copy link
Member

nsams commented Mar 4, 2025

Ok, I understand the problem now - and can reproduce it. Very strange.

I don't understand the solution, but at least it is a working solution.

@@ -11,10 +11,24 @@ export async function writeGenerated(filePath: string, contents: string): Promis
cwd: process.cwd(),
fix: true,
});

// Write not linted generated code into file. This is necessary to avoid linting errors like: Parsing error: file/path/file.tsx was not found by the project service.
await fs.writeFile(filePath, contents);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await fs.writeFile(filePath, contents);
await fs.writeFile(filePath, header + contents);

(was also wrong in line 33)

Comment on lines 32 to 33
const output = lintResult[0] && lintResult[0].output ? lintResult[0].output : lintResult[0].source;
await fs.writeFile(filePath, output ?? contents);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unlinted file is already written above, no need to fallback to contents (which was missing header)

Suggested change
const output = lintResult[0] && lintResult[0].output ? lintResult[0].output : lintResult[0].source;
await fs.writeFile(filePath, output ?? contents);
const lintOutput = lintResult[0] && lintResult[0].output ? lintResult[0].output : lintResult[0].source;
if (lintOutput) {
await fs.writeFile(filePath, lintOutput);
}

alternatively you could switch from eslint.lintText to eslint.lintFiles

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

Successfully merging this pull request may close these issues.

2 participants