Skip to content

Commit c886dde

Browse files
authored
Change to only look for package_config.json if asked for .packages by name. (dart-archive/package_config#78)
Some clean-up.
1 parent c1b6ac1 commit c886dde

File tree

8 files changed

+63
-66
lines changed

8 files changed

+63
-66
lines changed

pkgs/package_config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.9.2
2+
3+
- Updated to support new rules for picking `package_config.json` over
4+
a specified `.packages`.
5+
16
## 1.9.1
27

38
- Remove accidental transitive import of `dart:io` from entrypoints that are

pkgs/package_config/lib/package_config.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ export "package_config_types.dart";
2424
/// It is considered a `package_config.json` file if its first character
2525
/// is a `{`.
2626
///
27-
/// If the file is a `.packages` file and [preferNewest] is true, the default,
28-
/// also checks if there is a `.dart_tool/package_config.json` file next to the original file,
29-
/// and if so, loads that instead.
27+
/// If the file is a `.packages` file (the file name is `.packages`)
28+
/// and [preferNewest] is true, the default, also checks if there is
29+
/// a `.dart_tool/package_config.json` file next
30+
/// to the original file, and if so, loads that instead.
3031
/// If [preferNewest] is set to false, a directly specified `.packages` file
3132
/// is loaded even if there is an available `package_config.json` file.
3233
/// The caller can determine this from the [PackageConfig.version]
@@ -50,6 +51,7 @@ Future<PackageConfig> loadPackageConfig(File file,
5051
/// non-whitespace character is a `{`.
5152
///
5253
/// If [preferNewest] is true, the default, and the file is a `.packages` file,
54+
/// as determined by its file name being `.packages`,
5355
/// first checks if there is a `.dart_tool/package_config.json` file
5456
/// next to the original file, and if so, loads that instead.
5557
/// The [file] *must not* be a `package:` URI.

pkgs/package_config/lib/src/package_config_io.dart

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,21 @@ import "util_io.dart";
3030
/// The file must exist and be a normal file.
3131
Future<PackageConfig> readAnyConfigFile(
3232
File file, bool preferNewest, void onError(Object error)) async {
33+
if (preferNewest && fileName(file.path) == ".packages") {
34+
var alternateFile =
35+
File(pathJoin(dirName(file.path), ".dart_tool", "package_config.json"));
36+
if (alternateFile.existsSync()) {
37+
return await readPackageConfigJsonFile(alternateFile, onError);
38+
}
39+
}
3340
Uint8List bytes;
3441
try {
3542
bytes = await file.readAsBytes();
3643
} catch (e) {
3744
onError(e);
3845
return const SimplePackageConfig.empty();
3946
}
40-
var firstChar = firstNonWhitespaceChar(bytes);
41-
if (firstChar != $lbrace) {
42-
// Definitely not a JSON object, probably a .packages.
43-
if (preferNewest) {
44-
var alternateFile = File(
45-
pathJoin(dirName(file.path), ".dart_tool", "package_config.json"));
46-
if (alternateFile.existsSync()) {
47-
Uint8List /*?*/ bytes;
48-
try {
49-
bytes = await alternateFile.readAsBytes();
50-
} catch (e) {
51-
onError(e);
52-
return const SimplePackageConfig.empty();
53-
}
54-
if (bytes != null) {
55-
return parsePackageConfigBytes(bytes, alternateFile.uri, onError);
56-
}
57-
}
58-
}
59-
return packages_file.parse(bytes, file.uri, onError);
60-
}
61-
return parsePackageConfigBytes(bytes, file.uri, onError);
47+
return parseAnyConfigFile(bytes, file.uri, onError);
6248
}
6349

6450
/// Like [readAnyConfigFile] but uses a URI and an optional loader.
@@ -73,11 +59,24 @@ Future<PackageConfig> readAnyConfigFileUri(
7359
}
7460
if (loader == null) {
7561
if (file.isScheme("file")) {
76-
return readAnyConfigFile(File.fromUri(file), preferNewest, onError);
62+
return await readAnyConfigFile(File.fromUri(file), preferNewest, onError);
7763
}
7864
loader = defaultLoader;
7965
}
80-
Uint8List bytes;
66+
if (preferNewest && file.pathSegments.last == ".packages") {
67+
var alternateFile = file.resolve(".dart_tool/package_config.json");
68+
Uint8List /*?*/ bytes;
69+
try {
70+
bytes = await loader(alternateFile);
71+
} catch (e) {
72+
onError(e);
73+
return const SimplePackageConfig.empty();
74+
}
75+
if (bytes != null) {
76+
return parsePackageConfigBytes(bytes, alternateFile, onError);
77+
}
78+
}
79+
Uint8List /*?*/ bytes;
8180
try {
8281
bytes = await loader(file);
8382
} catch (e) {
@@ -89,23 +88,18 @@ Future<PackageConfig> readAnyConfigFileUri(
8988
file.toString(), "file", "File cannot be read"));
9089
return const SimplePackageConfig.empty();
9190
}
91+
return parseAnyConfigFile(bytes, file, onError);
92+
}
93+
94+
/// Parses a `.packages` or `package_config.json` file's contents.
95+
///
96+
/// Assumes it's a JSON file if the first non-whitespace character
97+
/// is `{`, otherwise assumes it's a `.packages` file.
98+
PackageConfig parseAnyConfigFile(
99+
Uint8List bytes, Uri file, void onError(Object error)) {
92100
var firstChar = firstNonWhitespaceChar(bytes);
93101
if (firstChar != $lbrace) {
94102
// Definitely not a JSON object, probably a .packages.
95-
if (preferNewest) {
96-
// Check if there is a package_config.json file.
97-
var alternateFile = file.resolveUri(packageConfigJsonPath);
98-
Uint8List alternateBytes;
99-
try {
100-
alternateBytes = await loader(alternateFile);
101-
} catch (e) {
102-
onError(e);
103-
return const SimplePackageConfig.empty();
104-
}
105-
if (alternateBytes != null) {
106-
return parsePackageConfigBytes(alternateBytes, alternateFile, onError);
107-
}
108-
}
109103
return packages_file.parse(bytes, file, onError);
110104
}
111105
return parsePackageConfigBytes(bytes, file, onError);

pkgs/package_config/lib/src/package_config_json.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ PackageConfig parsePackageConfigBytes(
3939
try {
4040
jsonObject = _jsonUtf8Decoder.convert(bytes);
4141
} on FormatException catch (e) {
42-
onError(PackageConfigFormatException(e.message, e.source, e.offset));
42+
onError(PackageConfigFormatException.from(e));
4343
return const SimplePackageConfig.empty();
4444
}
4545
return parsePackageConfigJson(jsonObject, file, onError);
@@ -51,7 +51,7 @@ PackageConfig parsePackageConfigString(
5151
try {
5252
jsonObject = jsonDecode(source);
5353
} on FormatException catch (e) {
54-
onError(PackageConfigFormatException(e.message, e.source, e.offset));
54+
onError(PackageConfigFormatException.from(e));
5555
return const SimplePackageConfig.empty();
5656
}
5757
return parsePackageConfigJson(jsonObject, file, onError);
@@ -271,7 +271,6 @@ void writeDotPackages(PackageConfig config, Uri baseUri, StringSink output) {
271271
}
272272
}
273273
packages_file.write(output, config, baseUri: baseUri, comment: comment);
274-
return;
275274
}
276275

277276
/// If "extraData" is a JSON map, then return it, otherwise return null.
@@ -304,12 +303,10 @@ bool _validateJson(dynamic object) {
304303
if (object == null || true == object || false == object) return true;
305304
if (object is num || object is String) return true;
306305
if (object is List<dynamic>) {
307-
for (var element in object) if (!_validateJson(element)) return false;
308-
return true;
306+
return object.every(_validateJson);
309307
}
310308
if (object is Map<String, dynamic>) {
311-
for (var value in object.values) if (!_validateJson(value)) return false;
312-
return true;
309+
return object.values.every(_validateJson);
313310
}
314311
return false;
315312
}

pkgs/package_config/lib/src/util_io.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Future<Uint8List> defaultLoader(Uri uri) async {
1313
if (uri.isScheme("file")) {
1414
var file = File.fromUri(uri);
1515
try {
16-
return file.readAsBytes();
16+
return await file.readAsBytes();
1717
} catch (_) {
1818
return null;
1919
}

pkgs/package_config/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: package_config
2-
version: 1.9.1
2+
version: 1.9.2
33
description: Support for working with Package Configuration files.
44
homepage: https://github.com/dart-lang/package_config
55

pkgs/package_config/test/discovery_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ void main() {
301301
});
302302

303303
// Find package_config.json in subdir even if initial file syntax error.
304-
fileTest("specified file syntax error", {
305-
"anyname": "syntax error",
304+
fileTest("specified file syntax onError", {
305+
".packages": "syntax error",
306306
".dart_tool": {
307307
"package_config.json": packageConfigFile,
308308
},
309309
}, (Directory directory) async {
310-
var file = dirFile(directory, "anyname");
310+
var file = dirFile(directory, ".packages");
311311
var config = await loadPackageConfig(file);
312312
expect(config.version, 2);
313313
validatePackagesFile(config, directory);

pkgs/package_config/test/discovery_uri_test.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void main() {
6060
".dart_tool": {
6161
"package_config.json": packageConfigFile,
6262
}
63-
}, (Uri directory, loader) async {
63+
}, (directory, loader) async {
6464
var config = await findPackageConfigUri(directory, loader: loader);
6565
expect(config.version, 2); // Found package_config.json file.
6666
validatePackagesFile(config, directory);
@@ -71,7 +71,7 @@ void main() {
7171
".packages": packagesFile,
7272
"script.dart": "main(){}",
7373
"packages": {"shouldNotBeFound": {}}
74-
}, (Uri directory, loader) async {
74+
}, (directory, loader) async {
7575
var config = await findPackageConfigUri(directory, loader: loader);
7676
expect(config.version, 1); // Found .packages file.
7777
validatePackagesFile(config, directory);
@@ -86,7 +86,7 @@ void main() {
8686
"subdir": {
8787
"script.dart": "main(){}",
8888
}
89-
}, (Uri directory, loader) async {
89+
}, (directory, loader) async {
9090
var config = await findPackageConfigUri(directory.resolve("subdir/"),
9191
loader: loader);
9292
expect(config.version, 2);
@@ -97,7 +97,7 @@ void main() {
9797
loaderTest(".packages recursive", {
9898
".packages": packagesFile,
9999
"subdir": {"script.dart": "main(){}"}
100-
}, (Uri directory, loader) async {
100+
}, (directory, loader) async {
101101
var config;
102102
config = await findPackageConfigUri(directory.resolve("subdir/"),
103103
loader: loader);
@@ -240,9 +240,9 @@ void main() {
240240
throwsFormatException);
241241
});
242242

243-
loaderTest("specified file syntax error", {
243+
loaderTest("specified file syntax onError", {
244244
"anyname": "syntax error",
245-
}, (Uri directory, loader) async {
245+
}, (directory, loader) async {
246246
var file = directory.resolve("anyname");
247247
var hadError = false;
248248
await loadPackageConfigUri(file,
@@ -254,23 +254,22 @@ void main() {
254254
expect(hadError, true);
255255
});
256256

257-
// Find package_config.json in subdir even if initial file syntax error.
258-
loaderTest("specified file syntax error", {
257+
// Don't look for package_config.json if original file not named .packages.
258+
loaderTest("specified file syntax error with alternative", {
259259
"anyname": "syntax error",
260260
".dart_tool": {
261261
"package_config.json": packageConfigFile,
262262
},
263-
}, (Uri directory, loader) async {
263+
}, (directory, loader) async {
264264
var file = directory.resolve("anyname");
265-
var config = await loadPackageConfigUri(file, loader: loader);
266-
expect(config.version, 2);
267-
validatePackagesFile(config, directory);
265+
expect(() => loadPackageConfigUri(file, loader: loader),
266+
throwsFormatException);
268267
});
269268

270269
// A file starting with `{` is a package_config.json file.
271270
loaderTest("file syntax error with {", {
272271
".packages": "{syntax error",
273-
}, (Uri directory, loader) async {
272+
}, (directory, loader) async {
274273
var file = directory.resolve(".packages");
275274
var hadError = false;
276275
await loadPackageConfigUri(file,

0 commit comments

Comments
 (0)