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

Better yaml serialization for pubspec.lock #4214

Open
sigurdm opened this issue Apr 11, 2024 · 2 comments
Open

Better yaml serialization for pubspec.lock #4214

sigurdm opened this issue Apr 11, 2024 · 2 comments
Labels
type-enhancement A request for a change that isn't a bug

Comments

@sigurdm
Copy link
Contributor

sigurdm commented Apr 11, 2024

Sometimes the content hash is quoted, sometimes not.

image

We use a quite naive yaml serialization for writing pubspec.lock.

Here is the regex that decides is a string needs to be json-encoded before serializing:

final _unquotableYamlString = RegExp(r'^[a-zA-Z_-][a-zA-Z_0-9-]*$');

/// The subset of strings that don't need quoting in YAML.
///
/// This pattern does not strictly follow the plain scalar grammar of YAML,
/// which means some strings may be unnecessarily quoted, but it's much simpler.
final _unquotableYamlString = RegExp(r'^[a-zA-Z_-][a-zA-Z_0-9-]*$');

We could consider using yaml_edit to write the pubspec.lock file

@sigurdm sigurdm added the type-enhancement A request for a change that isn't a bug label Apr 11, 2024
@sigurdm
Copy link
Contributor Author

sigurdm commented Apr 11, 2024

Looking into the quoting rules, I think there is a bug here.

'true' matches this regexp, but should be quoted in yaml.

That means the following test breaks:

import 'package:test/test.dart';

import 'descriptor.dart';
import 'test_pub.dart';

void main() {
  test('handles package "no"', () async {
    // final server = await servePackages();
    // server.serve('true', '1.0.0');
    await git('pkg', [
      libPubspec('pkg', '1.0.0'),
    ]).create();

    await git('pkg', []).runGit(['branch', '-C', 'true']);

    await appDir(
      dependencies: {
        'pkg': {
          'git': {'url': '../pkg', 'ref': 'true'},
        },
      },
    ).create();
    await pubGet();
    await pubGet();
  });
}

With:

[e] ERR : Failed parsing lock file:
[e]     | 
[e]     | Error on line 7, column 7 of pubspec.lock: The 'ref' field of the description must be a string.
[e]     |    ╷
[e]     | 7  │ ┌       path: "."
[e]     | 8  │ │       ref: true
[e]     | 9  │ │       resolved-ref: c43723560f742af8bf6d7836d3f87fe4fee0681f
[e]     | 10 │ │       url: "../pkg"
[e]     | 11 │ │     source: git
[e]     |    │ └────^
[e]     |    ╵
[e]     | 
[e]     | Consider deleting the file and running `dart pub get` to recreate it.

@sigurdm
Copy link
Contributor Author

sigurdm commented Apr 11, 2024

Fixing this for hashes starting with a digit might cause most pubspec.locks to be updated. Not sure it is worth it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

1 participant