Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Make error handling optionally more permissive. #58

Merged
merged 11 commits into from
Feb 11, 2020
Merged

Conversation

lrhn
Copy link
Member

@lrhn lrhn commented Feb 6, 2020

Fixes a bug.

@lrhn lrhn requested a review from jensjoha February 6, 2020 13:29
onError(PackageConfigFormatException(e.message, e.source, e.offset));
return null;
} on Object catch (e) {
print("WTF: $e");
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe not...

Copy link
Member Author

Choose a reason for hiding this comment

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

Debug-print, FTW!

@jensjoha
Copy link
Contributor

jensjoha commented Feb 6, 2020

For the following packages file:

foo:lib/#dart=2.6

I get 3 errors:

org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Location URI must not have query or fragment
org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Not an absolute URI with no query or fragment with a path ending in /
org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Not an absolute URI with no query or fragment with a path ending in /

Also - even though it's a FormatException with support for a location, there is none...

checkValidVersionNumber(languageVersion) >= 0) {
onError(PackageConfigArgumentError(languageVersion, "languageVersion",
"Invalid language version format"));
languageVersion = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we get a special marker for invalid language version?

Copy link
Member Author

Choose a reason for hiding this comment

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

What kind of "special marker"?

Copy link
Contributor

Choose a reason for hiding this comment

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

So a client can tell that a language version was set, although it was invalid. It my suggested hack-patch I simply overwrite the string "invalid" instead of null --- though something more elegant is surely possible...

@jensjoha
Copy link
Contributor

jensjoha commented Feb 6, 2020

{
    "configVersion": 2,
    "packages": [
        {
            "name": "foo",
            "rootUri": "../lib/",
            "languageVersion": "arglebargle",
            "languageVersion": "2.5"
        }
    ]
}

just gets language version 2.5.

@jensjoha
Copy link
Contributor

jensjoha commented Feb 6, 2020

While this might not be pretty, this fixes the most severe issues I have:

diff --git a/lib/src/package_config_impl.dart b/lib/src/package_config_impl.dart
index fb52a23..676854f 100644
--- a/lib/src/package_config_impl.dart
+++ b/lib/src/package_config_impl.dart
@@ -223,7 +223,7 @@ class SimplePackage implements Package {
         checkValidVersionNumber(languageVersion) >= 0) {
       onError(PackageConfigArgumentError(languageVersion, "languageVersion",
           "Invalid language version format"));
-      languageVersion = null;
+      languageVersion = "invalid";
     }
     if (fatalError) return null;
     return SimplePackage._(
diff --git a/lib/src/package_config_json.dart b/lib/src/package_config_json.dart
index 6528526..d3dd69e 100644
--- a/lib/src/package_config_json.dart
+++ b/lib/src/package_config_json.dart
@@ -238,7 +238,8 @@ PackageConfig parsePackageConfigJson(
           packageUri = checkType<String>(value, _packageUriKey, name);
           break;
         case _languageVersionKey:
-          languageVersion = checkType<String>(value, _languageVersionKey, name);
+          languageVersion =
+              checkType<String>(value, _languageVersionKey, name) ?? "invalid";
           break;
         default:
           (extraData ??= {})[key] = value;
diff --git a/lib/src/packages_file.dart b/lib/src/packages_file.dart
index 1bf3bf6..7e547ff 100644
--- a/lib/src/packages_file.dart
+++ b/lib/src/packages_file.dart
@@ -86,6 +86,12 @@ PackageConfig parse(
     if (packageLocation.hasQuery || packageLocation.hasFragment) {
       onError(PackageConfigFormatException(
           "Location URI must not have query or fragment", source, start));
+      packageLocation = new Uri(
+          scheme: packageLocation.scheme,
+          userInfo: packageLocation.userInfo,
+          host: packageLocation.host,
+          port: packageLocation.port,
+          path: packageLocation.path);
     }
     if (!packageLocation.path.endsWith('/')) {
       packageLocation =

It does not add offsets, but that's only a minor issue I think.
It also doesn't fix the json parsing taking the most reason value for a key, but offline you talked about that getting fixes once you do a more performant version of this package, so I can probably wait for that...

@lrhn
Copy link
Member Author

lrhn commented Feb 7, 2020

For the following packages file:

foo:lib/#dart=2.6

I get 3 errors:

org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Location URI must not have query or fragment
org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Not an absolute URI with no query or fragment with a path ending in /
org-dartlang-test:///a/b/c/.packages: Error: Problem in packages configuration file: Not an absolute URI with no query or fragment with a path ending in /

Also - even though it's a FormatException with support for a location, there is none...

That's a lot of errors.
It's explainable ... while I report the error in the parser, I don't correct it, so it later tries to use the invalid path, it's detected again (in the SimplePackage factory).

I'll try fixing the issue instead.

lrhn added 2 commits February 7, 2020 11:14
Make more errors be format exceptions with offset.
Don't continue with an invalid value. Instead either sanitize the value, if possible, ignore it if it was optional, or bail out if it is required.
Allow invalid language versions to be detectable.
- Do not require root URIs to have paths starting with `/`. That
only makes sense for `file` or `http`, and they enforce it anyway.
- Fixed bug in language version validation not accepting the digit `9`.

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing entry for language version becoming an object rather than a string.

throw PackageConfigFormatException(e.message, e.invalidValue);

LanguageVersion /*?*/ version;
if (languageVersion != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

languageVersion is null if it's not a string in the json format (line 240 --- for some reason I cannot mark that line).

pubspec.yaml Outdated
@@ -1,15 +1,11 @@
name: package_config
version: 2.0.0
name: package_config_2
Copy link
Contributor

Choose a reason for hiding this comment

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

This would require tests to be updated too, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should match, I forgot to change this one back after running tests.

@lrhn lrhn force-pushed the permissive-parsing branch from fb180d4 to d7f133c Compare February 7, 2020 12:49
@lrhn lrhn force-pushed the permissive-parsing branch from d7f133c to cbd07f9 Compare February 7, 2020 12:50
Copy link
Contributor

@jensjoha jensjoha left a comment

Choose a reason for hiding this comment

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

LGTM.

@lrhn lrhn merged commit 96ea916 into master Feb 11, 2020
@kevmoo kevmoo deleted the permissive-parsing branch February 21, 2020 19:48
mosuem pushed a commit to dart-lang/tools that referenced this pull request Dec 9, 2024
…nfig#58)

* Make error handling optionally more permissive.
* Make the LanguageVersion be an object, not a string.
* Make it version 3.0.0-dev
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants