diff --git a/CHANGELOG.md b/CHANGELOG.md index f2a876be..27bd1429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.0.4 +* Detect dependency loops in module migrator fix. + ## 2.0.3 ### Module Migrator diff --git a/lib/src/migrators/module.dart b/lib/src/migrators/module.dart index e1afbf80..71481ca4 100644 --- a/lib/src/migrators/module.dart +++ b/lib/src/migrators/module.dart @@ -199,6 +199,30 @@ class _ModuleMigrationVisitor extends MigrationVisitor { /// The values of the --forward flag. final Set forwards; + /// Dependencies where keys represent source URIs and values represent imported URIs. + final Map _dependencies = {}; + + /// Checks for dependency loops between source and imported paths. + /// + /// This method verifies whether importing a path introduces a circular dependency + /// by checking if the imported path is already mapped as a dependency of the source path. + /// + /// Throws a [MigrationException] if a dependency loop is detected. + /// + /// The [source] parameter is the path where the dependency is checked. + /// The [importedPath] parameter is the path being imported. + void _checkDependency(Uri source, Uri importedPath, FileSpan span) { + if (_dependencies.containsKey(importedPath) && + _dependencies[importedPath] == source) { + // Throw an error indicating a potential loop. + var (sourceUrl, _) = _absoluteUrlToDependency(source); + var (importedPathUrl, _) = _absoluteUrlToDependency(importedPath); + throw MigrationSourceSpanException( + 'Dependency loop detected: ${sourceUrl} -> ${importedPathUrl}', span); + } + _dependencies[source] = importedPath; + } + /// Constructs a new module migration visitor. /// /// [importCache] must be the same one used by [references]. @@ -1224,6 +1248,9 @@ class _ModuleMigrationVisitor extends MigrationVisitor { var url = declaration.sourceUrl; if (url == currentUrl) return null; + // Trace dependencies for loop detection. + _checkDependency(currentUrl, url, 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]; diff --git a/pubspec.yaml b/pubspec.yaml index df61c114..0b6ecd3e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/migrators/module/namespace_references/loop_error.hrx b/test/migrators/module/namespace_references/loop_error.hrx new file mode 100644 index 00000000..1f13c7ed --- /dev/null +++ b/test/migrators/module/namespace_references/loop_error.hrx @@ -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: Dependency loop detected: entrypoint -> ejemplo + , +1 | $value: blue; + | ^^^^^^^^^^^^ + ' + _ejemplo.scss 1:1 root stylesheet +Migration failed!