Skip to content
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

Warn instead of crashing for bad URLs #63

Merged
merged 5 commits into from
Jun 19, 2019
Merged

Warn instead of crashing for bad URLs #63

merged 5 commits into from
Jun 19, 2019

Conversation

jathak
Copy link
Member

@jathak jathak commented Jun 17, 2019

Fixes #57. Fixes #59.

The migrator will now print a warning for any URL that doesn't exist (both entrypoints and dependencies).

@jathak jathak requested a review from nex3 June 17, 2019 23:48
Copy link
Contributor

@nex3 nex3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned that this behavior, printing a warning for each unresolvable import, doesn't work well for some likely use-cases. Let's think through some scenarios where a user may have unresolvable imports:

  1. A user has a typo in a single import. The current behavior works well here: it shows them exactly where the typo is and what's wrong with it. This is also pretty unlikely, though, since the Sass compiler also won't be able to run on this code.

  2. A user is running a non-dependency-aware migrator like division and only cares about migrating their own stylesheets. Even once Gracefully handle dependencies from load paths and importers #58 is fixed, they won't want to pass load paths along with -d because they don't own their dependencies. Warnings about unresolvable imports are pure annoyance to them.

  3. A user is running a dependency-aware migrator like module. Whether or not they want to migrate their dependencies, the migrator needs to be able to load them to perform the migration. Printing a warning here is also wrong; it should be an error message.

Code-wise, I think this points to putting each each MigrationVisitor in charge of what to do if an import fails to load. The default visitor could aggregate all failed imports and print a single warning after the rest of the migration is done, to avoid cluttering the output for use case 2, and _ModuleMigrationVisitor could somehow override it to emit an error instead.

lib/src/utils.dart Outdated Show resolved Hide resolved
@jathak
Copy link
Member Author

jathak commented Jun 18, 2019

Okay, I changed the default to combine all warnings into one (when --verbose is passed, this combined warning will still list all of the missing dependencies, but in a shorter form then separate warnings would be).

The module migrator now throws a MigrationException, which the runner now catches and prints.

@jathak jathak requested a review from nex3 June 18, 2019 21:34
lib/src/migration_visitor.dart Outdated Show resolved Hide resolved
lib/src/migration_visitor.dart Outdated Show resolved Hide resolved
lib/src/migration_visitor.dart Outdated Show resolved Hide resolved
lib/src/migrator.dart Show resolved Hide resolved
lib/src/migrator.dart Show resolved Hide resolved
lib/src/migrator.dart Show resolved Hide resolved
lib/src/migrator.dart Show resolved Hide resolved
test/migrators/division/bad_paths_verbose.hrx Outdated Show resolved Hide resolved
@jathak jathak requested a review from nex3 June 19, 2019 21:59
@@ -34,6 +35,11 @@ abstract class MigrationVisitor extends RecursiveAstVisitor {
/// True if dependencies should be migrated as well.
final bool migrateDependencies;

/// Map of missing dependency URLs to the spans that import/use them.
UnmodifiableMapView<Uri, FileSpan> get missingDependencies =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UnmodifiableMapView<Uri, FileSpan> get missingDependencies =>
Map<Uri, FileSpan> get missingDependencies =>

if (stylesheet != null) {
visitStylesheet(stylesheet);
} else {
_missingDependencies[url] = context;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_missingDependencies[url] = context;
_missingDependencies.putIfAbsent(url, () => context);

This is minor, but I think it's more likely that an earlier import is more relevant to the user.

lib/src/migrator.dart Show resolved Hide resolved
@@ -26,6 +28,9 @@ abstract class Migrator extends Command<Map<Uri, String>> {
/// If true, dependencies will be migrated in addition to the entrypoints.
bool get migrateDependencies => globalResults['migrate-deps'] as bool;

/// Map of missing dependency URLs to the spans that import/use them.
final missingDependencies = <Uri, FileSpan>{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document how subclasses are intended to interact with this.

var visitor =
_DivisionMigrationVisitor(this.isPessimistic, migrateDependencies);
var result = visitor.run(entrypoint);
missingDependencies.addAll(visitor.missingDependencies);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleh, it sucks that each subclass has to do this manually rather than just having "warn for missing dependencies" be the automatic default behavior. It's not something that needs to happen in this PR, but it's worth thinking about how you could make this more automatic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why I originally passed in the list for the visitor to add to, but this is probably cleaner than that, particularly right now when there's only one migrator that actually uses missingDependencies. Created #64 for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crashes on input parse errors Crashes on a URL that can't be loaded
2 participants