Skip to content

Fixes lingering issues to support workspaces in CLI #336

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/custom_lint/lib/src/workspace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,9 @@ Stream<YamlMap> visitAnalysisOptionAndIncludes(
File analysisOptionsFile,
) async* {
final visited = <String>{};
late final packageConfigFuture = loadPackageConfig(
File(
join(analysisOptionsFile.parent.path, '.dart_tool/package_config.json'),
),
).then<PackageConfig?>(
late final packageConfigFuture =
loadPackageConfig(analysisOptionsFile.parent.packageConfig)
.then<PackageConfig?>(
(value) => value,
// On error, return null to not throw. The function later handles the null
onError: (e, s) => null,
Expand Down Expand Up @@ -418,7 +416,14 @@ String? _findOptionsForPubspec(String pubspecPath) {
Iterable<String> _findRoots(String path) sync* {
final directory = Directory(path);

yield* directory.listSync(recursive: true).whereType<File>().where((file) {
yield* directory
.listSync(
recursive: true,
// Do not follow symbolic links (can be problematic in flutter platform folders)
followLinks: false,
)
.whereType<File>()
.where((file) {
final fileName = basename(file.path);
if (fileName != 'pubspec.yaml' && fileName != 'analysis_options.yaml') {
return false;
Expand Down
32 changes: 18 additions & 14 deletions packages/custom_lint_core/lib/src/package_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ extension PackageIOUtils on Directory {
File get pubspecOverrides => file('pubspec_overrides.yaml');

/// The `.dart_tool/package_config.json` file.
File get packageConfig => file('.dart_tool', 'package_config.json');
File get _packageConfig => file('.dart_tool', 'package_config.json');

/// The `.dart_tool/pub/workspace_ref.json` file.
File get workspaceRef => file('.dart_tool', 'pub', 'workspace_ref.json');
/// The `.dart_tool/package_config.json` file. Either from the current
/// directory or the workspace root.
File get packageConfig {
if (_packageConfig.existsSync()) return _packageConfig;

final workspaceRefFile = file('.dart_tool', 'pub', 'workspace_ref.json');
if (!workspaceRefFile.existsSync()) return _packageConfig;

final content = workspaceRefFile.readAsStringSync();
final json = jsonDecode(content) as Map<String, dynamic>;
final workspaceRoot = json['workspaceRoot'] as String;
final workspacePath =
normalize(join(workspaceRefFile.parent.path, workspaceRoot));
final workspaceDir = Directory(workspacePath);
return workspaceDir._packageConfig;
}

/// Returns a path relative to the given [other].
String relativeTo(FileSystemEntity other) {
Expand Down Expand Up @@ -136,17 +150,7 @@ Future<PackageConfig?> tryParsePackageConfig(Directory directory) async {
/// Throws if the parsing fails, such as if the file is badly formatted or
/// does not exists.
Future<PackageConfig> parsePackageConfig(Directory directory) async {
var packageConfigFile = directory.packageConfig;
if (!packageConfigFile.existsSync()) {
final workspaceRefFile = directory.workspaceRef;
final content = workspaceRefFile.readAsStringSync();
final json = jsonDecode(content) as Map<String, dynamic>;
final workspaceRoot = json['workspaceRoot'] as String;
final workspacePath =
normalize(join(workspaceRefFile.parent.path, workspaceRoot));
final workspaceDir = Directory(workspacePath);
packageConfigFile = workspaceDir.packageConfig;
}
final packageConfigFile = directory.packageConfig;

return PackageConfig.parseBytes(
await packageConfigFile.readAsBytes(),
Expand Down