Skip to content

Commit df4f2e6

Browse files
authored
Merge pull request #1796 from dart-lang/dart-tool-dir
Use .dart_tool/pub as the cache directory
2 parents c2b4902 + 68a6c12 commit df4f2e6

24 files changed

+785
-711
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.buildlog
22
.DS_Store
33
.idea
4-
.pub/
4+
.dart_tool/
55
.settings/
66
/build/
77
packages

lib/src/barback/asset_environment.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ class AssetEnvironment {
127127
new Map.fromIterable(packages, value: (packageName) {
128128
var package = graph.packages[packageName];
129129
if (mode != BarbackMode.DEBUG) return package;
130-
var cache = path.join('.pub/deps/debug', packageName);
130+
var cache =
131+
path.join(graph.entrypoint.cachePath, 'deps/debug', packageName);
131132
if (!dirExists(cache)) return package;
132133
return new CachedPackage(package, cache);
133134
}));

lib/src/barback/transformer_cache.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class TransformerCache {
4545
/// Loads the transformer cache for [environment].
4646
///
4747
/// This may modify the cache.
48-
TransformerCache.load(PackageGraph graph)
49-
: _graph = graph,
50-
_dir = graph.entrypoint.root.path(".pub/transformers") {
48+
TransformerCache.load(PackageGraph graph) : _graph = graph {
49+
graph.entrypoint.migrateCache();
50+
_dir = p.join(graph.entrypoint.cachePath, "transformers");
5151
_oldTransformers = _parseManifest();
5252
}
5353

lib/src/entrypoint.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,26 @@ class Entrypoint {
133133
/// The path to the entrypoint package's lockfile.
134134
String get lockFilePath => root.path('pubspec.lock');
135135

136+
/// The path to the entrypoint package's `.dart_tool/pub` cache directory.
137+
///
138+
/// If the old-style `.pub` directory is being used, this returns that
139+
/// instead.
140+
String get cachePath {
141+
var newPath = root.path('.dart_tool/pub');
142+
var oldPath = root.path('.pub');
143+
if (!dirExists(newPath) && dirExists(oldPath)) return oldPath;
144+
return newPath;
145+
}
146+
136147
/// The path to the directory containing precompiled dependencies.
137148
///
138149
/// We just precompile the debug version of a package. We're mostly interested
139150
/// in improving speed for development iteration loops, which usually use
140151
/// debug mode.
141-
String get _precompiledDepsPath => root.path('.pub', 'deps', 'debug');
152+
String get _precompiledDepsPath => p.join(cachePath, 'deps', 'debug');
142153

143154
/// The path to the directory containing dependency executable snapshots.
144-
String get _snapshotPath => root.path('.pub', 'bin');
155+
String get _snapshotPath => p.join(cachePath, 'bin');
145156

146157
/// Loads the entrypoint from a package at [rootDir].
147158
Entrypoint(String rootDir, SystemCache cache, {this.isGlobal: false})
@@ -370,6 +381,7 @@ class Entrypoint {
370381
/// Precompiles all executables from dependencies that don't transitively
371382
/// depend on [this] or on a path dependency.
372383
Future precompileExecutables({Iterable<String> changed}) async {
384+
migrateCache();
373385
_deleteExecutableSnapshots(changed: changed);
374386

375387
var executables = new Map<String, List<AssetId>>.fromIterable(
@@ -785,4 +797,21 @@ class Entrypoint {
785797
if (entryExists(symlink)) deleteEntry(symlink);
786798
if (packagesDir) createSymlink(packagesPath, symlink, relative: true);
787799
}
800+
801+
/// If the entrypoint uses the old-style `.pub` cache directory, migrates it
802+
/// to the new-style `.dart_tool/pub` directory.
803+
void migrateCache() {
804+
var oldPath = root.path('.pub');
805+
if (!dirExists(oldPath)) return;
806+
807+
var newPath = root.path('.dart_tool/pub');
808+
809+
// If both the old and new directories exist, something weird is going on.
810+
// Do nothing to avoid making things worse. Pub will prefer the new
811+
// directory anyway.
812+
if (dirExists(newPath)) return;
813+
814+
ensureDir(p.dirname(newPath));
815+
renameDir(oldPath, newPath);
816+
}
788817
}

lib/src/executable.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ Future<int> runExecutable(Entrypoint entrypoint, String package,
5050
}
5151
}
5252

53+
entrypoint.migrateCache();
54+
5355
// Unless the user overrides the verbosity, we want to filter out the
5456
// normal pub output shown while loading the environment.
5557
if (log.verbosity == log.Verbosity.NORMAL) {
@@ -60,7 +62,7 @@ Future<int> runExecutable(Entrypoint entrypoint, String package,
6062
if (p.extension(executable) != ".dart") executable += ".dart";
6163

6264
var localSnapshotPath =
63-
p.join(".pub", "bin", package, "$executable.snapshot");
65+
p.join(entrypoint.cachePath, "bin", package, "$executable.snapshot");
6466
if (!isGlobal &&
6567
fileExists(localSnapshotPath) &&
6668
// Dependencies are only snapshotted in release mode, since that's the

lib/src/io.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,6 @@ final bool runningFromSdk =
526526
final _dartRepoRegExp = new RegExp(r"/third_party/pkg/pub/("
527527
r"bin/pub\.dart"
528528
r"|"
529-
r"\.pub/pub\.test\.snapshot"
530-
r"|"
531529
r"test/.*_test\.dart"
532530
r")$");
533531

@@ -584,7 +582,7 @@ final String pubRoot = (() {
584582
return path.joinAll(components.take(testIndex));
585583
}
586584

587-
// Pub is either run from ".pub/pub.test.snapshot" or "bin/pub.dart".
585+
// Pub is run from "bin/pub.dart".
588586
return path.dirname(path.dirname(script));
589587
})();
590588

lib/src/package.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Package {
152152
/// This is similar to `p.join(dir, part1, ...)`, except that subclasses may
153153
/// override it to report that certain paths exist elsewhere than within
154154
/// [dir]. For example, a [CachedPackage]'s `lib` directory is in the
155-
/// `.pub/deps` directory.
155+
/// `.dart_tool/pub/deps` directory.
156156
String path(String part1,
157157
[String part2,
158158
String part3,

test/get/cache_transformed_dependency_test.dart

Lines changed: 102 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'package:path/path.dart' as p;
66
import 'package:test/test.dart';
77

8+
import 'package:pub/src/io.dart';
9+
810
import '../descriptor.dart' as d;
911
import '../serve/utils.dart';
1012
import '../test_pub.dart';
@@ -115,7 +117,7 @@ main() {
115117
await pubGet(output: contains("Precompiled foo."));
116118

117119
await d.dir(appPath, [
118-
d.dir(".pub/deps/debug/foo/lib",
120+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
119121
[d.file("foo.dart", "final message = 'Goodbye!';")])
120122
]).validate();
121123
});
@@ -146,7 +148,7 @@ main() {
146148
await pubGet(output: contains("Precompiled foo."));
147149

148150
await d.dir(appPath, [
149-
d.dir(".pub/deps/debug/foo/lib",
151+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
150152
[d.file("foo.dart", "final message = 'Goodbye!';")])
151153
]).validate();
152154
});
@@ -164,7 +166,7 @@ main() {
164166

165167
await pubGet(output: isNot(contains("Precompiled foo.")));
166168

167-
await d.dir(appPath, [d.nothing(".pub/deps")]).validate();
169+
await d.dir(appPath, [d.nothing(".dart_tool/pub/deps")]).validate();
168170
});
169171

170172
test("recaches when the dependency is updated", () async {
@@ -199,7 +201,7 @@ main() {
199201
await pubGet(output: contains("Precompiled foo."));
200202

201203
await d.dir(appPath, [
202-
d.dir(".pub/deps/debug/foo/lib",
204+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
203205
[d.file("foo.dart", "final message = 'Goodbye!';")])
204206
]).validate();
205207

@@ -209,7 +211,7 @@ main() {
209211
await pubGet(output: contains("Precompiled foo."));
210212

211213
await d.dir(appPath, [
212-
d.dir(".pub/deps/debug/foo/lib",
214+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
213215
[d.file("foo.dart", "final message = 'See ya!';")])
214216
]).validate();
215217
});
@@ -286,7 +288,7 @@ main() {
286288
await pubGet(output: contains("Precompiled foo."));
287289

288290
await d.dir(appPath, [
289-
d.dir(".pub/deps/debug/foo/lib",
291+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
290292
[d.file("foo.dart", "final mode = 'debug';")])
291293
]).validate();
292294
});
@@ -320,7 +322,7 @@ main() {
320322
await pubGet(output: contains("Precompiled foo."));
321323

322324
await d.dir(appPath, [
323-
d.dir(".pub/deps/debug/foo/lib",
325+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
324326
[d.file("foo.dart", "final message = 'Modified!';")])
325327
]).create();
326328

@@ -357,6 +359,51 @@ main() {
357359

358360
await pubGet(output: contains("Precompiled foo."));
359361

362+
// Manually reset the cache to its original state to prove that the
363+
// transformer won't be run again on it.
364+
await d.dir(appPath, [
365+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
366+
[d.file("foo.dart", "final message = 'Hello!';")])
367+
]).create();
368+
369+
var pub = await pubRun(args: ["bin/script"]);
370+
expect(pub.stdout, emits("Hello!"));
371+
await pub.shouldExit();
372+
});
373+
374+
test("reads cached packages from the old-style cache", () async {
375+
await servePackages((builder) {
376+
builder.serveRealPackage('barback');
377+
378+
builder.serve("foo", "1.2.3", deps: {
379+
'barback': 'any'
380+
}, pubspec: {
381+
'transformers': ['foo']
382+
}, contents: [
383+
d.dir("lib", [
384+
d.file("transformer.dart", replaceTransformer("Hello", "Goodbye")),
385+
d.file("foo.dart", "final message = 'Hello!';")
386+
])
387+
]);
388+
});
389+
390+
await d.dir(appPath, [
391+
d.appPubspec({"foo": "1.2.3"}),
392+
d.dir('bin', [
393+
d.file('script.dart', """
394+
import 'package:foo/foo.dart';
395+
396+
void main() => print(message);""")
397+
])
398+
]).create();
399+
400+
await pubGet(output: contains("Precompiled foo."));
401+
402+
// Move the directory to the old location to simulate it being created by an
403+
// older version of pub.
404+
renameDir(p.join(d.sandbox, appPath, '.dart_tool', 'pub'),
405+
p.join(d.sandbox, appPath, '.pub'));
406+
360407
// Manually reset the cache to its original state to prove that the
361408
// transformer won't be run again on it.
362409
await d.dir(appPath, [
@@ -369,6 +416,42 @@ main() {
369416
await pub.shouldExit();
370417
});
371418

419+
test("migrates the old-style cache", () async {
420+
await servePackages((builder) {
421+
builder.serveRealPackage('barback');
422+
423+
builder.serve("foo", "1.2.3", deps: {
424+
'barback': 'any'
425+
}, pubspec: {
426+
'transformers': ['foo']
427+
}, contents: [
428+
d.dir("lib", [
429+
d.file("transformer.dart", replaceTransformer("Hello", "Goodbye")),
430+
d.file("foo.dart", "final message = 'Hello!';")
431+
])
432+
]);
433+
});
434+
435+
await d.dir(appPath, [
436+
d.appPubspec({"foo": "1.2.3"}),
437+
438+
// Simulate an old-style cache directory.
439+
d.dir(".pub", [d.file("junk", "junk")])
440+
]).create();
441+
442+
await pubGet(output: contains("Precompiled foo."));
443+
444+
await d.dir(appPath, [d.nothing(".pub")]).validate();
445+
446+
await d.dir(appPath, [
447+
d.dir(".dart_tool/pub", [
448+
d.file("junk", "junk"),
449+
d.dir("deps/debug/foo/lib",
450+
[d.file("foo.dart", "final message = 'Goodbye!';")])
451+
])
452+
]).validate();
453+
});
454+
372455
// Regression test for issue 21087.
373456
test("hasInput works for static packages", () async {
374457
await servePackages((builder) {
@@ -436,7 +519,7 @@ main() {
436519
await pubGet(output: contains("Precompiled foo."));
437520

438521
await d.dir(appPath, [
439-
d.dir(".pub/deps/debug/foo/lib",
522+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
440523
[d.file("foo.dart", "final message = 'Goodbye!';")])
441524
]).validate();
442525

@@ -445,7 +528,8 @@ main() {
445528

446529
await pubGet(output: isNot(contains("Precompiled foo.")));
447530

448-
await d.dir(appPath, [d.nothing(".pub/deps/debug/foo")]).validate();
531+
await d
532+
.dir(appPath, [d.nothing(".dart_tool/pub/deps/debug/foo")]).validate();
449533
});
450534

451535
// Regression test for https://github.com/dart-lang/pub/issues/1586.
@@ -472,7 +556,8 @@ main() {
472556
await pubGet(output: contains("Precompiled foo"));
473557

474558
await d.dir(appPath, [
475-
d.file(".pub/deps/debug/foo/lib/inputs.txt", contains('hello.dart.copy'))
559+
d.file(".dart_tool/pub/deps/debug/foo/lib/inputs.txt",
560+
contains('hello.dart.copy'))
476561
]).validate();
477562

478563
await pubServe();
@@ -508,7 +593,7 @@ foo|lib/list_transformer.dart.copy""");
508593
await pubGet(
509594
args: ["--no-precompile"], output: isNot(contains("Precompiled")));
510595

511-
await d.nothing(p.join(appPath, ".pub")).validate();
596+
await d.nothing(p.join(appPath, ".dart_tool/pub")).validate();
512597
});
513598

514599
test("deletes the cache when the dependency is updated", () async {
@@ -543,7 +628,7 @@ foo|lib/list_transformer.dart.copy""");
543628
await pubGet(output: contains("Precompiled foo."));
544629

545630
await d.dir(appPath, [
546-
d.dir(".pub/deps/debug/foo/lib",
631+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
547632
[d.file("foo.dart", "final message = 'Goodbye!';")])
548633
]).validate();
549634

@@ -553,7 +638,9 @@ foo|lib/list_transformer.dart.copy""");
553638
await pubGet(
554639
args: ["--no-precompile"], output: isNot(contains("Precompiled")));
555640

556-
await d.nothing(p.join(appPath, ".pub/deps/debug/foo")).validate();
641+
await d
642+
.nothing(p.join(appPath, ".dart_tool/pub/deps/debug/foo"))
643+
.validate();
557644
});
558645

559646
test(
@@ -580,7 +667,7 @@ foo|lib/list_transformer.dart.copy""");
580667
await pubGet(output: contains("Precompiled foo."));
581668

582669
await d.dir(appPath, [
583-
d.dir(".pub/deps/debug/foo/lib",
670+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
584671
[d.file("foo.dart", "final message = 'Goodbye!';")])
585672
]).validate();
586673

@@ -589,7 +676,7 @@ foo|lib/list_transformer.dart.copy""");
589676
args: ["--no-precompile"], output: isNot(contains("Precompiled")));
590677

591678
await d.dir(appPath, [
592-
d.dir(".pub/deps/debug/foo/lib",
679+
d.dir(".dart_tool/pub/deps/debug/foo/lib",
593680
[d.file("foo.dart", "final message = 'Goodbye!';")])
594681
]).validate();
595682
});

0 commit comments

Comments
 (0)