Skip to content

addXFileEdit with customPath does not save file #296

@unitmdev

Description

@unitmdev

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.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions