Skip to content

Commit

Permalink
Detect dependency loops in module migrator
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelalozano16 committed Jun 18, 2024
1 parent dce67db commit b3a6836
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.4
* Detect dependency loops in module migrator fix.

## 2.0.3

### Module Migrator
Expand Down
22 changes: 22 additions & 0 deletions lib/src/migrators/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
/// The values of the --forward flag.
final Set<ForwardType> forwards;

// Maps direct and indirect dependencies to prevent any potential loops.
final Map<Uri, Uri> _dependencies = {};

void _addDependency(Uri source, Uri importedPath) {
if (_dependencies.containsKey(importedPath) &&
_dependencies[importedPath] == source) {
// Throw an error indicating a potential loop.
var sourceUrl = _absoluteUrlToDependency(source);
var importedPathUrl = _absoluteUrlToDependency(importedPath);
throw MigrationException(
'Dependency loop detected: ${sourceUrl.item1} -> ${importedPathUrl.item1}');
}
_dependencies[source] = importedPath;
}

/// Constructs a new module migration visitor.
///
/// [importCache] must be the same one used by [references].
Expand Down Expand Up @@ -1224,6 +1239,13 @@ class _ModuleMigrationVisitor extends MigrationVisitor {
var url = declaration.sourceUrl;
if (url == currentUrl) return null;

// Trace dependencies for loop detection.
try {
_addDependency(currentUrl, url);
} on Exception catch (e) {
throw MigrationSourceSpanException(e.toString(), declaration.member.span);
}

// If we can load [declaration] from a library entrypoint URL, do so. Choose
// the shortest one if there are multiple options.
var libraryUrls = references.libraries[declaration];
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass_migrator
version: 2.0.3
version: 2.0.4
description: A tool for running migrations on Sass files
homepage: https://github.com/sass/migrator

Expand Down
21 changes: 21 additions & 0 deletions test/migrators/module/namespace_references/loop_error.hrx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<==> arguments
--migrate-deps

<==> input/entrypoint.scss
@import "ejemplo";
$var: $value;

<==> input/_ejemplo.scss
$value: blue;
a {
color: $var;
}

<==> error.txt
Error: Error: Dependency loop detected: entrypoint -> ejemplo
,
1 | $value: blue;
| ^^^^^^^^^^^^
'
_ejemplo.scss 1:1 root stylesheet
Migration failed!

0 comments on commit b3a6836

Please sign in to comment.