简体   繁体   中英

Dart AST library fails when compiled

I'm building a program that use the Dart's AST library , and it works fine as long as I use the Dart interpreter to run the program ( dart filename.dart ).

Once I want to compile the program ( dart compile filename.dart ), the program can't load the file and I have this stacktrace:

#0      _PhysicalFile.readAsStringSync (package:analyzer/file_system/physical_file_system.dart:184)
#1      FolderBasedDartSdk.languageVersion (package:analyzer/src/dart/sdk/sdk.dart:400)
#2      FeatureSetProvider.build (package:analyzer/src/dart/analysis/feature_set_provider.dart:143)
#3      AnalysisDriver._createFileTracker (package:analyzer/src/dart/analysis/driver.dart:1500)
#4      new AnalysisDriver (package:analyzer/src/dart/analysis/driver.dart:291)
#5      ContextBuilder.buildDriver (package:analyzer/src/context/builder.dart:119)
#6      ContextBuilderImpl.createContext (package:analyzer/src/dart/analysis/context_builder.dart:94)
#7      new AnalysisContextCollectionImpl (package:analyzer/src/dart/analysis/analysis_context_collection.dart:55)
#8      _createAnalysisContext (package:analyzer/dart/analysis/utilities.dart:125)
#9      resolveFile (package:analyzer/dart/analysis/utilities.dart:115)
#10     main (package:DartProjects/dartprojects.dart:122)
#11     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299)
#12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168)

I took a look at the source code to see where could be the error, and it seems that in package:analyzer/src/dart/sdk/sdk.dart it tries to get the langage version file, but instead of using the PATH to know where my dart sdk is, it tries to find it in my InteliJ folder, which fails. Also, I tried to run it on a freshly created VM, and it fails too.

Here is the code that produce this output:

import 'dart:io';
import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

void main(List<String> arguments) async {
  final fileName = Directory.current.path + r'\test.dart';

  var source = null;
  try {
    source = await resolveFile(path: fileName);
  } catch (e, s) {
    print('${s}');
    return;
  }
}

Thanks for your help.

Not a good workaround.

Copy the "version" file from the Dart SDK folder to the root of your project.
The analyzer will take it and use it.
This will work if your compiled file is in the "bin" folder.

This is not a good workaround.

PS
This is called a hack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM