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

addXFileEdit with customPath does not save file #296

Open
unitmdev opened this issue Dec 4, 2024 · 0 comments
Open

addXFileEdit with customPath does not save file #296

unitmdev opened this issue Dec 4, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@unitmdev
Copy link

unitmdev commented Dec 4, 2024

Describe the bug

Applying a DartFix to a customPath does not save the file. Reading the file later with File(path).readAsStringSync() ignores any fixes applied using addXFileEdit until customPath is saved. All unsaved changes are ignored when reading file contents.

To Reproduce

class SomeFix extends DartFix {
  @override
  void run(
    CustomLintResolver resolver,
    ChangeReporter reporter,
    CustomLintContext context,
    AnalysisError analysisError,
    List<AnalysisError> others,
  ) {
    context.registry.addNode((node) {
      final file = File('${Directory.current.path}/lib/foo.txt');
      final fileContents = file.readAsStringSync();

      if (fileContents == '') {
        reporter
            .createChangeBuilder(
              message: 'Fill empty file',
              priority: 0,
            )
            .addGenericFileEdit(
              (builder) => builder.addSimpleInsertion(0, 'foo'),
              customPath: file.path,
            );
      }
    });
  }
}

Expected behavior

Applying SomeFix should only be possible once. After foo is added to the file, the ChangeBuilder shouldn't get created.

I noticed that the analyzer plugin gets file contents from ResourceProvider, but I don't see how to access that. There must be a more reliable way to read the file contents than File(path).readAsStringSync(), or there must be a way to save the customPath file.

@unitmdev unitmdev added the bug Something isn't working label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants