gmlewis / flutter-stylizer

Flutter Stylizer is a VSCode extension that organizes your Flutter classes and mixins in an opinionated and consistent manner.
https://marketplace.visualstudio.com/items?itemName=gmlewis-vscode.flutter-stylizer
Apache License 2.0
23 stars 3 forks source link

Plugin is broken on Windows #19

Closed timsneath closed 3 years ago

timsneath commented 3 years ago

When I run the stylizer against this file:

// Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

import 'base.dart';
import 'com/IMetaDataImport2.dart';
import 'type_aliases.dart';

/// A custom (named) attribute.
class CustomAttribute extends TokenObject {
  final int modifiedObjectToken;
  final int attributeType;
  final Uint8List signatureBlob;

  CustomAttribute(IMetaDataImport2 reader, int token, this.modifiedObjectToken,
      this.attributeType, this.signatureBlob)
      : super(reader, token);

  /// Creates a custom attribute object from its given token.
  factory CustomAttribute.fromToken(IMetaDataImport2 reader, int token) {
    final ptkObj = calloc<mdToken>();
    final ptkType = calloc<mdToken>();
    final ppBlob = calloc<IntPtr>();
    final pcbBlob = calloc<ULONG>();

    try {
      final hr = reader.GetCustomAttributeProps(
          token, ptkObj, ptkType, ppBlob, pcbBlob);
      if (SUCCEEDED(hr)) {
        return CustomAttribute(
            reader,
            token,
            ptkObj.value,
            ptkType.value,
            Pointer<Uint8>.fromAddress(ppBlob.value)
                .asTypedList(pcbBlob.value));
      } else {
        throw WindowsException(hr);
      }
    } finally {
      free(pcbBlob);
      free(ppBlob);
      free(ptkType);
      free(ptkObj);
    }
  }
}

it duplicates the factory constructor, as so:

// Copyright (c) 2020, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:ffi';
import 'dart:typed_data';

import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';

import 'base.dart';
import 'com/IMetaDataImport2.dart';
import 'type_aliases.dart';

/// A custom (named) attribute.
class CustomAttribute extends TokenObject {
  CustomAttribute(IMetaDataImport2 reader, int token, this.modifiedObjectToken,
      this.attributeType, this.signatureBlob)
      : super(reader, token);

  factory CustomAttribute.fromToken(IMetaDataImport2 reader, int token) {
    final ptkObj = calloc<mdToken>();
    final ptkType = calloc<mdToken>();
    final ppBlob = calloc<IntPtr>();
    final pcbBlob = calloc<ULONG>();

    try {
      final hr = reader.GetCustomAttributeProps(
          token, ptkObj, ptkType, ppBlob, pcbBlob);
      if (SUCCEEDED(hr)) {
        return CustomAttribute(
            reader,
            token,
            ptkObj.value,
            ptkType.value,
            Pointer<Uint8>.fromAddress(ppBlob.value)
                .asTypedList(pcbBlob.value));
      } else {
        throw WindowsException(hr);
      }
    } finally {
      free(pcbBlob);
      free(ppBlob);
      free(ptkType);
      free(ptkObj);
    }
  }

  final int attributeType;
  final int modifiedObjectToken;
  final Uint8List signatureBlob;

  /// Creates a custom attribute object from its given token.
  factory CustomAttribute.fromToken(IMetaDataImport2 reader, int token) {
    final ptkObj = calloc<mdToken>();
    final ptkType = calloc<mdToken>();
    final ppBlob = calloc<IntPtr>();
    final pcbBlob = calloc<ULONG>();

    try {
      final hr = reader.GetCustomAttributeProps(
          token, ptkObj, ptkType, ppBlob, pcbBlob);
      if (SUCCEEDED(hr)) {
        return CustomAttribute(
            reader,
            token,
            ptkObj.value,
            ptkType.value,
            Pointer<Uint8>.fromAddress(ppBlob.value)
                .asTypedList(pcbBlob.value));
      } else {
        throw WindowsException(hr);
      }
    } finally {
      free(pcbBlob);
      free(ppBlob);
      free(ptkType);
      free(ptkObj);
    }
  }
}
gmlewis commented 3 years ago

Yikes! Thank you for the report, @timsneath ! I'll investigate.

gmlewis commented 3 years ago

@timsneath - I'm unable to duplicate the problem with the default configuration... please see my new unit test here: https://github.com/gmlewis/flutter-stylizer/commit/275997b13e72e2e0b08eaf8e671b8496d0804777

Could you please share your configuration settings to see if that is causing the problem?

timsneath commented 3 years ago

Hmm -- that's interesting / weird. I literally just grabbed the extension and ran it against my repo: https://github.com/timsneath/winmd

(this file is lib/src/customattribute.dart)

I didn't change anything in terms of config. Although perhaps there's something odd in the latest Dart master build? flutter doctor reports:

[C:\src\winmd] flutter doctor -v
[✓] Flutter (Channel master, 2.2.0-11.0.pre.123, on Microsoft Windows [Version 10.0.21354.1], locale en-US)
    • Flutter version 2.2.0-11.0.pre.123 at c:\flutter
    • Upstream repository git@github.com:flutter/flutter.git
    • Framework revision b52eebb8d4 (27 hours ago), 2021-04-14 20:04:02 -0400
    • Engine revision 1a9cd85bc5
    • Dart version 2.14.0 (build 2.14.0-4.0.dev)

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.3)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.9.31129.286
    • Windows 10 SDK version 10.0.19041.0

[✓] VS Code (version 1.55.2)
    • VS Code at C:\Users\tim\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.21.0

[✓] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.21354.1]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 89.0.4389.114
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 89.0.774.76

• No issues found!
gmlewis commented 3 years ago

It looks like I'm the one who is way out-of-date:

$ flutter doctor -v
  ╔════════════════════════════════════════════════════════════════════════════╗
  ║ A new version of Flutter is available!                                     ║
  ║                                                                            ║
  ║ To update to the latest version, run "flutter upgrade".                    ║
  ╚════════════════════════════════════════════════════════════════════════════╝

[✓] Flutter (Channel beta, 1.24.0-10.2.pre, on Linux, locale en_US.UTF-8)
    • Flutter version 1.24.0-10.2.pre at /home/glenn/tools/flutter
    • Framework revision 022b333a08 (5 months ago), 2020-11-18 11:35:09 -0800
    • Engine revision 07c1eed46b
    • Dart version 2.12.0 (build 2.12.0-29.10.beta)

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.55.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.21.0

[✓] Connected device (2 available)
    • Web Server (web) • web-server • web-javascript • Flutter Tools
    • Chrome (web)     • chrome     • web-javascript • Google Chrome 90.0.4430.72

! Doctor found issues in 2 categories.

I'll try upgrading and I'll report back what happens. Thanks, Tim!

gmlewis commented 3 years ago

Hmmm... I just upgraded flutter, and still can't duplicate the problem:

$ flutter doctor -v
[✓] Flutter (Channel master, 2.2.0-11.0.pre.136, on Linux, locale en_US.UTF-8)
    • Flutter version 2.2.0-11.0.pre.136 at /home/glenn/tools/flutter
    • Upstream repository ssh://git@github.com/flutter/flutter.git
    • Framework revision f789acae41 (3 hours ago), 2021-04-15 19:14:02 -0500
    • Engine revision 0ee8f267df
    • Dart version 2.14.0 (build 2.14.0-4.0.dev)

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code (version 1.55.2)
    • VS Code at /usr/share/code
    • Flutter extension version 3.21.0

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 90.0.4430.72

! Doctor found issues in 2 categories.

I'm running on Linux and it looks like you are running on Windows... Maybe this is a VSCode issue?!?!?

gmlewis commented 3 years ago

I'll leave this issue open in case anyone can help me duplicate the problem on Linux.

timsneath commented 3 years ago

Thanks. I'll try and get a solid repro for you.

On Fri, Apr 16, 2021 at 1:26 PM Glenn Lewis @.***> wrote:

I'll leave this issue open in case anyone can help me duplicate the problem on Linux.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gmlewis/flutter-stylizer/issues/19#issuecomment-821542019, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARWL67DZKQRYOUW7IUXZP3TJCMRFANCNFSM43ARVRUQ .

gmlewis commented 3 years ago

I found the problem!!! I fired up VirtualBox and set up VSCode on Windows 10, and started looking into the problem.

Would you believe it if I told you that this TypeScript/JavaScript regular expression:

const commentRE = /^(.*?)\s*\/\/.*$/
const m = commentRE.exec(line)

for this input: // value xor isInfinity returns:

m=['// value xor isInfinity', ''] // on Linux
m=null // on Windows

What the heck?!?!? There are not even any \r or \n characters in the string!!!

So now I'm trying to find out why the regular expression is behaving differently on Windows. Wish me luck.

gmlewis commented 3 years ago

Apparently it is a known VSCode issue!

Thanks to this comment: https://github.com/microsoft/vscode/issues/50001#issuecomment-389686400 I found the workaround:

BEFORE:

const commentRE = /^(.*?)\s*\/\/.*$/

AFTER:

const commentRE = /^(.*?)\s*\/\/.*\r?$/

So I guess I lied before... there actually was an embedded \r in there somehow even though the file was unix-style formatted?!? Again I find this incredible, but now I'll try to see if I can release a version that works on Windows as well with this workaround.

gmlewis commented 3 years ago

Would you also believe that this regexp:

const matchClassRE = /^(?:abstract\s+)?class\s+(\S+).*$/mg

behaves differently on VSCode-Windows and VSCode-Linux? And this time, \r doesn't even fix the problem!

I now believe I've solved the issue with a global workaround and should have v0.0.20 ready shortly to try out.

gmlewis commented 3 years ago

@timsneath - this should now be fixed on Windows by release https://github.com/gmlewis/flutter-stylizer/releases/tag/v0.0.20, as all unit tests now pass on Windows VSCode as well. Closing ticket.

Please re-open if you still find bugs, and thank you for the report!!!

timsneath commented 3 years ago

Congratulations! Confirm a fix on my side. (Although I discovered another one -- stay tuned...)

gmlewis commented 3 years ago

OK, cool, thanks, @timsneath! And thanks for helping me make my plugin better! :joy: (Sorry for the problems, though!)