Skip to content

Commit

Permalink
fix: Only report invalid dart endpoint definition files when using wa…
Browse files Browse the repository at this point in the history
…tch. (serverpod#2963)
  • Loading branch information
SandPod authored Nov 15, 2024
1 parent 4e36a51 commit 1b56a92
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 210 deletions.
4 changes: 0 additions & 4 deletions tools/serverpod_cli/lib/src/analyzer/dart/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ class EndpointDefinition {
/// The methods this endpoint defines.
final List<MethodDefinition> methods;

/// The subdirectories this endpoints dart file is stored in,
final List<String> subDirParts;

/// Create a new [EndpointDefinition].
const EndpointDefinition({
required this.name,
required this.documentationComment,
required this.methods,
required this.className,
required this.filePath,
required this.subDirParts,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:path/path.dart' as path;
import 'package:serverpod_cli/src/analyzer/code_analysis_collector.dart';
import 'package:serverpod_cli/src/analyzer/dart/definitions.dart';
import 'package:serverpod_cli/src/analyzer/dart/element_extensions.dart';
Expand All @@ -11,20 +10,17 @@ abstract class EndpointClassAnalyzer {
ClassElement element,
List<MethodDefinition> methodDefinitions,
String filePath,
String rootPath,
) {
var className = element.name;
var endpointName = _formatEndpointName(className);
var classDocumentationComment = element.documentationComment;
var subDirectoryParts = _getSubdirectoryParts(filePath, rootPath);

return EndpointDefinition(
name: endpointName,
documentationComment: classDocumentationComment,
className: className,
methods: methodDefinitions,
filePath: filePath,
subDirParts: subDirectoryParts,
);
}

Expand Down Expand Up @@ -73,21 +69,4 @@ abstract class EndpointClassAnalyzer {

return endpointName;
}

static List<String> _getSubdirectoryParts(String filePath, String rootPath) {
// Get the subdirectory of the filePath by removing the first elements
// of the root path and the file path as long as they match.
var rootPathParts = path.split(rootPath);
var fileDirPathParts = path.split(path.dirname(filePath));
while (rootPathParts.isNotEmpty && fileDirPathParts.isNotEmpty) {
if (rootPathParts.first == fileDirPathParts.first) {
rootPathParts.removeAt(0);
fileDirPathParts.removeAt(0);
} else {
break;
}
}

return fileDirPathParts;
}
}
28 changes: 15 additions & 13 deletions tools/serverpod_cli/lib/src/analyzer/dart/endpoints_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class EndpointsAnalyzer {

/// Create a new [EndpointsAnalyzer], containing a
/// [AnalysisContextCollection] that analyzes all dart files in the
/// provided [endpointDirectory].
EndpointsAnalyzer(Directory endpointDirectory)
/// provided [directory].
EndpointsAnalyzer(Directory directory)
: collection = AnalysisContextCollection(
includedPaths: [endpointDirectory.absolute.path],
includedPaths: [directory.absolute.path],
resourceProvider: PhysicalResourceProvider.INSTANCE,
);

Expand All @@ -37,9 +37,14 @@ class EndpointsAnalyzer {

var endpointDefs = <EndpointDefinition>[];

List<(ResolvedLibraryResult, String, String)> validLibraries = [];
List<(ResolvedLibraryResult, String)> validLibraries = [];
Map<String, int> endpointClassMap = {};
await for (var (library, filePath, rootPath) in _libraries) {
await for (var (library, filePath) in _libraries) {
var endpointClasses = _getEndpointClasses(library);
if (endpointClasses.isEmpty) {
continue;
}

var maybeDartErrors = await _getErrorsForFile(library.session, filePath);
if (maybeDartErrors.isNotEmpty) {
collector.addError(
Expand All @@ -55,7 +60,7 @@ class EndpointsAnalyzer {
continue;
}

for (var endpointClass in _getEndpointClasses(library)) {
for (var endpointClass in endpointClasses) {
var className = endpointClass.name;
endpointClassMap.update(
className,
Expand All @@ -64,15 +69,15 @@ class EndpointsAnalyzer {
);
}

validLibraries.add((library, filePath, rootPath));
validLibraries.add((library, filePath));
}

var duplicateEndpointClasses = endpointClassMap.entries
.where((entry) => entry.value > 1)
.map((entry) => entry.key)
.toSet();

for (var (library, filePath, rootPath) in validLibraries) {
for (var (library, filePath) in validLibraries) {
var severityExceptions = _validateLibrary(
library,
filePath,
Expand All @@ -86,7 +91,6 @@ class EndpointsAnalyzer {
library,
collector,
filePath,
rootPath,
failingExceptions,
));
}
Expand Down Expand Up @@ -116,7 +120,6 @@ class EndpointsAnalyzer {
ResolvedLibraryResult library,
CodeAnalysisCollector collector,
String filePath,
String rootPath,
Map<String, List<SourceSpanSeverityException>> validationErrors,
) {
var topElements = library.element.topLevelElements;
Expand Down Expand Up @@ -150,7 +153,6 @@ class EndpointsAnalyzer {
classElement,
methodDefs,
filePath,
rootPath,
);

endpointDefs.add(endpointDefinition);
Expand Down Expand Up @@ -209,7 +211,7 @@ class EndpointsAnalyzer {
return validationErrors;
}

Stream<(ResolvedLibraryResult, String, String)> get _libraries async* {
Stream<(ResolvedLibraryResult, String)> get _libraries async* {
for (var context in collection.contexts) {
var analyzedFiles = context.contextRoot.analyzedFiles().toList();
analyzedFiles.sort();
Expand All @@ -219,7 +221,7 @@ class EndpointsAnalyzer {
for (var filePath in analyzedDartFiles) {
var library = await context.currentSession.getResolvedLibrary(filePath);
if (library is ResolvedLibraryResult) {
yield (library, filePath, context.contextRoot.root.path);
yield (library, filePath);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions tools/serverpod_cli/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ class GeneratorConfig {
/// server package.
List<String> get generatedServeModelPackagePathParts => ['src', 'generated'];

/// The path parts of the generated endpoint file.
List<String> get generatedServerEndpointFilePathParts =>
[...generatedServeModelPathParts, 'endpoints.dart'];

/// The path parts of the generated protocol file.
List<String> get generatedServerProtocolFilePathParts =>
[...generatedServeModelPathParts, 'protocol.dart'];

/// The path parts of the directory, where the generated code is stored in the
/// server package.
List<String> get generatedServeModelPathParts => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,13 +757,15 @@ class LibraryGenerator {
]).code;
}

String? _generatedDirectoryPathCache;
String _buildGeneratedDirectoryPath() => _generatedDirectoryPathCache ??=
p.joinAll([...config.generatedServeModelPathParts]);

String _endpointPath(EndpointDefinition endpoint) {
return p.posix.joinAll([
'..',
'endpoints',
...endpoint.subDirParts,
p.basename(endpoint.filePath),
]);
return p.relative(
endpoint.filePath,
from: _buildGeneratedDirectoryPath(),
);
}

Code _buildEndpointLookupMap(List<EndpointDefinition> endpoints) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class DartServerCodeGenerator extends CodeGenerator {
);

var codeMap = {
p.joinAll([...config.generatedServeModelPathParts, 'protocol.dart']):
p.joinAll([...config.generatedServerProtocolFilePathParts]):
serverClassGenerator.generateProtocol().generateCode(),
p.joinAll([...config.generatedServeModelPathParts, 'endpoints.dart']):
p.joinAll([...config.generatedServerEndpointFilePathParts]):
serverClassGenerator.generateServerEndpointDispatch().generateCode(),
};

Expand Down
40 changes: 0 additions & 40 deletions tools/serverpod_cli/lib/src/run/file_watcher.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class EndpointDefinitionBuilder {
String _className = 'ExampleEndpoint';
String _filePath = 'example.dart';
List<MethodDefinition> _methods = [];
List<String> _subDirParts = [];

EndpointDefinitionBuilder();

Expand Down Expand Up @@ -36,19 +35,13 @@ class EndpointDefinitionBuilder {
return this;
}

EndpointDefinitionBuilder withSubDirParts(List<String> subDirParts) {
_subDirParts = subDirParts;
return this;
}

EndpointDefinition build() {
return EndpointDefinition(
name: _name,
documentationComment: _documentationComment,
className: _className,
filePath: _filePath,
methods: _methods,
subDirParts: _subDirParts,
);
}
}
Loading

0 comments on commit 1b56a92

Please sign in to comment.