Skip to content

Commit 2af3166

Browse files
dcharkesCommit Queue
authored andcommitted
[pkg] Introduce dart_data_home
The directory location depends on the current OS: - on Windows: - `%LOCALAPPDATA%\Dart\<tool>` - on Mac OS: - `$HOME/Library/Application Support/Dart/<tool>` - on Linux: - `$XDG_STATE_HOME/Dart/<tool>` if `$XDG_STATE_HOME` is defined, and - `$HOME/.local/state/Dart/<tool>` otherwise. The Dart data home can be overridden with the `DART_DATA_HOME` environment variable. This CL does not start using the new location yet. Bug: #60922 Bug: #41560 Bug: flutter/flutter#59430 Change-Id: I55e0ba610f8665ea3c9f053b36c943b097313046 Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-mac-release-try,pkg-win-release-arm64-try,pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/440462 Reviewed-by: Alexander Thomas <[email protected]> Auto-Submit: Daco Harkes <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent 615e1d1 commit 2af3166

File tree

8 files changed

+154
-14
lines changed

8 files changed

+154
-14
lines changed

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ vars = {
147147
"sync_http_rev": "c07f96f89a7eec7e3daac641fa6c587224fcfbaa",
148148
"tar_rev": "5a1ea943e70cdf3fa5e1102cdbb9418bd9b4b81a",
149149
"test_rev": "2be5ca067bdf09e999be2ad760ab8efab854e789",
150-
"tools_rev": "e1b1b4c6f3a25bcd52c201a535f2f3ed66f7ac7e",
150+
"tools_rev": "a4335eb80c55c3944a6af1a5ce20f5694298afdc",
151151
"vector_math_rev": "13f185f7e97d559e003f5ac79201da12f9a01049",
152152
"web_rev": "7e0853d6255d988a5813e680853565b4317da729",
153153
"webdev_rev": "7f376d242709e933fff70610503d0c5c09b2e17e",

pkg/dart_data_home/OWNERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set noparent
2+
file:/tools/OWNERS_INTEROP
3+
4+
# In addition allow global owners.
5+
file:/OWNERS
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @docImport 'src/dart_data_home.dart';
6+
7+
/// A package providing [getDartDataHome], a standardized way to access a
8+
/// user-specific data directory for Dart and Flutter tooling, defaulting to
9+
/// OS-conventions and configurable via the `DART_USER_HOME` environment
10+
/// variable.
11+
library;
12+
13+
export 'src/dart_data_home.dart';
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:cli_util/cli_util.dart';
8+
9+
/// Get the file system location for storing global data on the users' system.
10+
///
11+
/// The directory follows OS defaults and is not backed up or synchronized
12+
/// across devices by the OS.
13+
///
14+
/// The [packageName] must be a valid Dart package name. Prefer using the name
15+
/// of the package calling this function to avoid name clashes.
16+
///
17+
/// If provided, [environment] is used for environment variables. Otherwise,
18+
/// [Platform.environment] is used.
19+
///
20+
/// The directory location depends on the current [Platform.operatingSystem]:
21+
/// - on **Windows**:
22+
/// - `%LOCALAPPDATA%\Dart\<packageName>`
23+
/// - on **Mac OS**:
24+
/// - `$HOME/Library/Application Support/Dart/<packageName>`
25+
/// - on **Linux**:
26+
/// - `$XDG_STATE_HOME/Dart/<packageName>` if `$XDG_STATE_HOME` is defined,
27+
/// and
28+
/// - `$HOME/.local/state/Dart/<packageName>` otherwise.
29+
///
30+
/// The Dart data home can be overridden with the `DART_DATA_HOME` environment
31+
/// variable.
32+
///
33+
/// The directory won't be created, this method merely returns the recommended
34+
/// location.
35+
String getDartDataHome(String packageName, {Map<String, String>? environment}) {
36+
environment ??= Platform.environment;
37+
final overridden = environment['DART_DATA_HOME'];
38+
final Directory dartDataHome;
39+
if (overridden != null) {
40+
dartDataHome = Directory(overridden);
41+
} else {
42+
final dartBaseDirectories = BaseDirectories(
43+
'Dart',
44+
environment: environment,
45+
);
46+
// Use 'state', not 'data': Don't synchronize across devices.
47+
dartDataHome = Directory(dartBaseDirectories.stateHome);
48+
}
49+
return dartDataHome.uri.resolve('$packageName/').toFilePath();
50+
}

pkg/dart_data_home/pubspec.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: dart_data_home
2+
version: 0.1.0-wip
3+
description: >-
4+
A package providing a standardized way to access a user-specific data
5+
directory for Dart and Flutter tooling, defaulting to OS-conventions
6+
and configurable via the `DART_USER_HOME` environment variable.
7+
8+
# This package will be published at some point to be able to use it for Dart and
9+
# Flutter tooling that does not live in the Dart SDK.
10+
11+
environment:
12+
sdk: ^3.8.0
13+
14+
resolution: workspace
15+
16+
dependencies:
17+
cli_util: 0.5.0-wip
18+
19+
# We use 'any' version constraints here as we get our package versions from
20+
# the dart-lang/sdk repo's DEPS file. Note that this is a special case; the
21+
# best practice for packages is to specify their compatible version ranges.
22+
# See also https://dart.dev/tools/pub/dependencies.
23+
dev_dependencies:
24+
path: any
25+
test: any
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:dart_data_home/dart_data_home.dart';
8+
import 'package:path/path.dart' as p;
9+
import 'package:test/test.dart';
10+
11+
void main() {
12+
test('returns a non-empty string', () {
13+
final myAppHome = getDartDataHome('my_app');
14+
expect(myAppHome, isNotEmpty);
15+
});
16+
17+
test('has an ancestor folder that exists', () {
18+
void expectAncestorExists(String path) {
19+
// We expect that first two segments of the path exist. This is really
20+
// just a dummy check that some part of the path exists.
21+
final ancestorPath = p.joinAll(p.split(path).take(2));
22+
expect(Directory(ancestorPath).existsSync(), isTrue);
23+
}
24+
25+
final myAppHome = getDartDataHome('my_app');
26+
expectAncestorExists(myAppHome);
27+
});
28+
29+
test('empty environment throws exception', () async {
30+
expect(
31+
() => getDartDataHome('some_app', environment: <String, String>{}),
32+
throwsA(isA<Exception>()),
33+
);
34+
});
35+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ workspace:
3434
- pkg/dart2native
3535
- pkg/dart2wasm
3636
- pkg/dartdev
37+
- pkg/dart_data_home
3738
- pkg/dart_internal
3839
- pkg/dart_service_protocol_shared
3940
- pkg/dds

tools/bots/test_matrix.json

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
"tests/ffi/",
171171
"pkg/_fe_analyzer_shared/",
172172
"pkg/build_integration/",
173+
"pkg/dart_data_home/",
173174
"pkg/dart_internal/",
174175
"pkg/dart2native/",
175176
"pkg/dart2js_tools/",
@@ -310,12 +311,12 @@
310311
},
311312
"vm-aot-mac-(debug|product|release)-(x64|x64c|arm64|arm64c)": {
312313
"options": {
313-
"gen-snapshot-format" : "macho-dylib"
314+
"gen-snapshot-format": "macho-dylib"
314315
}
315316
},
316317
"vm-aot-(linux|mac|win)-(debug|product|release)-(simarm|simarm_x64|simarm64|simriscv32|simriscv64)": {
317318
"options": {
318-
"gen-snapshot-format" : "elf"
319+
"gen-snapshot-format": "elf"
319320
}
320321
},
321322
"vm-asan-(linux|mac|win)-(debug|product|release)-(ia32|x64|arm64|simarm|simarm64|simriscv32|simriscv64)": {},
@@ -330,12 +331,12 @@
330331
},
331332
"vm-aot-lsan-(linux|mac)-(debug|product|release)-(x64|arm64|simarm|simarm64|simriscv32|simriscv64)": {
332333
"options": {
333-
"gen-snapshot-format" : "elf"
334+
"gen-snapshot-format": "elf"
334335
}
335336
},
336337
"vm-aot-msan-linux-(debug|product|release)-(x64|arm64|simarm64|simriscv64)": {
337338
"options": {
338-
"gen-snapshot-format" : "elf"
339+
"gen-snapshot-format": "elf"
339340
}
340341
},
341342
"vm-aot-tsan-(linux|mac)-(debug|product|release)-(x64|arm64|simarm64|simriscv64)": {
@@ -345,27 +346,37 @@
345346
},
346347
"vm-aot-ubsan-(linux|mac|win)-(debug|product|release)-(x64|arm64|simarm|simarm64|simriscv32|simriscv64)": {
347348
"options": {
348-
"gen-snapshot-format" : "elf"
349+
"gen-snapshot-format": "elf"
349350
}
350351
},
351352
"vm-linux-(debug|product|release)-simarm64_arm64": {
352353
"options": {
353-
"vm-options": ["--use_simulator=true"], "use-qemu": true
354+
"vm-options": [
355+
"--use_simulator=true"
356+
],
357+
"use-qemu": true
354358
}
355359
},
356360
"vm-linux-(debug|product|release)-simarm64_arm64-nosim": {
357361
"options": {
358-
"vm-options": ["--use_simulator=false"], "use-qemu": true
362+
"vm-options": [
363+
"--use_simulator=false"
364+
],
365+
"use-qemu": true
359366
}
360367
},
361368
"vm-mac-(debug|product|release)-simarm64_arm64": {
362369
"options": {
363-
"vm-options": ["--use_simulator=true"]
370+
"vm-options": [
371+
"--use_simulator=true"
372+
]
364373
}
365374
},
366375
"vm-mac-(debug|product|release)-simarm64_arm64-nosim": {
367376
"options": {
368-
"vm-options": ["--use_simulator=false"]
377+
"vm-options": [
378+
"--use_simulator=false"
379+
]
369380
}
370381
},
371382
"dart2js-(linux|win)-chrome": {
@@ -501,12 +512,12 @@
501512
"vm-aot-android-(debug|product|release)-arm_x64": {
502513
"options": {
503514
"builder-tag": "crossword",
504-
"gen-snapshot-format" : "elf"
515+
"gen-snapshot-format": "elf"
505516
}
506517
},
507518
"vm-aot-android-(debug|product|release)-(ia32|x64|x64c|arm|arm64|arm64c|riscv64)": {
508519
"options": {
509-
"gen-snapshot-format" : "elf"
520+
"gen-snapshot-format": "elf"
510521
}
511522
},
512523
"vm-aot-dwarf-linux-(debug|release|product)-x64": {
@@ -515,7 +526,7 @@
515526
"vm-options": [
516527
"--dwarf_stack_traces"
517528
],
518-
"gen-snapshot-format" : "elf"
529+
"gen-snapshot-format": "elf"
519530
}
520531
},
521532
"vm-aot-obfuscate-linux-(debug|release|product)-x64": {
@@ -2667,7 +2678,7 @@
26672678
},
26682679
{
26692680
"name": "dart2wasm unit tests",
2670-
"script": "out/ReleaseX64/dart-sdk/bin/dart",
2681+
"script": "out/ReleaseX64/dart-sdk/bin/dart",
26712682
"testRunner": true,
26722683
"arguments": [
26732684
"pkg/dart2wasm/test/dry_run/dry_run_test.dart",

0 commit comments

Comments
 (0)