dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

AnalysisSession.getUnitElement returns null if the path contains a space #40974

Closed isoos closed 1 year ago

isoos commented 4 years ago

In the latest package:analyzer 0.39.4:

lib/without_space.dart:

import 'dart:io';

import 'package:analyzer/dart/analysis/context_builder.dart';
import 'package:analyzer/dart/analysis/context_locator.dart';

Future<void> main() async {
  final currentDir = Directory.current.path;
  final paths = ContextLocator().locateRoots(includedPaths: [currentDir]);
  final context = ContextBuilder().createContext(contextRoot: paths.single);
  final session = context.currentSession;
  final elem = await session.getUnitElement('$currentDir/lib/with space.dart');
  print(elem == null); // prints true

  final elem2 =
      await session.getUnitElement('$currentDir/lib/without_space.dart');
  print(elem2 == null); // prints false
}

lib/with space.dart:

class X {}

main() {
  print('hello');
}

Original error report: https://github.com/dart-lang/pana/issues/617

isoos commented 4 years ago

This is still present in package:analyzer 0.39.5

asashour commented 3 years ago

I guess this is fixed by NNBD, as I don't get the error:

  1. On Windows, after normalizing the path
  2. top_level_inference_test passes, when changing testFilePath to te st.dart in https://github.com/dart-lang/sdk/blob/a24d875ff6aa7caf00bb4bbc11975cc206d9265b/pkg/analyzer/test/src/dart/resolution/context_collection_resolution.dart#L261
srawlins commented 1 year ago

Closing as stale, as per @asashour 's comment. But please re-open if this reproduces on a recent release of analyzer.